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: #34778
  • Loading branch information
geoand committed Jul 20, 2023
1 parent 35c1ca2 commit 6fb25f3
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
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,96 @@
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 io.quarkus.opentelemetry.deployment.common.TestUtil;
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.test.QuarkusUnitTest;
import io.quarkus.vertx.web.Route;
import io.restassured.RestAssured;
import io.smallrye.config.SmallRyeConfig;
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

0 comments on commit 6fb25f3

Please sign in to comment.