Skip to content

Commit

Permalink
Fix instantiation of the exporters for Quarkus v3.2+
Browse files Browse the repository at this point in the history
  • Loading branch information
brunobat committed Oct 16, 2023
1 parent 8231c45 commit 7374302
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 127 deletions.
2 changes: 1 addition & 1 deletion docs/modules/ROOT/pages/includes/attributes.adoc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
:project-version: 1.1.1.Final
:project-version: 2.0.0.Final

:examples-dir: ./../examples/
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<quarkus.version>3.0.0.CR2</quarkus.version>
<opentelemetry.version>1.23.1</opentelemetry.version>
<opentelemetry-alpha.version>1.23.0-alpha</opentelemetry-alpha.version>
<quarkus.version>3.5.1</quarkus.version>
<opentelemetry.version>1.30.1</opentelemetry.version>
<opentelemetry-alpha.version>1.30.0-alpha</opentelemetry-alpha.version>

<gcp-opentelemetry.version>0.23.0</gcp-opentelemetry.version>
<gax-grpc.version>2.13.0</gax-grpc.version>
<gcp-opentelemetry.version>0.25.2</gcp-opentelemetry.version>
<gax-grpc.version>2.25.0</gax-grpc.version>

<assertj-core.version>3.24.2</assertj-core.version>
<junit-jupiter.version>1.18.0</junit-jupiter.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ public class LateBoundSpanProcessor implements SpanProcessor {
private boolean warningLogged = false;
private SpanProcessor delegate;

public LateBoundSpanProcessor() {
}

public LateBoundSpanProcessor(SpanProcessor delegate) {
this.delegate = delegate;
}

// fixme remove this method
/**
* Set the actual {@link SpanProcessor} to use as the delegate.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,26 @@

import java.util.function.BooleanSupplier;

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.quarkiverse.opentelemetry.exporter.common.runtime.LateBoundSpanProcessor;
import io.quarkiverse.opentelemetry.exporter.gcp.runtime.GcpExporterConfig;
import io.quarkiverse.opentelemetry.exporter.gcp.runtime.GcpExporterProvider;
import io.quarkiverse.opentelemetry.exporter.gcp.runtime.GcpRecorder;
import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.SyntheticBeanBuildItem;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.BuildSteps;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.LaunchModeBuildItem;
import io.quarkus.opentelemetry.deployment.exporter.otlp.ExternalOtelExporterBuildItem;

@BuildSteps(onlyIf = GcpExporterProcessor.GcpExporterEnabled.class)
public class GcpExporterProcessor {
Expand All @@ -25,17 +37,24 @@ public boolean getAsBoolean() {
}

@BuildStep
AdditionalBeanBuildItem createBatchSpanProcessor() {
return AdditionalBeanBuildItem.builder()
.addBeanClass(GcpExporterProvider.class)
.setUnremovable().build();
void registerExternalExporter(BuildProducer<ExternalOtelExporterBuildItem> buildProducer) {
buildProducer.produce(new ExternalOtelExporterBuildItem("gcp"));
}

@BuildStep
@Record(RUNTIME_INIT)
void installBatchSpanProcessorForGcp(GcpRecorder recorder,
SyntheticBeanBuildItem installBatchSpanProcessorForGcp(GcpRecorder recorder,
LaunchModeBuildItem launchModeBuildItem,
GcpExporterConfig.GcpExporterRuntimeConfig runtimeConfig) {
recorder.installSpanProcessorForGcp(runtimeConfig, launchModeBuildItem.getLaunchMode());

return SyntheticBeanBuildItem.configure(LateBoundSpanProcessor.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.installSpanProcessorForGcp(runtimeConfig, launchModeBuildItem.getLaunchMode()))
.done();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package io.quarkiverse.opentelemetry.exporter.gcp.runtime;

import java.io.IOException;

import jakarta.enterprise.inject.Any;
import jakarta.enterprise.inject.spi.CDI;
import java.util.function.Function;

import com.google.cloud.opentelemetry.trace.TestTraceConfigurationBuilder;
import com.google.cloud.opentelemetry.trace.TraceConfiguration;
Expand All @@ -13,40 +11,51 @@
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
import io.quarkiverse.opentelemetry.exporter.common.runtime.LateBoundSpanProcessor;
import io.quarkus.arc.SyntheticCreationalContext;
import io.quarkus.runtime.LaunchMode;
import io.quarkus.runtime.annotations.Recorder;

@Recorder
public class GcpRecorder {
public void installSpanProcessorForGcp(GcpExporterConfig.GcpExporterRuntimeConfig runtimeConfig, LaunchMode launchMode) {
if (launchMode != LaunchMode.TEST && runtimeConfig.endpoint.isEmpty()) {
try {
configureTraceExporter(runtimeConfig);
} catch (IOException e) {
throw new RuntimeException("Unable to initialize GCP TraceExporter.", e);
}
} else {
TraceConfiguration.Builder builder = TestTraceConfigurationBuilder.buildTestTraceConfiguration();

if (runtimeConfig.endpoint.isPresent() && runtimeConfig.endpoint.get().trim().length() > 0) {
builder.setTraceServiceEndpoint(runtimeConfig.endpoint.get());
}

TraceConfiguration config = builder.build();
try {
if (runtimeConfig.cloudrun) {
configureSimpleSpanExporter(config);
public Function<SyntheticCreationalContext<LateBoundSpanProcessor>, LateBoundSpanProcessor> installSpanProcessorForGcp(
GcpExporterConfig.GcpExporterRuntimeConfig runtimeConfig,
LaunchMode launchMode) {
return new Function<>() {
@Override
public LateBoundSpanProcessor apply(
SyntheticCreationalContext<LateBoundSpanProcessor> lateBoundSpanProcessorSyntheticCreationalContext) {

if (launchMode != LaunchMode.TEST && runtimeConfig.endpoint.isEmpty()) {
try {
return configureTraceExporter(runtimeConfig);
} catch (IOException e) {
throw new RuntimeException("Unable to initialize GCP TraceExporter.", e);
}
} else {
configureBatchSpanExporter(config);
TraceConfiguration.Builder builder = TestTraceConfigurationBuilder.buildTestTraceConfiguration();

if (runtimeConfig.endpoint.isPresent() && runtimeConfig.endpoint.get().trim().length() > 0) {
builder.setTraceServiceEndpoint(runtimeConfig.endpoint.get());
}

TraceConfiguration config = builder.build();
try {
if (runtimeConfig.cloudrun) {
return configureSimpleSpanExporter(config);
} else {
return configureBatchSpanExporter(config);
}
} catch (IOException e) {
throw new RuntimeException("Unable to initialize GCP TraceExporter.", e);
}
}
} catch (IOException e) {
throw new RuntimeException("Unable to initialize GCP TraceExporter.", e);
}
}

};
}

private void configureTraceExporter(GcpExporterConfig.GcpExporterRuntimeConfig runtimeConfig) throws IOException {
private LateBoundSpanProcessor configureTraceExporter(GcpExporterConfig.GcpExporterRuntimeConfig runtimeConfig)
throws IOException {
TraceConfiguration.Builder builder = TraceConfiguration.builder();

if (runtimeConfig.projectid.isPresent() && runtimeConfig.projectid.get().trim().length() > 0) {
Expand All @@ -56,29 +65,21 @@ private void configureTraceExporter(GcpExporterConfig.GcpExporterRuntimeConfig r
TraceConfiguration traceConfig = builder.build();
// Initialize GCP TraceExporter default configuration
if (runtimeConfig.cloudrun) {
configureSimpleSpanExporter(traceConfig);
return configureSimpleSpanExporter(traceConfig);
} else {
configureBatchSpanExporter(traceConfig);
return configureBatchSpanExporter(traceConfig);
}
}

private void configureBatchSpanExporter(TraceConfiguration config) throws IOException {
BatchSpanProcessor batchSpanProcessor = BatchSpanProcessor.builder(TraceExporter.createWithConfiguration(config))
private LateBoundSpanProcessor configureBatchSpanExporter(TraceConfiguration config) throws IOException {
BatchSpanProcessor batchSpanProcessor = BatchSpanProcessor
.builder(TraceExporter.createWithConfiguration(config))
.build();

LateBoundSpanProcessor delayedProcessor = CDI.current()
.select(LateBoundSpanProcessor.class, Any.Literal.INSTANCE).get();

delayedProcessor.setSpanProcessorDelegate(batchSpanProcessor);
return new LateBoundSpanProcessor(batchSpanProcessor);
}

private void configureSimpleSpanExporter(TraceConfiguration config) throws IOException {
TraceExporter traceExporter = TraceExporter.createWithConfiguration(config);
SpanProcessor spanProcessor = SimpleSpanProcessor.create(traceExporter);

LateBoundSpanProcessor delayedProcessor = CDI.current()
.select(LateBoundSpanProcessor.class, Any.Literal.INSTANCE).get();

delayedProcessor.setSpanProcessorDelegate(spanProcessor);
private LateBoundSpanProcessor configureSimpleSpanExporter(TraceConfiguration config) throws IOException {
SpanProcessor spanProcessor = SimpleSpanProcessor.create(TraceExporter.createWithConfiguration(config));
return new LateBoundSpanProcessor(spanProcessor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,27 @@

import java.util.function.BooleanSupplier;

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.quarkiverse.opentelemetry.exporter.common.runtime.LateBoundSpanProcessor;
import io.quarkiverse.opentelemetry.exporter.jaeger.runtime.JaegerExporterConfig;
import io.quarkiverse.opentelemetry.exporter.jaeger.runtime.JaegerExporterProvider;
import io.quarkiverse.opentelemetry.exporter.jaeger.runtime.JaegerRecorder;
import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.SyntheticBeanBuildItem;
import io.quarkus.deployment.annotations.BuildProducer;
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.deployment.exporter.otlp.ExternalOtelExporterBuildItem;

@BuildSteps(onlyIf = JaegerExporterProcessor.JaegerExporterEnabled.class)
public class JaegerExporterProcessor {
Expand All @@ -24,17 +36,23 @@ public boolean getAsBoolean() {
}

@BuildStep
AdditionalBeanBuildItem createBatchSpanProcessor() {
return AdditionalBeanBuildItem.builder()
.addBeanClass(JaegerExporterProvider.class)
.setUnremovable().build();
void registerExternalExporter(BuildProducer<ExternalOtelExporterBuildItem> buildProducer) {
buildProducer.produce(new ExternalOtelExporterBuildItem("jaeger"));
}

@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
void installBatchSpanProcessorForJaeger(JaegerRecorder recorder,
SyntheticBeanBuildItem installBatchSpanProcessorForJaeger(JaegerRecorder recorder,
LaunchModeBuildItem launchModeBuildItem,
JaegerExporterConfig.JaegerExporterRuntimeConfig runtimeConfig) {
recorder.installBatchSpanProcessorForJaeger(runtimeConfig, launchModeBuildItem.getLaunchMode());
return SyntheticBeanBuildItem.configure(LateBoundSpanProcessor.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.createBatchSpanProcessorForJaeger(runtimeConfig))
.done();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class JaegerExporterBadEndpointTest {
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withEmptyApplication()
.overrideConfigKey("quarkus.opentelemetry.tracer.exporter.jaeger.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,8 @@
import io.quarkus.runtime.configuration.TrimmedStringConverter;

public class JaegerExporterConfig {
static final String DEFAULT_JAEGER_BASE_URI = "http://localhost:14250";

@ConfigRoot(name = "opentelemetry.tracer.exporter.jaeger", phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED)
public static class JaegerExporterBuildConfig {
/**
Expand All @@ -26,7 +28,7 @@ public static class JaegerExporterRuntimeConfig {
/**
* The Jaeger endpoint to connect to. The endpoint must start with either http:// or https://.
*/
@ConfigItem
@ConfigItem(defaultValue = DEFAULT_JAEGER_BASE_URI)
@ConvertWith(TrimmedStringConverter.class)
public Optional<String> endpoint;

Expand Down

This file was deleted.

Loading

0 comments on commit 7374302

Please sign in to comment.