Skip to content

Commit

Permalink
[Issue-380] [Enhancement] Tries to inject Vertx instance from CDI usi…
Browse files Browse the repository at this point in the history
…ng a qualifier (#381)

Signed-off-by: Lin Gao <[email protected]>
  • Loading branch information
gaol authored Oct 11, 2024
1 parent 418a806 commit 6e0e060
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
9 changes: 9 additions & 0 deletions implementation/exporters/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
<groupId>io.smallrye.reactive</groupId>
<artifactId>mutiny</artifactId>
</dependency>
<dependency>
<groupId>io.smallrye.common</groupId>
<artifactId>smallrye-common-annotation</artifactId>
</dependency>

<dependency>
<groupId>io.smallrye.opentelemetry</groupId>
Expand All @@ -38,6 +42,11 @@
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-otlp-common</artifactId>
</dependency>
<dependency>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
<scope>provided</scope>
</dependency>

<!-- Test Dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,6 +38,10 @@ public abstract class AbstractVertxExporterProvider<T extends Marshaler> {

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;

Expand All @@ -57,14 +67,34 @@ protected HttpExporter<T> 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<Vertx> 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<T> createGrpcSender(ConfigProperties config, String grpcEndpointPath) throws URISyntaxException {
return new VertxGrpcSender<>(
new URI(getOtlpEndpoint(config, OTLP_GRPC_ENDPOINT)),
grpcEndpointPath,
getCompression(config),
getTimeout(config),
OtlpExporterUtil.populateTracingExportHttpHeaders(),
Vertx.vertx());
getVertx(config));
}

protected VertxHttpSender createHttpSender(ConfigProperties config, String httpEndpointPath) throws URISyntaxException {
Expand All @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<version.microprofile.opentelemetry>2.0</version.microprofile.opentelemetry>
<version.microprofile.config>3.1</version.microprofile.config>
<version.mutiny>2.6.2</version.mutiny>
<version.smallrye.common>2.5.0</version.smallrye.common>
<version.resteasy>6.2.10.Final</version.resteasy>
<version.vertx.grpc>4.5.10</version.vertx.grpc>
<micrometer.version>1.13.4</micrometer.version>
Expand Down Expand Up @@ -85,6 +86,11 @@
<artifactId>mutiny</artifactId>
<version>${version.mutiny}</version>
</dependency>
<dependency>
<groupId>io.smallrye.common</groupId>
<artifactId>smallrye-common-annotation</artifactId>
<version>${version.smallrye.common}</version>
</dependency>

<!-- Micrometer Core and Registries, imported as BOM -->
<dependency>
Expand Down

0 comments on commit 6e0e060

Please sign in to comment.