Skip to content

Commit

Permalink
feat(strawberry): Use operation name as transaction name (#3294)
Browse files Browse the repository at this point in the history
The Strawberry integration is creating spans at the moment, but they're all grouped under the same /graphql transaction coming from the web framework integration. This has significant effect on the usefulness of tracing.

With this change we start using the operation name to update the name of the transaction so that each unique operation becomes its own event group.
  • Loading branch information
sentrivana authored Jul 16, 2024
1 parent a9eed79 commit cf8e37f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
13 changes: 10 additions & 3 deletions sentry_sdk/integrations/strawberry.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from sentry_sdk.integrations import Integration, DidNotEnable
from sentry_sdk.integrations.logging import ignore_logger
from sentry_sdk.scope import Scope, should_send_default_pii
from sentry_sdk.tracing import TRANSACTION_SOURCE_COMPONENT
from sentry_sdk.utils import (
capture_internal_exceptions,
ensure_integration_enabled,
Expand Down Expand Up @@ -176,9 +177,9 @@ def on_operation(self):
},
)

scope = Scope.get_isolation_scope()
if scope.span:
self.graphql_span = scope.span.start_child(
span = sentry_sdk.get_current_span()
if span:
self.graphql_span = span.start_child(
op=op,
description=description,
origin=StrawberryIntegration.origin,
Expand All @@ -197,6 +198,12 @@ def on_operation(self):

yield

transaction = self.graphql_span.containing_transaction
if transaction and self.execution_context.operation_name:
transaction.name = self.execution_context.operation_name
transaction.source = TRANSACTION_SOURCE_COMPONENT
transaction.op = op

self.graphql_span.finish()

def on_validate(self):
Expand Down
21 changes: 6 additions & 15 deletions tests/integrations/strawberry/test_strawberry.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,8 @@ def test_capture_transaction_on_error(
assert len(events) == 2
(_, transaction_event) = events

if async_execution:
assert transaction_event["transaction"] == "/graphql"
else:
assert transaction_event["transaction"] == "graphql_view"

assert transaction_event["transaction"] == "ErrorQuery"
assert transaction_event["contexts"]["trace"]["op"] == OP.GRAPHQL_QUERY
assert transaction_event["spans"]

query_spans = [
Expand Down Expand Up @@ -404,11 +401,8 @@ def test_capture_transaction_on_success(
assert len(events) == 1
(transaction_event,) = events

if async_execution:
assert transaction_event["transaction"] == "/graphql"
else:
assert transaction_event["transaction"] == "graphql_view"

assert transaction_event["transaction"] == "GreetingQuery"
assert transaction_event["contexts"]["trace"]["op"] == OP.GRAPHQL_QUERY
assert transaction_event["spans"]

query_spans = [
Expand Down Expand Up @@ -564,11 +558,8 @@ def test_transaction_mutation(
assert len(events) == 1
(transaction_event,) = events

if async_execution:
assert transaction_event["transaction"] == "/graphql"
else:
assert transaction_event["transaction"] == "graphql_view"

assert transaction_event["transaction"] == "Change"
assert transaction_event["contexts"]["trace"]["op"] == OP.GRAPHQL_MUTATION
assert transaction_event["spans"]

query_spans = [
Expand Down

0 comments on commit cf8e37f

Please sign in to comment.