forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce a way to customize OTel TextMapPropagator
Fixes: quarkusio#31656
- Loading branch information
Showing
4 changed files
with
147 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...ava/io/quarkus/opentelemetry/deployment/OpenTelemetryTextMapPropagatorCustomizerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...telemetry/runtime/src/main/java/io/quarkus/opentelemetry/TextMapPropagatorCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters