Skip to content

Commit

Permalink
Properly construct instance of LateBoundBatchSpanProcessor
Browse files Browse the repository at this point in the history
This is now done entirely in a CDI producer method
which is then fully initialized when OpenTelemetry
autoconfigures itself

Fixes: quarkusio#29987
  • Loading branch information
geoand committed Jul 5, 2023
1 parent 66e1053 commit 2fc1b2a
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 318 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,11 @@
import java.util.function.BooleanSupplier;

import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.BeanContainerBuildItem;
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.OtlpRecorder;

@BuildSteps(onlyIf = OtlpExporterProcessor.OtlpExporterEnabled.class)
public class OtlpExporterProcessor {
Expand All @@ -39,17 +32,4 @@ AdditionalBeanBuildItem createBatchSpanProcessor() {
.addBeanClass(OtlpExporterProvider.class)
.setUnremovable().build();
}

@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
void installBatchSpanProcessorForOtlp(
OtlpRecorder recorder,
LaunchModeBuildItem launchModeBuildItem,
OTelRuntimeConfig otelRuntimeConfig,
OtlpExporterRuntimeConfig exporterRuntimeConfig,
BeanContainerBuildItem beanContainerBuildItem) {
recorder.installBatchSpanProcessorForOtlp(otelRuntimeConfig,
exporterRuntimeConfig,
launchModeBuildItem.getLaunchMode());
}
}

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
@@ -1,14 +1,113 @@
package io.quarkus.opentelemetry.runtime.exporter.otlp;

import static io.quarkus.opentelemetry.runtime.OpenTelemetryUtil.convertKeyValueListToMap;
import static io.quarkus.opentelemetry.runtime.config.runtime.exporter.OtlpExporterRuntimeConfig.DEFAULT_GRPC_BASE_URI;
import static io.quarkus.opentelemetry.runtime.config.runtime.exporter.OtlpExporterTracesConfig.Protocol.HTTP_PROTOBUF;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import jakarta.enterprise.inject.Instance;
import jakarta.enterprise.inject.Produces;
import jakarta.inject.Singleton;

import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter;
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporterBuilder;
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
import io.opentelemetry.sdk.trace.export.BatchSpanProcessorBuilder;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import io.quarkus.opentelemetry.runtime.config.runtime.OTelRuntimeConfig;
import io.quarkus.opentelemetry.runtime.config.runtime.exporter.CompressionType;
import io.quarkus.opentelemetry.runtime.config.runtime.exporter.OtlpExporterRuntimeConfig;

@Deprecated
@Singleton
public class OtlpExporterProvider {

@Produces
@Singleton
public LateBoundBatchSpanProcessor batchSpanProcessorForOtlp() {
return new LateBoundBatchSpanProcessor();
public LateBoundBatchSpanProcessor batchSpanProcessorForOtlp(OTelRuntimeConfig otelRuntimeConfig,
OtlpExporterRuntimeConfig exporterRuntimeConfig,
Instance<SpanExporter> spanExporters) {
var result = new LateBoundBatchSpanProcessor();

if (otelRuntimeConfig.sdkDisabled()) {
return result;
}

String endpoint = resolveEndpoint(exporterRuntimeConfig).trim();

// Only create the OtlpGrpcSpanExporter if an endpoint was set in runtime config
if (endpoint.length() > 0) {
try {
// CDI exporter was already added to a processor by OTEL
if (spanExporters.isUnsatisfied()) {
var spanExporter = createOtlpGrpcSpanExporter(exporterRuntimeConfig, endpoint);

BatchSpanProcessorBuilder processorBuilder = BatchSpanProcessor.builder(spanExporter);

processorBuilder.setScheduleDelay(otelRuntimeConfig.bsp().scheduleDelay());
processorBuilder.setMaxQueueSize(otelRuntimeConfig.bsp().maxQueueSize());
processorBuilder.setMaxExportBatchSize(otelRuntimeConfig.bsp().maxExportBatchSize());
processorBuilder.setExporterTimeout(otelRuntimeConfig.bsp().exportTimeout());
// processorBuilder.setMeterProvider() // TODO add meter provider to span processor.

result.setBatchSpanProcessorDelegate(processorBuilder.build());
}
} catch (IllegalArgumentException iae) {
throw new IllegalStateException("Unable to install OTLP Exporter", iae);
}
}

return result;
}

static String resolveEndpoint(final OtlpExporterRuntimeConfig runtimeConfig) {
String endpoint = runtimeConfig.traces().legacyEndpoint()
.filter(OtlpExporterProvider::excludeDefaultEndpoint)
.orElse(runtimeConfig.traces().endpoint()
.filter(OtlpExporterProvider::excludeDefaultEndpoint)
.orElse(runtimeConfig.endpoint()
.filter(OtlpExporterProvider::excludeDefaultEndpoint)
.orElse(DEFAULT_GRPC_BASE_URI)));
return endpoint.trim();
}

private static boolean excludeDefaultEndpoint(String endpoint) {
return !DEFAULT_GRPC_BASE_URI.equals(endpoint);
}

private OtlpGrpcSpanExporter createOtlpGrpcSpanExporter(OtlpExporterRuntimeConfig exporterRuntimeConfig, String endpoint) {
OtlpGrpcSpanExporterBuilder exporterBuilder = OtlpGrpcSpanExporter.builder()
.setEndpoint(endpoint)
.setTimeout(exporterRuntimeConfig.traces().timeout());

// FIXME TLS Support. Was not available before but will be available soon.
// exporterRuntimeConfig.traces.certificate.ifPresent(exporterBuilder::setTrustedCertificates);
// exporterRuntimeConfig.client.ifPresent(exporterBuilder::setClientTls);
exporterRuntimeConfig.traces().headers().ifPresent(new Consumer<List<String>>() {
@Override
public void accept(final List<String> headers) {
for (Map.Entry<String, String> entry : convertKeyValueListToMap(headers).entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
exporterBuilder.addHeader(key, value);
}
}
});
exporterRuntimeConfig.traces().compression()
.ifPresent(new Consumer<CompressionType>() {
@Override
public void accept(CompressionType compression) {
exporterBuilder.setCompression(compression.getValue());
}
});

if (!exporterRuntimeConfig.traces().protocol().orElse("").equals(HTTP_PROTOBUF)) {
throw new IllegalStateException("Only the GRPC Exporter is currently supported. " +
"Please check `otel.exporter.otlp.traces.protocol` property");
}
return exporterBuilder.build();
}
}
Loading

0 comments on commit 2fc1b2a

Please sign in to comment.