Skip to content

Commit

Permalink
Have a baggage propagation test
Browse files Browse the repository at this point in the history
  • Loading branch information
brunobat committed Jul 27, 2023
1 parent 340b9a8 commit 8dd7aab
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
import org.eclipse.microprofile.rest.client.inject.RestClient;

import io.opentelemetry.api.baggage.Baggage;
import io.opentelemetry.context.Scope;

@Path("")
@Produces(MediaType.APPLICATION_JSON)
public class SimpleResource {
Expand All @@ -22,6 +25,10 @@ public interface SimpleClient {
@Path("/")
@GET
TraceData slashPath();

@Path("/from-baggage")
@GET
TraceData fromBaggagePath();
}

@Inject
Expand All @@ -31,6 +38,9 @@ public interface SimpleClient {
@RestClient
SimpleClient simpleClient;

@Inject
Baggage baggage;

@GET
public TraceData noPath() {
TraceData data = new TraceData();
Expand All @@ -50,6 +60,25 @@ public TraceData slashPathClient() {
return simpleClient.slashPath();
}

@GET
@Path("/slashpath-baggage")
public TraceData slashPathBaggageClient() {
try (Scope scope = baggage.toBuilder()
.put("baggage-key", "baggage-value")
.build()
.makeCurrent()) {
return simpleClient.fromBaggagePath();
}
}

@GET
@Path("/from-baggage")
public TraceData fromBaggageValue() {
TraceData data = new TraceData();
data.message = baggage.getEntryValue("baggage-key");
return data;
}

@GET
@Path("/direct")
public TraceData directTrace() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,43 @@ void testSlashClientPath() {
assertEquals(clientServer.get("parentSpanId"), client.get("spanId"));
}

@Test
void testBaggagePath() {
given()
.contentType("application/json")
.when().get("/slashpath-baggage")
.then()
.statusCode(200)
.body("message", equalTo("baggage-value"));

await().atMost(5, SECONDS).until(() -> getSpans().size() == 3);
List<Map<String, Object>> spans = getSpans();
assertEquals(3, spans.size());
assertEquals(1, spans.stream().map(map -> map.get("traceId")).collect(toSet()).size());

Map<String, Object> server = getSpanByKindAndParentId(spans, SERVER, "0000000000000000");
assertEquals(SERVER.toString(), server.get("kind"));
verifyResource(server);
assertEquals("GET /slashpath-baggage", server.get("name"));
assertEquals(SpanId.getInvalid(), server.get("parent_spanId"));
assertEquals(TraceId.getInvalid(), server.get("parent_traceId"));
assertFalse((Boolean) server.get("parent_valid"));
assertFalse((Boolean) server.get("parent_remote"));

Map<String, Object> client = getSpanByKindAndParentId(spans, CLIENT, server.get("spanId"));
assertEquals(CLIENT.toString(), client.get("kind"));
assertEquals("GET", client.get("name"));
assertEquals("http://localhost:8081/from-baggage", client.get("attr_http.url"));
assertEquals("200", client.get("attr_http.status_code"));
assertEquals(client.get("parentSpanId"), server.get("spanId"));

Map<String, Object> clientServer = getSpanByKindAndParentId(spans, SERVER, client.get("spanId"));
assertEquals(SERVER.toString(), clientServer.get("kind"));
verifyResource(clientServer);
assertEquals("GET /from-baggage", clientServer.get("name"));
assertEquals(clientServer.get("parentSpanId"), client.get("spanId"));
}

@Test
void testChainedResourceTracing() {
given()
Expand Down

0 comments on commit 8dd7aab

Please sign in to comment.