Skip to content

Commit cf8e37f

Browse files
authored
feat(strawberry): Use operation name as transaction name (#3294)
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.
1 parent a9eed79 commit cf8e37f

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

sentry_sdk/integrations/strawberry.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from sentry_sdk.integrations import Integration, DidNotEnable
77
from sentry_sdk.integrations.logging import ignore_logger
88
from sentry_sdk.scope import Scope, should_send_default_pii
9+
from sentry_sdk.tracing import TRANSACTION_SOURCE_COMPONENT
910
from sentry_sdk.utils import (
1011
capture_internal_exceptions,
1112
ensure_integration_enabled,
@@ -176,9 +177,9 @@ def on_operation(self):
176177
},
177178
)
178179

179-
scope = Scope.get_isolation_scope()
180-
if scope.span:
181-
self.graphql_span = scope.span.start_child(
180+
span = sentry_sdk.get_current_span()
181+
if span:
182+
self.graphql_span = span.start_child(
182183
op=op,
183184
description=description,
184185
origin=StrawberryIntegration.origin,
@@ -197,6 +198,12 @@ def on_operation(self):
197198

198199
yield
199200

201+
transaction = self.graphql_span.containing_transaction
202+
if transaction and self.execution_context.operation_name:
203+
transaction.name = self.execution_context.operation_name
204+
transaction.source = TRANSACTION_SOURCE_COMPONENT
205+
transaction.op = op
206+
200207
self.graphql_span.finish()
201208

202209
def on_validate(self):

tests/integrations/strawberry/test_strawberry.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,8 @@ def test_capture_transaction_on_error(
324324
assert len(events) == 2
325325
(_, transaction_event) = events
326326

327-
if async_execution:
328-
assert transaction_event["transaction"] == "/graphql"
329-
else:
330-
assert transaction_event["transaction"] == "graphql_view"
331-
327+
assert transaction_event["transaction"] == "ErrorQuery"
328+
assert transaction_event["contexts"]["trace"]["op"] == OP.GRAPHQL_QUERY
332329
assert transaction_event["spans"]
333330

334331
query_spans = [
@@ -404,11 +401,8 @@ def test_capture_transaction_on_success(
404401
assert len(events) == 1
405402
(transaction_event,) = events
406403

407-
if async_execution:
408-
assert transaction_event["transaction"] == "/graphql"
409-
else:
410-
assert transaction_event["transaction"] == "graphql_view"
411-
404+
assert transaction_event["transaction"] == "GreetingQuery"
405+
assert transaction_event["contexts"]["trace"]["op"] == OP.GRAPHQL_QUERY
412406
assert transaction_event["spans"]
413407

414408
query_spans = [
@@ -564,11 +558,8 @@ def test_transaction_mutation(
564558
assert len(events) == 1
565559
(transaction_event,) = events
566560

567-
if async_execution:
568-
assert transaction_event["transaction"] == "/graphql"
569-
else:
570-
assert transaction_event["transaction"] == "graphql_view"
571-
561+
assert transaction_event["transaction"] == "Change"
562+
assert transaction_event["contexts"]["trace"]["op"] == OP.GRAPHQL_MUTATION
572563
assert transaction_event["spans"]
573564

574565
query_spans = [

0 commit comments

Comments
 (0)