diff --git a/sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/SpanDescriptionExtractor.java b/sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/SpanDescriptionExtractor.java index 3af3d8f96f0..af6d1b74e74 100644 --- a/sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/SpanDescriptionExtractor.java +++ b/sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/SpanDescriptionExtractor.java @@ -10,6 +10,7 @@ import io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes; import io.sentry.SentryOptions; import io.sentry.protocol.TransactionNameSource; +import io.sentry.util.UrlUtils; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -71,16 +72,14 @@ private OtelSpanInfo descriptionForHttpMethod( final @Nullable String httpTarget = attributes.get(HttpIncubatingAttributes.HTTP_TARGET); final @Nullable String httpRoute = attributes.get(HttpAttributes.HTTP_ROUTE); @Nullable String httpPath = httpRoute; - if (httpPath == null) { - httpPath = httpTarget; + if (httpPath == null && httpTarget != null) { + httpPath = UrlUtils.parse(httpTarget).getUrl(); } final @NotNull String op = opBuilder.toString(); final @Nullable String urlFull = attributes.get(UrlAttributes.URL_FULL); - if (urlFull != null) { - if (httpPath == null) { - httpPath = urlFull; - } + if (urlFull != null && httpPath == null) { + httpPath = UrlUtils.parse(urlFull).getUrl(); } final @Nullable String urlPath = attributes.get(UrlAttributes.URL_PATH); diff --git a/sentry-opentelemetry/sentry-opentelemetry-core/src/test/kotlin/SpanDescriptionExtractorTest.kt b/sentry-opentelemetry/sentry-opentelemetry-core/src/test/kotlin/SpanDescriptionExtractorTest.kt index a43afb849e6..5100ff715bb 100644 --- a/sentry-opentelemetry/sentry-opentelemetry-core/src/test/kotlin/SpanDescriptionExtractorTest.kt +++ b/sentry-opentelemetry/sentry-opentelemetry-core/src/test/kotlin/SpanDescriptionExtractorTest.kt @@ -113,7 +113,7 @@ class SpanDescriptionExtractorTest { val info = whenExtractingSpanInfo() assertEquals("http.server", info.op) - assertEquals("GET https://sentry.io/some/path?q=1#top", info.description) + assertEquals("GET https://sentry.io/some/path", info.description) assertEquals(TransactionNameSource.URL, info.transactionNameSource) } @@ -132,7 +132,7 @@ class SpanDescriptionExtractorTest { } @Test - fun `uses HTTP_TARGET for description`() { + fun `uses HTTP_ROUTE over HTTP_TARGET for description`() { givenSpanKind(SpanKind.SERVER) givenAttributes( mapOf( @@ -150,6 +150,23 @@ class SpanDescriptionExtractorTest { assertEquals(TransactionNameSource.ROUTE, info.transactionNameSource) } + @Test + fun `removes query and fragment from HTTP_TARGET description`() { + givenSpanKind(SpanKind.SERVER) + givenAttributes( + mapOf( + HttpAttributes.HTTP_REQUEST_METHOD to "GET", + HttpIncubatingAttributes.HTTP_TARGET to "/checkout?page=1&token=secret#details", + ) + ) + + val info = whenExtractingSpanInfo() + + assertEquals("http.server", info.op) + assertEquals("GET /checkout", info.description) + assertEquals(TransactionNameSource.URL, info.transactionNameSource) + } + @Test fun `uses span name as description fallback`() { givenSpanKind(SpanKind.SERVER)