Skip to content

Commit

Permalink
Introduce a way to customize OTel TextMapPropagator
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Jul 24, 2023
1 parent d1cff95 commit a99cb86
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ AdditionalBeanBuildItem ensureProducerIsRetained() {
.addBeanClasses(OpenTelemetryProducer.class,
AutoConfiguredOpenTelemetrySdkBuilderCustomizer.ResourceCustomizer.class,
AutoConfiguredOpenTelemetrySdkBuilderCustomizer.SamplerCustomizer.class,
AutoConfiguredOpenTelemetrySdkBuilderCustomizer.TracerProviderCustomizer.class)
AutoConfiguredOpenTelemetrySdkBuilderCustomizer.TracerProviderCustomizer.class,
AutoConfiguredOpenTelemetrySdkBuilderCustomizer.TextMapPropagatorCustomizers.class)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package io.quarkus.opentelemetry.deployment;

import static org.assertj.core.api.Assertions.*;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.opentelemetry.api.baggage.propagation.W3CBaggagePropagator;
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.quarkus.opentelemetry.TextMapPropagatorCustomizer;
import io.quarkus.opentelemetry.deployment.common.TestSpanExporter;
import io.quarkus.opentelemetry.deployment.common.TestSpanExporterProvider;
import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;

public class OpenTelemetryTextMapPropagatorCustomizerTest {

@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest().setArchiveProducer(
() -> ShrinkWrap.create(JavaArchive.class)
.addClass(TestSpanExporter.class)
.addClass(TestSpanExporterProvider.class)
.addClass(TestTextMapPropagatorCustomizer.class)
.addAsResource("resource-config/application.properties", "application.properties")
.addAsResource(
"META-INF/services-config/io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSpanExporterProvider",
"META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSpanExporterProvider"));
@Inject
TestSpanExporter spanExporter;

@Test
void testSvcNameHasPriorityOverAppNameAndResourceAttr() {
RestAssured.when()
.get("/hello").then()
.statusCode(200)
.body(is("hello"));
RestAssured.when()
.get("/hello").then()
.statusCode(200)
.body(is("hello"));
RestAssured.when()
.get("/hello").then()
.statusCode(200)
.body(is("hello"));

List<SpanData> spans = spanExporter.getFinishedSpanItems(3);
final SpanData server = spans.get(0);
assertEquals("GET /hello", server.getName());

assertThat(TestTextMapPropagatorCustomizer.PROPAGATORS).containsOnly(W3CBaggagePropagator.class.getName(),
W3CTraceContextPropagator.class.getName());
}

@Path("/hello")
public static class HelloResource {
@GET
public String hello() {
return "hello";
}
}

@Singleton
public static class TestTextMapPropagatorCustomizer implements TextMapPropagatorCustomizer {

public static final Set<String> PROPAGATORS = ConcurrentHashMap.newKeySet();

@Override
public TextMapPropagator customize(Context context) {
TextMapPropagator propagator = context.propagator();
PROPAGATORS.add(propagator.getClass().getName());
return propagator;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.quarkus.opentelemetry;

import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;

/**
* /**
* Meant to be implemented by a CDI bean that provides arbitrary customization for the {@link TextMapPropagator}
* that are to be registered with OpenTelemetry
*/
public interface TextMapPropagatorCustomizer {

TextMapPropagator customize(Context context);

interface Context {
TextMapPropagator propagator();

ConfigProperties otelConfigProperties();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import jakarta.enterprise.inject.Instance;
import jakarta.inject.Singleton;

import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.resources.Resource;
Expand All @@ -19,6 +20,7 @@
import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.sdk.trace.samplers.Sampler;
import io.quarkus.arc.All;
import io.quarkus.opentelemetry.TextMapPropagatorCustomizer;
import io.quarkus.opentelemetry.runtime.config.build.OTelBuildConfig;
import io.quarkus.opentelemetry.runtime.config.runtime.OTelRuntimeConfig;
import io.quarkus.opentelemetry.runtime.exporter.otlp.RemoveableLateBoundBatchSpanProcessor;
Expand Down Expand Up @@ -164,4 +166,38 @@ public SdkTracerProviderBuilder apply(SdkTracerProviderBuilder builder,
});
}
}

@Singleton
final class TextMapPropagatorCustomizers implements AutoConfiguredOpenTelemetrySdkBuilderCustomizer {

private final List<TextMapPropagatorCustomizer> customizers;

public TextMapPropagatorCustomizers(@All List<TextMapPropagatorCustomizer> customizers) {
this.customizers = customizers;
}

@Override
public void customize(AutoConfiguredOpenTelemetrySdkBuilder builder) {
for (TextMapPropagatorCustomizer customizer : customizers) {
builder.addPropagatorCustomizer(
new BiFunction<>() {
@Override
public TextMapPropagator apply(TextMapPropagator textMapPropagator,
ConfigProperties configProperties) {
return customizer.customize(new TextMapPropagatorCustomizer.Context() {
@Override
public TextMapPropagator propagator() {
return textMapPropagator;
}

@Override
public ConfigProperties otelConfigProperties() {
return configProperties;
}
});
}
});
}
}
}
}

0 comments on commit a99cb86

Please sign in to comment.