Skip to content

Commit

Permalink
Fix http.route tracing attribute reporting
Browse files Browse the repository at this point in the history
We know properly report the root path
whereas previously it was being ignored

Fixes: quarkusio#34778
(cherry picked from commit aa2a35a)
  • Loading branch information
geoand authored and gsmet committed Jul 28, 2023
1 parent 70cbe8a commit ac4729d
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 6 deletions.
5 changes: 5 additions & 0 deletions extensions/opentelemetry/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@
<artifactId>vertx-web-client</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-reactive-routes-deployment</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-deployment</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package io.quarkus.opentelemetry.deployment;

import static io.opentelemetry.api.trace.SpanKind.SERVER;
import static io.quarkus.opentelemetry.deployment.common.TestSpanExporter.getSpanByKindAndParentId;
import static io.quarkus.opentelemetry.deployment.common.TestUtil.assertStringAttribute;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import io.quarkus.opentelemetry.deployment.common.TestSpanExporter;
import io.quarkus.opentelemetry.deployment.common.TestSpanExporterProvider;
import io.quarkus.opentelemetry.deployment.common.TestUtil;
import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.vertx.web.Route;
import io.restassured.RestAssured;
import io.vertx.ext.web.RoutingContext;

public class OpenTelemetryReactiveRoutesTest {
@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest().setArchiveProducer(
() -> ShrinkWrap.create(JavaArchive.class)
.addClass(TestUtil.class)
.addClass(TestSpanExporter.class)
.addClass(TestSpanExporterProvider.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;

@AfterEach
void tearDown() {
spanExporter.reset();
}

@Test
void root() {
RestAssured.when()
.get("/").then()
.statusCode(200)
.body(is("hello"));

List<SpanData> spans = spanExporter.getFinishedSpanItems(1);

final SpanData server = getSpanByKindAndParentId(spans, SERVER, "0000000000000000");
assertEquals("GET /", server.getName());
assertStringAttribute(server, SemanticAttributes.HTTP_ROUTE, "/");
}

@Test
void nonRoot() {
RestAssured.when()
.get("/hello").then()
.statusCode(200)
.body(is("hello world"));

List<SpanData> spans = spanExporter.getFinishedSpanItems(1);

final SpanData server = getSpanByKindAndParentId(spans, SERVER, "0000000000000000");
assertEquals("GET /hello", server.getName());
assertStringAttribute(server, SemanticAttributes.HTTP_ROUTE, "/hello");
}

@ApplicationScoped
public static class Routes {

@Route(path = "/", methods = Route.HttpMethod.GET)
public void root(RoutingContext rc) {
rc.response().end("hello");
}

@Route(path = "/hello", methods = Route.HttpMethod.GET)
public void hello(RoutingContext rc) {
String name = rc.request().getParam("name");
if (name == null) {
name = "world";
}
rc.response().end("hello " + name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public String get(final io.opentelemetry.context.Context context, final HttpRequ
route = requestSpan.getContext().getLocal("VertxRoute");
}

if (route != null && route.length() > 1) {
if (route != null && route.length() >= 1) {
return route;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
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 @@ -153,7 +152,7 @@ void testEmptyClientPath() {
Map<String, Object> clientServer = getSpanByKindAndParentId(spans, SERVER, client.get("spanId"));
assertEquals(SERVER.toString(), clientServer.get("kind"));
verifyResource(clientServer);
assertEquals("GET", clientServer.get("name"));
assertEquals("GET /", clientServer.get("name"));
assertEquals(SERVER.toString(), clientServer.get("kind"));
assertTrue((Boolean) clientServer.get("ended"));
assertTrue((Boolean) clientServer.get("parent_valid"));
Expand All @@ -163,7 +162,7 @@ void testEmptyClientPath() {
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"));
assertNull(clientServer.get("attr_http.route"));
assertEquals("/", 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"));
Expand Down Expand Up @@ -219,7 +218,7 @@ void testSlashClientPath() {
Map<String, Object> clientServer = getSpanByKindAndParentId(spans, SERVER, client.get("spanId"));
assertEquals(SERVER.toString(), clientServer.get("kind"));
verifyResource(clientServer);
assertEquals("GET", clientServer.get("name"));
assertEquals("GET /", clientServer.get("name"));
assertEquals(SERVER.toString(), clientServer.get("kind"));
assertTrue((Boolean) clientServer.get("ended"));
assertTrue((Boolean) clientServer.get("parent_valid"));
Expand All @@ -229,7 +228,7 @@ void testSlashClientPath() {
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"));
assertNull(clientServer.get("attr_http.route"));
assertEquals("/", 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"));
Expand Down

0 comments on commit ac4729d

Please sign in to comment.