Skip to content

Commit

Permalink
test for WithSpan on a rest client
Browse files Browse the repository at this point in the history
  • Loading branch information
brunobat committed Sep 25, 2023
1 parent e653e8f commit 458933e
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
import org.eclipse.microprofile.rest.client.inject.RestClient;

import io.opentelemetry.instrumentation.annotations.SpanAttribute;
import io.opentelemetry.instrumentation.annotations.WithSpan;
import io.smallrye.common.annotation.Blocking;
import io.smallrye.mutiny.Uni;
import io.vertx.core.MultiMap;
Expand All @@ -34,6 +36,11 @@ public interface PingPongRestClient {
@GET
@Path("/client/pong/{message}")
Uni<String> asyncPingpong(@PathParam("message") String message);

@GET
@Path("/client/pong/{message}")
@WithSpan
String pingpongIntercept(@SpanAttribute(value = "message") @PathParam("message") String message);
}

@Inject
Expand Down Expand Up @@ -81,4 +88,9 @@ public Uni<String> asyncPingNamed(@PathParam("message") String message) {
.onItemOrFailure().call(httpClient::close);
}

@GET
@Path("pong-intercept/{message}")
public String pongIntercept(@PathParam("message") String message) {
return pingRestClient.pingpongIntercept(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

Expand Down Expand Up @@ -557,6 +558,79 @@ void testAsyncClientTracing() {
assertNotNull(clientServer.get("attr_user_agent.original"));
}

@Test
void testClientTracingWithInterceptor() {
given()
.when().get("/client/pong-intercept/one")
.then()
.statusCode(200)
.body(containsString("one"));

await().atMost(5, SECONDS).until(() -> getSpans().size() == 4);
List<Map<String, Object>> spans = getSpans();
assertEquals(4, 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 /client/pong-intercept/{message}", server.get("name"));
assertEquals(SERVER.toString(), server.get("kind"));
assertTrue((Boolean) server.get("ended"));
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"));
assertEquals("GET", server.get("attr_http.method"));
assertEquals("/client/pong-intercept/one", server.get("attr_http.target"));
assertEquals(pathParamUrl.getHost(), server.get("attr_net.host.name"));
assertEquals(pathParamUrl.getPort(), Integer.valueOf((String) server.get("attr_net.host.port")));
assertEquals("http", server.get("attr_http.scheme"));
assertEquals("/client/pong-intercept/{message}", server.get("attr_http.route"));
assertEquals("200", server.get("attr_http.status_code"));
assertNotNull(server.get("attr_http.client_ip"));
assertNotNull(server.get("attr_user_agent.original"));

Map<String, Object> fromInterceptor = getSpanByKindAndParentId(spans, INTERNAL, server.get("spanId"));
assertEquals("PingPongRestClient.pingpongIntercept", fromInterceptor.get("name"));
assertEquals(INTERNAL.toString(), fromInterceptor.get("kind"));
assertTrue((Boolean) fromInterceptor.get("ended"));
assertTrue((Boolean) fromInterceptor.get("parent_valid"));
assertFalse((Boolean) fromInterceptor.get("parent_remote"));
assertNull(fromInterceptor.get("attr_http.method"));
assertNull(fromInterceptor.get("attr_http.status_code"));
assertEquals("one", fromInterceptor.get("attr_message"));

Map<String, Object> client = getSpanByKindAndParentId(spans, CLIENT, fromInterceptor.get("spanId"));
assertEquals("GET", client.get("name"));
assertEquals(SpanKind.CLIENT.toString(), client.get("kind"));
assertTrue((Boolean) client.get("ended"));
assertTrue((Boolean) client.get("parent_valid"));
assertFalse((Boolean) client.get("parent_remote"));
assertEquals("GET", client.get("attr_http.method"));
assertEquals("http://localhost:8081/client/pong/one", client.get("attr_http.url"));
assertEquals("200", client.get("attr_http.status_code"));

Map<String, Object> clientServer = getSpanByKindAndParentId(spans, SERVER, client.get("spanId"));
assertEquals(SERVER.toString(), clientServer.get("kind"));
verifyResource(clientServer);
assertEquals("GET /client/pong/{message}", clientServer.get("name"));
assertEquals(SERVER.toString(), clientServer.get("kind"));
assertTrue((Boolean) clientServer.get("ended"));
assertTrue((Boolean) clientServer.get("parent_valid"));
assertTrue((Boolean) clientServer.get("parent_remote"));
assertEquals("GET", clientServer.get("attr_http.method"));
assertEquals("/client/pong/one", clientServer.get("attr_http.target"));
assertEquals(pathParamUrl.getHost(), server.get("attr_net.host.name"));
assertEquals(pathParamUrl.getPort(), Integer.valueOf((String) server.get("attr_net.host.port")));
assertEquals("http", clientServer.get("attr_http.scheme"));
assertEquals("/client/pong/{message}", clientServer.get("attr_http.route"));
assertEquals("200", clientServer.get("attr_http.status_code"));
assertNotNull(clientServer.get("attr_http.client_ip"));
assertNotNull(clientServer.get("attr_user_agent.original"));
assertEquals(clientServer.get("parentSpanId"), client.get("spanId"));
}

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

0 comments on commit 458933e

Please sign in to comment.