From f5ba630628e962c252da7da9e4304d2416c00e90 Mon Sep 17 00:00:00 2001 From: brunobat Date: Wed, 20 Sep 2023 13:03:44 +0100 Subject: [PATCH] Improvement in the propagation test setup --- ...emetryTextMapPropagatorCustomizerTest.java | 66 +++++++++++-------- .../resource-config/application.properties | 2 + 2 files changed, 42 insertions(+), 26 deletions(-) diff --git a/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryTextMapPropagatorCustomizerTest.java b/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryTextMapPropagatorCustomizerTest.java index a10924e35c4a9..63236480a6356 100644 --- a/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryTextMapPropagatorCustomizerTest.java +++ b/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryTextMapPropagatorCustomizerTest.java @@ -1,7 +1,7 @@ package io.quarkus.opentelemetry.deployment; -import static org.assertj.core.api.Assertions.*; -import static org.hamcrest.Matchers.is; +import static io.quarkus.opentelemetry.deployment.common.TestSpanExporter.getSpanByKindAndParentId; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.List; @@ -12,13 +12,17 @@ import jakarta.inject.Singleton; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; +import jakarta.ws.rs.core.Response; +import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; +import org.eclipse.microprofile.rest.client.inject.RestClient; 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.SpanKind; import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; import io.opentelemetry.context.propagation.TextMapPropagator; import io.opentelemetry.sdk.trace.data.SpanData; @@ -26,7 +30,6 @@ 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 { @@ -43,27 +46,33 @@ public class OpenTelemetryTextMapPropagatorCustomizerTest { @Inject TestSpanExporter spanExporter; + @Inject + @RestClient + HelloRestClient client; + @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 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()); + void testPropagatorCustomizer_NoPropagation() { + Response response = client.hello(); + + List spans = spanExporter.getFinishedSpanItems(2); + SpanData clientSpan = getSpanByKindAndParentId(spans, SpanKind.CLIENT, "0000000000000000"); + assertEquals("GET", clientSpan.getName()); + + // There is a parent id, therefore propagation is working. + SpanData serverSpan = getSpanByKindAndParentId(spans, SpanKind.SERVER, clientSpan.getSpanId()); + assertEquals("GET /hello", serverSpan.getName()); + + assertThat(TestTextMapPropagatorCustomizer.PROPAGATORS_USED).containsOnly(W3CTraceContextPropagator.class.getName()); + + assertThat(TestTextMapPropagatorCustomizer.PROPAGATORS_DISCARDED).containsOnly(W3CBaggagePropagator.class.getName()); + + } + + @RegisterRestClient(configKey = "client") + @Path("/hello") + public interface HelloRestClient { + @GET + Response hello(); } @Path("/hello") @@ -76,13 +85,18 @@ public String hello() { @Singleton public static class TestTextMapPropagatorCustomizer implements TextMapPropagatorCustomizer { - - public static final Set PROPAGATORS = ConcurrentHashMap.newKeySet(); + // just to understand this was actually called. + public static final Set PROPAGATORS_USED = ConcurrentHashMap.newKeySet(); + public static final Set PROPAGATORS_DISCARDED = ConcurrentHashMap.newKeySet(); @Override public TextMapPropagator customize(Context context) { TextMapPropagator propagator = context.propagator(); - PROPAGATORS.add(propagator.getClass().getName()); + if (propagator instanceof W3CBaggagePropagator) { + PROPAGATORS_DISCARDED.add(propagator.getClass().getName()); + return TextMapPropagator.noop(); + } + PROPAGATORS_USED.add(propagator.getClass().getName()); return propagator; } } diff --git a/extensions/opentelemetry/deployment/src/test/resources/resource-config/application.properties b/extensions/opentelemetry/deployment/src/test/resources/resource-config/application.properties index 15f46b6b2a75a..a63d6ddd2f6b9 100644 --- a/extensions/opentelemetry/deployment/src/test/resources/resource-config/application.properties +++ b/extensions/opentelemetry/deployment/src/test/resources/resource-config/application.properties @@ -3,3 +3,5 @@ quarkus.otel.resource.attributes=service.name=authservice,service.instance.id=${ quarkus.otel.traces.exporter=test-span-exporter quarkus.otel.bsp.schedule.delay=50 + +quarkus.rest-client.client.url=${test.url}