From a67bd7b2db3def6c8292ed270ef3632bf4e2fb42 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Tue, 16 Jul 2024 13:27:26 +0200 Subject: [PATCH 1/7] feat(strawberry): Use operation name as transaction name --- sentry_sdk/integrations/strawberry.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/sentry_sdk/integrations/strawberry.py b/sentry_sdk/integrations/strawberry.py index 5c16c60ff2..ce7eccd424 100644 --- a/sentry_sdk/integrations/strawberry.py +++ b/sentry_sdk/integrations/strawberry.py @@ -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, @@ -176,9 +177,15 @@ 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: + if self._operation_name: + transaction = span.containing_transaction + transaction.name = self._operation_name + transaction.source = TRANSACTION_SOURCE_COMPONENT + + self.graphql_span = span.start_child( op=op, description=description, origin=StrawberryIntegration.origin, From 00b7203772a7926d80d933d57ffd3f02684c0be8 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Tue, 16 Jul 2024 13:41:19 +0200 Subject: [PATCH 2/7] mypy; --- sentry_sdk/integrations/strawberry.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sentry_sdk/integrations/strawberry.py b/sentry_sdk/integrations/strawberry.py index ce7eccd424..357b010a64 100644 --- a/sentry_sdk/integrations/strawberry.py +++ b/sentry_sdk/integrations/strawberry.py @@ -182,8 +182,9 @@ def on_operation(self): if span: if self._operation_name: transaction = span.containing_transaction - transaction.name = self._operation_name - transaction.source = TRANSACTION_SOURCE_COMPONENT + if transaction: + transaction.name = self._operation_name + transaction.source = TRANSACTION_SOURCE_COMPONENT self.graphql_span = span.start_child( op=op, From effae81ad848571541ffd0dc8a2b21502a9e455f Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Tue, 16 Jul 2024 14:08:43 +0200 Subject: [PATCH 3/7] operation name might not be set yet --- sentry_sdk/integrations/strawberry.py | 12 +++++------- .../integrations/strawberry/test_strawberry.py | 18 +++--------------- 2 files changed, 8 insertions(+), 22 deletions(-) diff --git a/sentry_sdk/integrations/strawberry.py b/sentry_sdk/integrations/strawberry.py index 357b010a64..dc49f46c3e 100644 --- a/sentry_sdk/integrations/strawberry.py +++ b/sentry_sdk/integrations/strawberry.py @@ -178,14 +178,7 @@ def on_operation(self): ) span = sentry_sdk.get_current_span() - if span: - if self._operation_name: - transaction = span.containing_transaction - if transaction: - transaction.name = self._operation_name - transaction.source = TRANSACTION_SOURCE_COMPONENT - self.graphql_span = span.start_child( op=op, description=description, @@ -205,6 +198,11 @@ 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 + self.graphql_span.finish() def on_validate(self): diff --git a/tests/integrations/strawberry/test_strawberry.py b/tests/integrations/strawberry/test_strawberry.py index fc6f31710e..37325b6991 100644 --- a/tests/integrations/strawberry/test_strawberry.py +++ b/tests/integrations/strawberry/test_strawberry.py @@ -324,11 +324,7 @@ 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["spans"] query_spans = [ @@ -404,11 +400,7 @@ 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["spans"] query_spans = [ @@ -564,11 +556,7 @@ 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["spans"] query_spans = [ From 623f1f322ea46f474b33780446fa75673685613c Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Tue, 16 Jul 2024 14:40:53 +0200 Subject: [PATCH 4/7] also update op --- sentry_sdk/integrations/strawberry.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/sentry_sdk/integrations/strawberry.py b/sentry_sdk/integrations/strawberry.py index dc49f46c3e..c973de1eaa 100644 --- a/sentry_sdk/integrations/strawberry.py +++ b/sentry_sdk/integrations/strawberry.py @@ -199,9 +199,18 @@ 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 + if transaction: + if self.execution_context.operation_name: + transaction.name = self.execution_context.operation_name + transaction.source = TRANSACTION_SOURCE_COMPONENT + if operation_type: + op = { + "query": OP.GRAPHQL_QUERY, + "mutation": OP.GRAPHQL_MUTATION, + "subscription": OP.GRAPHQL_SUBSCRIPTION, + }.get(operation_type) + if op is not None: + transaction.op = op self.graphql_span.finish() From 934b36aa4b22ecb2471035084247d057849a8321 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Tue, 16 Jul 2024 14:45:47 +0200 Subject: [PATCH 5/7] tests --- tests/integrations/strawberry/test_strawberry.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/integrations/strawberry/test_strawberry.py b/tests/integrations/strawberry/test_strawberry.py index 37325b6991..dcc6632bdb 100644 --- a/tests/integrations/strawberry/test_strawberry.py +++ b/tests/integrations/strawberry/test_strawberry.py @@ -325,6 +325,7 @@ def test_capture_transaction_on_error( (_, transaction_event) = events assert transaction_event["transaction"] == "ErrorQuery" + assert transaction_event["contexts"]["trace"]["op"] == OP.GRAPHQL_QUERY assert transaction_event["spans"] query_spans = [ @@ -401,6 +402,7 @@ def test_capture_transaction_on_success( (transaction_event,) = events assert transaction_event["transaction"] == "GreetingQuery" + assert transaction_event["contexts"]["trace"]["op"] == OP.GRAPHQL_QUERY assert transaction_event["spans"] query_spans = [ @@ -557,6 +559,7 @@ def test_transaction_mutation( (transaction_event,) = events assert transaction_event["transaction"] == "Change" + assert transaction_event["contexts"]["trace"]["op"] == OP.GRAPHQL_MUTATION assert transaction_event["spans"] query_spans = [ From df43fa511ba57e31ef13e363571bf4e1e172d813 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Tue, 16 Jul 2024 14:59:33 +0200 Subject: [PATCH 6/7] we already have the op --- sentry_sdk/integrations/strawberry.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/sentry_sdk/integrations/strawberry.py b/sentry_sdk/integrations/strawberry.py index c973de1eaa..d378f7c796 100644 --- a/sentry_sdk/integrations/strawberry.py +++ b/sentry_sdk/integrations/strawberry.py @@ -203,14 +203,7 @@ def on_operation(self): if self.execution_context.operation_name: transaction.name = self.execution_context.operation_name transaction.source = TRANSACTION_SOURCE_COMPONENT - if operation_type: - op = { - "query": OP.GRAPHQL_QUERY, - "mutation": OP.GRAPHQL_MUTATION, - "subscription": OP.GRAPHQL_SUBSCRIPTION, - }.get(operation_type) - if op is not None: - transaction.op = op + transaction.op = op self.graphql_span.finish() From 95d62ba1ab587a3a9bf6355f05137004f24ed3b0 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Tue, 16 Jul 2024 15:33:11 +0200 Subject: [PATCH 7/7] formatting --- sentry_sdk/integrations/strawberry.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/sentry_sdk/integrations/strawberry.py b/sentry_sdk/integrations/strawberry.py index d378f7c796..326dd37fd6 100644 --- a/sentry_sdk/integrations/strawberry.py +++ b/sentry_sdk/integrations/strawberry.py @@ -199,11 +199,10 @@ def on_operation(self): yield transaction = self.graphql_span.containing_transaction - if transaction: - if self.execution_context.operation_name: - transaction.name = self.execution_context.operation_name - transaction.source = TRANSACTION_SOURCE_COMPONENT - transaction.op = op + 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()