Skip to content

Commit

Permalink
Merge pull request #36036 from brunobat/otel-propagation-test-improve…
Browse files Browse the repository at this point in the history
…ment

Improvement in the propagation test setup
  • Loading branch information
geoand authored Sep 21, 2023
2 parents a984388 + f5ba630 commit e68d23e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,21 +12,24 @@
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;
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 {

Expand All @@ -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<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());
void testPropagatorCustomizer_NoPropagation() {
Response response = client.hello();

List<SpanData> 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")
Expand All @@ -76,13 +85,18 @@ public String hello() {

@Singleton
public static class TestTextMapPropagatorCustomizer implements TextMapPropagatorCustomizer {

public static final Set<String> PROPAGATORS = ConcurrentHashMap.newKeySet();
// just to understand this was actually called.
public static final Set<String> PROPAGATORS_USED = ConcurrentHashMap.newKeySet();
public static final Set<String> 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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}

0 comments on commit e68d23e

Please sign in to comment.