Skip to content

Commit

Permalink
Merge pull request #34545 from geoand/#29987
Browse files Browse the repository at this point in the history
Properly construct instance of LateBoundBatchSpanProcessor
  • Loading branch information
geoand authored Jul 19, 2023
2 parents c3f47ac + c50213f commit 5726f7c
Show file tree
Hide file tree
Showing 20 changed files with 228 additions and 395 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import jakarta.enterprise.inject.Default;
import jakarta.inject.Singleton;

import org.jboss.jandex.ClassType;
import org.jboss.jandex.DotName;
import org.jboss.logging.Logger;

Expand Down Expand Up @@ -278,9 +279,10 @@ void generateDataSourceBeans(AgroalRecorder recorder,
.scope(Singleton.class)
.setRuntimeInit()
.unremovable()
.addInjectionPoint(ClassType.create(DotName.createSimple(DataSources.class)))
// pass the runtime config into the recorder to ensure that the DataSource related beans
// are created after runtime configuration has been set up
.supplier(recorder.agroalDataSourceSupplier(dataSourceName, dataSourcesRuntimeConfig));
.createWith(recorder.agroalDataSourceSupplier(dataSourceName, dataSourcesRuntimeConfig));

if (entry.getValue().isDefault) {
configurator.addQualifier(Default.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package io.quarkus.agroal.runtime;

import java.util.function.Function;
import java.util.function.Supplier;

import io.agroal.api.AgroalDataSource;
import io.quarkus.arc.SyntheticCreationalContext;
import io.quarkus.datasource.runtime.DataSourcesRuntimeConfig;
import io.quarkus.runtime.annotations.Recorder;

Expand All @@ -18,13 +20,14 @@ public DataSourceSupport get() {
};
}

public Supplier<AgroalDataSource> agroalDataSourceSupplier(String dataSourceName,
public Function<SyntheticCreationalContext<AgroalDataSource>, AgroalDataSource> agroalDataSourceSupplier(
String dataSourceName,
@SuppressWarnings("unused") DataSourcesRuntimeConfig dataSourcesRuntimeConfig) {
final AgroalDataSource agroalDataSource = DataSources.fromName(dataSourceName);
return new Supplier<AgroalDataSource>() {
return new Function<>() {
@Override
public AgroalDataSource get() {
return agroalDataSource;
public AgroalDataSource apply(SyntheticCreationalContext<AgroalDataSource> context) {
DataSources dataSources = context.getInjectedReference(DataSources.class);
return dataSources.getDataSource(dataSourceName);
}
};
}
Expand Down
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 All @@ -28,6 +30,7 @@
import io.quarkus.arc.DefaultBean;
import io.quarkus.opentelemetry.runtime.config.build.OTelBuildConfig;
import io.quarkus.opentelemetry.runtime.config.runtime.OTelRuntimeConfig;
import io.quarkus.opentelemetry.runtime.exporter.otlp.RemoveableLateBoundBatchSpanProcessor;
import io.quarkus.opentelemetry.runtime.tracing.DelayedAttributes;
import io.quarkus.opentelemetry.runtime.tracing.DropTargetsSampler;
import io.quarkus.opentelemetry.runtime.tracing.TracerRecorder;
Expand Down Expand Up @@ -62,6 +65,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 Expand Up @@ -144,7 +155,8 @@ public SdkTracerProviderBuilder apply(SdkTracerProviderBuilder builder,
ConfigProperties configProperties) {
if (oTelBuildConfig.traces().enabled().orElse(TRUE)) {
idGenerator.stream().findFirst().ifPresent(builder::setIdGenerator); // from cdi
spanProcessors.stream().forEach(builder::addSpanProcessor);
spanProcessors.stream().filter(sp -> !(sp instanceof RemoveableLateBoundBatchSpanProcessor))
.forEach(builder::addSpanProcessor);
}
return builder;
}
Expand Down
Loading

0 comments on commit 5726f7c

Please sign in to comment.