From 7cf0733fb8bd82e663bf7efb2005bfa39567fd54 Mon Sep 17 00:00:00 2001 From: Lin Gao Date: Thu, 26 Sep 2024 15:50:19 +0800 Subject: [PATCH] [Issue-380] [Enhancement] Tries to inject Vertx instance from CDI using a qualifier --- implementation/exporters/pom.xml | 9 +++++ .../AbstractVertxExporterProvider.java | 34 +++++++++++++++++-- pom.xml | 6 ++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/implementation/exporters/pom.xml b/implementation/exporters/pom.xml index efb2c633..46331fe0 100644 --- a/implementation/exporters/pom.xml +++ b/implementation/exporters/pom.xml @@ -16,6 +16,10 @@ io.smallrye.reactive mutiny + + io.smallrye.common + smallrye-common-annotation + io.smallrye.opentelemetry @@ -38,6 +42,11 @@ io.opentelemetry opentelemetry-exporter-otlp-common + + jakarta.enterprise + jakarta.enterprise.cdi-api + provided + diff --git a/implementation/exporters/src/main/java/io/smallrye/opentelemetry/implementation/exporters/AbstractVertxExporterProvider.java b/implementation/exporters/src/main/java/io/smallrye/opentelemetry/implementation/exporters/AbstractVertxExporterProvider.java index 32971751..310bcff3 100644 --- a/implementation/exporters/src/main/java/io/smallrye/opentelemetry/implementation/exporters/AbstractVertxExporterProvider.java +++ b/implementation/exporters/src/main/java/io/smallrye/opentelemetry/implementation/exporters/AbstractVertxExporterProvider.java @@ -8,12 +8,18 @@ import java.net.URI; import java.net.URISyntaxException; import java.time.Duration; +import java.util.logging.Level; +import java.util.logging.Logger; + +import jakarta.enterprise.inject.Instance; +import jakarta.enterprise.inject.spi.CDI; import io.opentelemetry.api.metrics.MeterProvider; import io.opentelemetry.exporter.internal.grpc.GrpcExporter; import io.opentelemetry.exporter.internal.http.HttpExporter; import io.opentelemetry.exporter.internal.marshal.Marshaler; import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; +import io.smallrye.common.annotation.Identifier; import io.smallrye.opentelemetry.implementation.exporters.sender.VertxGrpcSender; import io.smallrye.opentelemetry.implementation.exporters.sender.VertxHttpSender; import io.vertx.core.Vertx; @@ -32,6 +38,10 @@ public abstract class AbstractVertxExporterProvider { private static final String MIMETYPE_PROTOBUF = "application/x-protobuf"; + private static final String OTEL_EXPORTER_VERTX_CDI_QUALIFIER = "otel.exporter.vertx.cdi.identifier"; + + private static final Logger logger = Logger.getLogger(AbstractVertxExporterProvider.class.getName()); + private final String signalType; private final String exporterName; @@ -57,6 +67,26 @@ protected HttpExporter createHttpExporter(ConfigProperties config, String htt false);//TODO: this will be enhanced in the future } + /** + * If the CDI qualifier is specified in the config, it tries to get it from CDI, and if CDI does not provide such + * an instance on the specified qualifier, it will log some WARNING messages and return a new Vertx instance. + * If the CDI qualifier is not specified in the config, it creates a new Vertx instance. + */ + private Vertx getVertx(ConfigProperties config) { + String cdiQualifier = config.getString(OTEL_EXPORTER_VERTX_CDI_QUALIFIER); + if (cdiQualifier != null && !cdiQualifier.isEmpty()) { + Instance vertxCDI = CDI.current().select(Vertx.class, Identifier.Literal.of(cdiQualifier)); + if (vertxCDI != null && vertxCDI.isResolvable()) { + return vertxCDI.get(); + } else { + logger.log(Level.WARNING, "The Vertx instance with CDI qualifier @Identifier(\"{0}\") is not resolvable.", + cdiQualifier); + } + } + logger.log(Level.INFO, "Create a new Vertx instance"); + return Vertx.vertx(); + } + protected VertxGrpcSender createGrpcSender(ConfigProperties config, String grpcEndpointPath) throws URISyntaxException { return new VertxGrpcSender<>( new URI(getOtlpEndpoint(config, OTLP_GRPC_ENDPOINT)), @@ -64,7 +94,7 @@ protected VertxGrpcSender createGrpcSender(ConfigProperties config, String gr getCompression(config), getTimeout(config), OtlpExporterUtil.populateTracingExportHttpHeaders(), - Vertx.vertx()); + getVertx(config)); } protected VertxHttpSender createHttpSender(ConfigProperties config, String httpEndpointPath) throws URISyntaxException { @@ -75,7 +105,7 @@ protected VertxHttpSender createHttpSender(ConfigProperties config, String httpE getTimeout(config), OtlpExporterUtil.populateTracingExportHttpHeaders(), MIMETYPE_PROTOBUF, - Vertx.vertx()); + getVertx(config)); } protected IllegalArgumentException buildUnsupportedProtocolException(String protocol) { diff --git a/pom.xml b/pom.xml index fe4c6940..3028ce30 100644 --- a/pom.xml +++ b/pom.xml @@ -21,6 +21,7 @@ 2.0 3.1 2.6.2 + 2.5.0 6.2.10.Final 4.5.10 1.13.4 @@ -85,6 +86,11 @@ mutiny ${version.mutiny} + + io.smallrye.common + smallrye-common-annotation + ${version.smallrye.common} +