diff --git a/CHANGELOG.md b/CHANGELOG.md index 6753c06d4bd..ed9c2c6f3f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -131,6 +131,7 @@ ### Fixes +- OpenTelemetry: Align GraphQL span operations and descriptions with Sentry instrumentation by grouping operation spans under `graphql` while retaining names such as `query GreetingQuery` as descriptions ([#6764](https://github.com/getsentry/sentry-java/pull/6764)) - Set Apollo 3, 4, and 5 HTTP spans to `CANCELLED` instead of `INTERNAL_ERROR` when the request is cancelled ([#6284](https://github.com/getsentry/sentry-java/pull/6284)) - Preserve independent Netty read and write timeout events when exception singletons are reused. These exceptions now bypass event deduplication by default, so applications with multiple capture integrations may report the same timeout more than once. Use `SentryOptions.setIgnoredErrorsForDeduplication(...)` or `ignored-errors-for-deduplication` to customize or clear the default exclusions. ([#6465](https://github.com/getsentry/sentry-java/pull/6465)) - Prevent changes to reused tracing options from affecting spans and transactions after creation ([#6417](https://github.com/getsentry/sentry-java/pull/6417)) 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 6326dd2848a..7036c73d14b 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 @@ -6,6 +6,7 @@ import io.opentelemetry.semconv.HttpAttributes; import io.opentelemetry.semconv.UrlAttributes; import io.opentelemetry.semconv.incubating.DbIncubatingAttributes; +import io.opentelemetry.semconv.incubating.GraphqlIncubatingAttributes; import io.opentelemetry.semconv.incubating.HttpIncubatingAttributes; import io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes; import io.sentry.SentryOptions; @@ -28,6 +29,12 @@ public final class SpanDescriptionExtractor { return descriptionForMessagingSystem(otelSpan); } + final @Nullable String graphqlOperationType = + attributes.get(GraphqlIncubatingAttributes.GRAPHQL_OPERATION_TYPE); + if (graphqlOperationType != null) { + return descriptionForGraphqlOperation(otelSpan); + } + final @Nullable String httpMethod = attributes.get(HttpAttributes.HTTP_REQUEST_METHOD); if (httpMethod != null) { return descriptionForHttpMethod(otelSpan, httpMethod); @@ -45,6 +52,10 @@ public final class SpanDescriptionExtractor { return new OtelSpanInfo(name, description, TransactionNameSource.CUSTOM); } + private OtelSpanInfo descriptionForGraphqlOperation(final @NotNull SpanData otelSpan) { + return new OtelSpanInfo("graphql", otelSpan.getName(), TransactionNameSource.TASK); + } + @SuppressWarnings("deprecation") private OtelSpanInfo descriptionForHttpMethod( final @NotNull SpanData otelSpan, final @NotNull String httpMethod) { 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 db9a2d517c5..90926440ebc 100644 --- a/sentry-opentelemetry/sentry-opentelemetry-core/src/test/kotlin/SpanDescriptionExtractorTest.kt +++ b/sentry-opentelemetry/sentry-opentelemetry-core/src/test/kotlin/SpanDescriptionExtractorTest.kt @@ -10,6 +10,7 @@ import io.opentelemetry.sdk.trace.data.SpanData import io.opentelemetry.semconv.HttpAttributes import io.opentelemetry.semconv.UrlAttributes import io.opentelemetry.semconv.incubating.DbIncubatingAttributes +import io.opentelemetry.semconv.incubating.GraphqlIncubatingAttributes import io.opentelemetry.semconv.incubating.HttpIncubatingAttributes import io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes import io.sentry.SentryOptions @@ -41,6 +42,48 @@ class SpanDescriptionExtractorTest { private val fixture = Fixture() + @Test + fun `sets op to graphql and preserves query span name as description`() { + givenSpanName("query GreetingQuery") + givenAttributes(mapOf(GraphqlIncubatingAttributes.GRAPHQL_OPERATION_TYPE to "query")) + + val info = whenExtractingSpanInfo() + + assertEquals("graphql", info.op) + assertEquals("query GreetingQuery", info.description) + assertEquals(TransactionNameSource.TASK, info.transactionNameSource) + } + + @Test + fun `sets op to graphql and preserves mutation span name as description`() { + givenSpanName("mutation AddProjectMutation") + givenAttributes(mapOf(GraphqlIncubatingAttributes.GRAPHQL_OPERATION_TYPE to "mutation")) + + val info = whenExtractingSpanInfo() + + assertEquals("graphql", info.op) + assertEquals("mutation AddProjectMutation", info.description) + assertEquals(TransactionNameSource.TASK, info.transactionNameSource) + } + + @Test + fun `graphql mapping wins over http when both attributes present`() { + givenSpanKind(SpanKind.CLIENT) + givenSpanName("query GreetingQuery") + givenAttributes( + mapOf( + GraphqlIncubatingAttributes.GRAPHQL_OPERATION_TYPE to "query", + HttpAttributes.HTTP_REQUEST_METHOD to "POST", + ), + ) + + val info = whenExtractingSpanInfo() + + assertEquals("graphql", info.op) + assertEquals("query GreetingQuery", info.description) + assertEquals(TransactionNameSource.TASK, info.transactionNameSource) + } + @Test fun `sets op to http server for kind SERVER`() { givenSpanKind(SpanKind.SERVER) diff --git a/sentry-spring-boot/src/integrationTestOtelAgent/kotlin/io/sentry/spring/boot/otelagentintegrationtest/GraphqlIntegrationTest.kt b/sentry-spring-boot/src/integrationTestOtelAgent/kotlin/io/sentry/spring/boot/otelagentintegrationtest/GraphqlIntegrationTest.kt index e881a8af4d0..b47a689e4f1 100644 --- a/sentry-spring-boot/src/integrationTestOtelAgent/kotlin/io/sentry/spring/boot/otelagentintegrationtest/GraphqlIntegrationTest.kt +++ b/sentry-spring-boot/src/integrationTestOtelAgent/kotlin/io/sentry/spring/boot/otelagentintegrationtest/GraphqlIntegrationTest.kt @@ -9,8 +9,8 @@ import io.sentry.test.transaction import kotlin.test.Test /** - * The agent traces each GraphQL operation as one span, with the operation's name as its op and - * description, so the queries are named. + * The agent traces each GraphQL operation as one span, using the GraphQL operation name as its + * description. */ class GraphqlIntegrationTest : IntegrationTest() { @@ -97,7 +97,7 @@ class GraphqlIntegrationTest : IntegrationTest() { .transaction()!! private fun SentryTransaction.hasOperationSpan(operation: String): Boolean = - hasSpanWithOpAndDescription(operation, operation) + hasSpanWithOpAndDescription("graphql", operation) /** Finds the error for [exceptionMessage], then the transaction it belongs to by trace ID. */ private fun awaitTransactionOfError(exceptionMessage: String): SentryTransaction {