From fd3662fd4c46948b4329bed4262e746b84184b6d Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Tue, 17 Sep 2024 20:02:13 +0200 Subject: [PATCH] Use AutoConfiguredOpenTelemetrySdkBuilderCustomizer for Azure (#226) --- .../deployment/AzureExporterProcessor.java | 24 +++++++++---------- .../azure/runtime/AzureMonitorCustomizer.java | 21 ++++++++++++++++ .../exporter/azure/runtime/AzureRecorder.java | 21 ++++------------ 3 files changed, 37 insertions(+), 29 deletions(-) create mode 100644 quarkus-opentelemetry-exporter-azure/runtime/src/main/java/io/quarkiverse/opentelemetry/exporter/azure/runtime/AzureMonitorCustomizer.java diff --git a/quarkus-opentelemetry-exporter-azure/deployment/src/main/java/io/quarkiverse/opentelemetry/exporter/azure/deployment/AzureExporterProcessor.java b/quarkus-opentelemetry-exporter-azure/deployment/src/main/java/io/quarkiverse/opentelemetry/exporter/azure/deployment/AzureExporterProcessor.java index 3757cec..8a5b41e 100644 --- a/quarkus-opentelemetry-exporter-azure/deployment/src/main/java/io/quarkiverse/opentelemetry/exporter/azure/deployment/AzureExporterProcessor.java +++ b/quarkus-opentelemetry-exporter-azure/deployment/src/main/java/io/quarkiverse/opentelemetry/exporter/azure/deployment/AzureExporterProcessor.java @@ -5,6 +5,7 @@ import java.util.function.BooleanSupplier; import java.util.stream.Stream; +import io.quarkus.opentelemetry.runtime.AutoConfiguredOpenTelemetrySdkBuilderCustomizer; import jakarta.enterprise.inject.Instance; import jakarta.inject.Singleton; @@ -18,15 +19,9 @@ import com.azure.core.http.vertx.VertxProvider; import io.netty.handler.ssl.OpenSsl; -import io.opentelemetry.sdk.trace.SpanProcessor; -import io.opentelemetry.sdk.trace.export.SpanExporter; +import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider; import io.opentelemetry.sdk.trace.samplers.Sampler; -import io.quarkiverse.opentelemetry.exporter.azure.runtime.AzureEndpointSampler; -import io.quarkiverse.opentelemetry.exporter.azure.runtime.AzureExporterBuildConfig; -import io.quarkiverse.opentelemetry.exporter.azure.runtime.AzureExporterQuarkusRuntimeConfig; -import io.quarkiverse.opentelemetry.exporter.azure.runtime.AzureExporterRuntimeConfig; -import io.quarkiverse.opentelemetry.exporter.azure.runtime.AzureRecorder; -import io.quarkiverse.opentelemetry.exporter.common.runtime.LateBoundSpanProcessor; +import io.quarkiverse.opentelemetry.exporter.azure.runtime.*; import io.quarkus.arc.deployment.SyntheticBeanBuildItem; import io.quarkus.deployment.annotations.BuildProducer; import io.quarkus.deployment.annotations.BuildStep; @@ -138,17 +133,20 @@ void registerExternalExporter(BuildProducer build @BuildStep @Record(ExecutionTime.RUNTIME_INIT) - SyntheticBeanBuildItem installBatchSpanProcessorForAzure(AzureRecorder recorder, + SyntheticBeanBuildItem openTelemetryCustomizer(AzureRecorder recorder, AzureExporterRuntimeConfig runtimeConfig, AzureExporterQuarkusRuntimeConfig quarkusRuntimeConfig) { - return SyntheticBeanBuildItem.configure(LateBoundSpanProcessor.class) - .types(SpanProcessor.class) + return SyntheticBeanBuildItem.configure(AutoConfiguredOpenTelemetrySdkBuilderCustomizer.class) + .types(AutoConfigurationCustomizerProvider.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.createSpanProcessorForAzure(runtimeConfig, quarkusRuntimeConfig)) + new Type[] { + ClassType.create(DotName.createSimple(AutoConfigurationCustomizerProvider.class.getName())) }, + null)) + .createWith(recorder.createAzureMonitorCustomizer(runtimeConfig, quarkusRuntimeConfig)) .done(); + } @BuildStep diff --git a/quarkus-opentelemetry-exporter-azure/runtime/src/main/java/io/quarkiverse/opentelemetry/exporter/azure/runtime/AzureMonitorCustomizer.java b/quarkus-opentelemetry-exporter-azure/runtime/src/main/java/io/quarkiverse/opentelemetry/exporter/azure/runtime/AzureMonitorCustomizer.java new file mode 100644 index 0000000..8134cbd --- /dev/null +++ b/quarkus-opentelemetry-exporter-azure/runtime/src/main/java/io/quarkiverse/opentelemetry/exporter/azure/runtime/AzureMonitorCustomizer.java @@ -0,0 +1,21 @@ +package io.quarkiverse.opentelemetry.exporter.azure.runtime; + +import com.azure.monitor.opentelemetry.exporter.AzureMonitorExporterBuilder; + +import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder; +import io.quarkus.opentelemetry.runtime.AutoConfiguredOpenTelemetrySdkBuilderCustomizer; + +public class AzureMonitorCustomizer implements AutoConfiguredOpenTelemetrySdkBuilderCustomizer { + + private final String azureConnectionString; + + public AzureMonitorCustomizer(String azureConnectionString) { + this.azureConnectionString = azureConnectionString; + } + + @Override + public void customize(AutoConfiguredOpenTelemetrySdkBuilder builder) { + new AzureMonitorExporterBuilder().connectionString(azureConnectionString) + .install(builder); + } +} diff --git a/quarkus-opentelemetry-exporter-azure/runtime/src/main/java/io/quarkiverse/opentelemetry/exporter/azure/runtime/AzureRecorder.java b/quarkus-opentelemetry-exporter-azure/runtime/src/main/java/io/quarkiverse/opentelemetry/exporter/azure/runtime/AzureRecorder.java index 42a8b02..42451bf 100644 --- a/quarkus-opentelemetry-exporter-azure/runtime/src/main/java/io/quarkiverse/opentelemetry/exporter/azure/runtime/AzureRecorder.java +++ b/quarkus-opentelemetry-exporter-azure/runtime/src/main/java/io/quarkiverse/opentelemetry/exporter/azure/runtime/AzureRecorder.java @@ -4,11 +4,6 @@ import java.util.function.Function; import java.util.regex.Pattern; -import com.azure.monitor.opentelemetry.exporter.AzureMonitorExporterBuilder; - -import io.opentelemetry.sdk.trace.export.BatchSpanProcessor; -import io.opentelemetry.sdk.trace.export.SpanExporter; -import io.quarkiverse.opentelemetry.exporter.common.runtime.LateBoundSpanProcessor; import io.quarkus.arc.SyntheticCreationalContext; import io.quarkus.runtime.annotations.Recorder; @@ -17,20 +12,14 @@ public class AzureRecorder { public static final Pattern SEMI_COLON_PATTERN = Pattern.compile(";"); - public Function, LateBoundSpanProcessor> createSpanProcessorForAzure( + public Function, AzureMonitorCustomizer> createAzureMonitorCustomizer( AzureExporterRuntimeConfig runtimeConfig, AzureExporterQuarkusRuntimeConfig quarkusRuntimeConfig) { return new Function<>() { @Override - public LateBoundSpanProcessor apply(SyntheticCreationalContext context) { - try { - String azureConnectionString = findConnectionString(runtimeConfig, quarkusRuntimeConfig); - SpanExporter azureSpanExporter = new AzureMonitorExporterBuilder() - .connectionString(azureConnectionString) - .buildTraceExporter(); - return new LateBoundSpanProcessor(BatchSpanProcessor.builder(azureSpanExporter).build()); - } catch (IllegalArgumentException iae) { - throw new IllegalStateException("Unable to install OTel Azure Exporter", iae); - } + public AzureMonitorCustomizer apply( + SyntheticCreationalContext objectSyntheticCreationalContext) { + String azureConnectionString = findConnectionString(runtimeConfig, quarkusRuntimeConfig); + return new AzureMonitorCustomizer(azureConnectionString); } }; }