Skip to content

Commit

Permalink
Properly construct instance of LateBoundBatchSpanProcessor
Browse files Browse the repository at this point in the history
This is now done entirely in a CDI producer method which is then fully initialized when OpenTelemetry autoconfigures itself

Fixes: quarkusio#29987 (which with newer Quarkus versions is even more problematic)
  • Loading branch information
geoand committed Jul 18, 2023
1 parent 3f9de0c commit 690c46b
Show file tree
Hide file tree
Showing 16 changed files with 205 additions and 387 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.DotName;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.exporter.otlp.internal.OtlpSpanExporterProvider;
import io.opentelemetry.instrumentation.annotations.SpanAttribute;
Expand All @@ -30,6 +29,7 @@
import io.quarkus.agroal.spi.OpenTelemetryInitBuildItem;
import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.AnnotationsTransformerBuildItem;
import io.quarkus.arc.deployment.BeanContainerBuildItem;
import io.quarkus.arc.deployment.InterceptorBindingRegistrarBuildItem;
import io.quarkus.arc.deployment.ValidationPhaseBuildItem.ValidationErrorBuildItem;
import io.quarkus.arc.processor.AnnotationsTransformer;
Expand All @@ -43,7 +43,6 @@
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.LaunchModeBuildItem;
import io.quarkus.deployment.builditem.RemovedResourceBuildItem;
import io.quarkus.deployment.builditem.ShutdownContextBuildItem;
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import io.quarkus.deployment.builditem.nativeimage.RuntimeReinitializedClassBuildItem;
Expand All @@ -56,7 +55,6 @@
import io.quarkus.opentelemetry.runtime.tracing.cdi.WithSpanInterceptor;
import io.quarkus.opentelemetry.runtime.tracing.intrumentation.InstrumentationRecorder;
import io.quarkus.runtime.LaunchMode;
import io.quarkus.runtime.RuntimeValue;
import io.quarkus.runtime.configuration.ConfigurationException;
import io.quarkus.vertx.core.deployment.CoreVertxBuildItem;

Expand Down Expand Up @@ -185,21 +183,23 @@ public void transform(TransformationContext context) {
@Produce(OpenTelemetryInitBuildItem.class)
void createOpenTelemetry(
OpenTelemetryRecorder recorder,
InstrumentationRecorder instrumentationRecorder,
CoreVertxBuildItem vertx,
LaunchModeBuildItem launchMode,
ShutdownContextBuildItem shutdownContextBuildItem) {
LaunchModeBuildItem launchMode) {

if (launchMode.getLaunchMode() == LaunchMode.DEVELOPMENT || launchMode.getLaunchMode() == LaunchMode.TEST) {
recorder.resetGlobalOpenTelemetryForDevMode();
}

RuntimeValue<OpenTelemetry> openTelemetry = recorder.createOpenTelemetry(shutdownContextBuildItem);

recorder.eagerlyCreateContextStorage();
recorder.storeVertxOnContextStorage(vertx.getVertx());
}

@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
void setupVertx(InstrumentationRecorder recorder,
BeanContainerBuildItem beanContainerBuildItem) {

instrumentationRecorder.setTracer(instrumentationRecorder.createTracers(openTelemetry));
recorder.setupVertxTracer(beanContainerBuildItem.getValue());
}

@BuildStep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,28 @@

import java.util.function.BooleanSupplier;

import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.BeanContainerBuildItem;
import jakarta.enterprise.inject.Instance;
import jakarta.inject.Singleton;

import org.jboss.jandex.ClassType;
import org.jboss.jandex.DotName;
import org.jboss.jandex.ParameterizedType;
import org.jboss.jandex.Type;

import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import io.quarkus.arc.deployment.SyntheticBeanBuildItem;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.BuildSteps;
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.LaunchModeBuildItem;
import io.quarkus.opentelemetry.runtime.config.build.OTelBuildConfig;
import io.quarkus.opentelemetry.runtime.config.build.exporter.OtlpExporterBuildConfig;
import io.quarkus.opentelemetry.runtime.config.runtime.OTelRuntimeConfig;
import io.quarkus.opentelemetry.runtime.config.runtime.exporter.OtlpExporterRuntimeConfig;
import io.quarkus.opentelemetry.runtime.exporter.otlp.OtlpExporterProvider;
import io.quarkus.opentelemetry.runtime.exporter.otlp.LateBoundBatchSpanProcessor;
import io.quarkus.opentelemetry.runtime.exporter.otlp.OtlpRecorder;
import io.quarkus.vertx.deployment.VertxBuildItem;
import io.quarkus.vertx.core.deployment.CoreVertxBuildItem;

@BuildSteps(onlyIf = OtlpExporterProcessor.OtlpExporterEnabled.class)
public class OtlpExporterProcessor {
Expand All @@ -34,25 +42,24 @@ public boolean getAsBoolean() {
}
}

@BuildStep
AdditionalBeanBuildItem createBatchSpanProcessor() {
return AdditionalBeanBuildItem.builder()
.addBeanClass(OtlpExporterProvider.class)
.setUnremovable().build();
}

@SuppressWarnings("deprecation")
@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
void installBatchSpanProcessorForOtlp(
OtlpRecorder recorder,
LaunchModeBuildItem launchModeBuildItem,
SyntheticBeanBuildItem createBatchSpanProcessor(OtlpRecorder recorder,
OTelRuntimeConfig otelRuntimeConfig,
OtlpExporterRuntimeConfig exporterRuntimeConfig,
VertxBuildItem vertxBuildItem,
BeanContainerBuildItem beanContainerBuildItem) {
recorder.installBatchSpanProcessorForOtlp(otelRuntimeConfig,
exporterRuntimeConfig,
vertxBuildItem.getVertx(),
launchModeBuildItem.getLaunchMode());
CoreVertxBuildItem vertxBuildItem) {
return SyntheticBeanBuildItem
.configure(LateBoundBatchSpanProcessor.class)
.types(SpanProcessor.class)
.setRuntimeInit()
.scope(Singleton.class)
.unremovable()
.addInjectionPoint(ParameterizedType.create(DotName.createSimple(Instance.class),
new Type[] { ClassType.create(DotName.createSimple(SpanExporter.class.getName())) }, null))
.createWith(recorder.batchSpanProcessorForOtlp(otelRuntimeConfig, exporterRuntimeConfig,
vertxBuildItem.getVertx()))
.done();

}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -143,42 +143,14 @@ void dropNames(

@BuildStep
@Record(ExecutionTime.STATIC_INIT)
TracerIdGeneratorBuildItem createIdGenerator(TracerRecorder recorder,
BeanContainerBuildItem beanContainerBuildItem) {
return new TracerIdGeneratorBuildItem(recorder.createIdGenerator());
}

@BuildStep
@Record(ExecutionTime.STATIC_INIT)
TracerResourceBuildItem createResource(TracerRecorder recorder,
ApplicationInfoBuildItem appInfo,
BeanContainerBuildItem beanContainerBuildItem) {
String serviceName = appInfo.getName();
String serviceVersion = appInfo.getVersion();
return new TracerResourceBuildItem(recorder.createResource(Version.getVersion(), serviceName, serviceVersion));
}

@BuildStep
@Record(ExecutionTime.STATIC_INIT)
TracerSpanExportersBuildItem createSpanExporters(TracerRecorder recorder,
BeanContainerBuildItem beanContainerBuildItem) {
return new TracerSpanExportersBuildItem(recorder.createSpanExporter());
}

@BuildStep
@Record(ExecutionTime.STATIC_INIT)
TracerSpanProcessorsBuildItem createSpanProcessors(TracerRecorder recorder,
BeanContainerBuildItem beanContainerBuildItem) {
return new TracerSpanProcessorsBuildItem(recorder.createSpanProcessors());
}

@BuildStep
@Record(ExecutionTime.STATIC_INIT)
void setupTracer(
void staticInitSetup(
TracerRecorder recorder,
ApplicationInfoBuildItem appInfo,
BeanContainerBuildItem beanContainerBuildItem,
DropNonApplicationUrisBuildItem dropNonApplicationUris,
DropStaticResourcesBuildItem dropStaticResources) {

recorder.setAttributes(beanContainerBuildItem.getValue(), Version.getVersion(),
appInfo.getName(), appInfo.getVersion());
recorder.setupSampler(
dropNonApplicationUris.getDropNames(),
dropStaticResources.getDropNames());
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class OtlpExporterBadEndpointTest {
.withEmptyApplication()
.overrideConfigKey("quarkus.otel.traces.exporter", "cdi")
.overrideConfigKey("quarkus.opentelemetry.tracer.exporter.otlp.endpoint", "httz://nada:zero")
.setExpectedException(IllegalStateException.class);
.setExpectedException(IllegalArgumentException.class);

@Inject
OpenTelemetry openTelemetry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.function.BiFunction;

import jakarta.enterprise.inject.Any;
import jakarta.enterprise.inject.Disposes;
import jakarta.enterprise.inject.Instance;
import jakarta.enterprise.inject.Produces;
import jakarta.inject.Inject;
Expand All @@ -18,6 +19,7 @@
import org.eclipse.microprofile.config.ConfigProvider;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.resources.Resource;
Expand Down Expand Up @@ -62,6 +64,14 @@ public class OpenTelemetryProducer {
@Inject
ApplicationConfig appConfig;

public void disposeOfOpenTelemetry(@Disposes OpenTelemetry openTelemetry) {
if (openTelemetry instanceof OpenTelemetrySdk) {
var openTelemetrySdk = ((OpenTelemetrySdk) openTelemetry);
openTelemetrySdk.getSdkTracerProvider().forceFlush();
openTelemetrySdk.getSdkTracerProvider().shutdown();
}
}

@Produces
@Singleton
@DefaultBean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,10 @@

import java.util.function.Supplier;

import jakarta.enterprise.inject.Any;
import jakarta.enterprise.inject.spi.BeanManager;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.events.GlobalEventEmitterProvider;
import io.opentelemetry.api.logs.GlobalLoggerProvider;
import io.opentelemetry.context.ContextStorage;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.quarkus.arc.Arc;
import io.quarkus.runtime.RuntimeValue;
import io.quarkus.runtime.ShutdownContext;
import io.quarkus.runtime.annotations.Recorder;
import io.vertx.core.Vertx;

Expand All @@ -29,24 +21,6 @@ public void resetGlobalOpenTelemetryForDevMode() {
GlobalEventEmitterProvider.resetForTest();
}

/* RUNTIME INIT */
public RuntimeValue<OpenTelemetry> createOpenTelemetry(ShutdownContext shutdownContext) {

BeanManager beanManager = Arc.container().beanManager();

OpenTelemetry openTelemetry = beanManager.createInstance()
.select(OpenTelemetry.class, Any.Literal.INSTANCE).get();

// Because we are producing the ObfuscatedOpenTelemetry. These methods are not be available otherwise.
// Register shutdown tasks, because we are using CDI beans
shutdownContext.addShutdownTask(() -> {
((OpenTelemetrySdk) openTelemetry).getSdkTracerProvider().forceFlush();
((OpenTelemetrySdk) openTelemetry).getSdkTracerProvider().shutdown();
});

return new RuntimeValue<>(openTelemetry);
}

/* RUNTIME INIT */
public void eagerlyCreateContextStorage() {
ContextStorage.get();
Expand Down

This file was deleted.

Loading

0 comments on commit 690c46b

Please sign in to comment.