Skip to content

Commit

Permalink
Fix strawberry integration (#3874)
Browse files Browse the repository at this point in the history
  • Loading branch information
sl0thentr0py authored Dec 18, 2024
1 parent 1984a5c commit 92f5391
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 41 deletions.
69 changes: 30 additions & 39 deletions sentry_sdk/integrations/strawberry.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,59 +179,50 @@ def on_operation(self):
},
)

span = sentry_sdk.get_current_span()
if span:
self.graphql_span = span.start_child(
op=op,
name=description,
origin=StrawberryIntegration.origin,
)
else:
self.graphql_span = sentry_sdk.start_span(
op=op,
name=description,
origin=StrawberryIntegration.origin,
only_if_parent=True,
)
with sentry_sdk.start_span(
op=op,
name=description,
origin=StrawberryIntegration.origin,
only_if_parent=True,
) as graphql_span:
graphql_span.set_data("graphql.operation.type", operation_type)
graphql_span.set_data("graphql.document", self.execution_context.query)
graphql_span.set_data("graphql.resource_name", self._resource_name)

yield

self.graphql_span.set_data("graphql.operation.type", operation_type)
self.graphql_span.set_data("graphql.operation.name", self._operation_name)
self.graphql_span.set_data("graphql.document", self.execution_context.query)
self.graphql_span.set_data("graphql.resource_name", self._resource_name)
# we might have a more accurate operation_name after the parsing
self._operation_name = self.execution_context.operation_name

yield
if self._operation_name is not None:
graphql_span.set_data("graphql.operation.name", self._operation_name)

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
sentry_sdk.get_current_scope().set_transaction_name(
self._operation_name,
source=TRANSACTION_SOURCE_COMPONENT,
)

self.graphql_span.finish()
root_span = graphql_span.root_span
if root_span:
root_span.op = op

def on_validate(self):
# type: () -> Generator[None, None, None]
self.validation_span = self.graphql_span.start_child(
with sentry_sdk.start_span(
op=OP.GRAPHQL_VALIDATE,
name="validation",
origin=StrawberryIntegration.origin,
)

yield

self.validation_span.finish()
):
yield

def on_parse(self):
# type: () -> Generator[None, None, None]
self.parsing_span = self.graphql_span.start_child(
with sentry_sdk.start_span(
op=OP.GRAPHQL_PARSE,
name="parsing",
origin=StrawberryIntegration.origin,
)

yield

self.parsing_span.finish()
):
yield

def should_skip_tracing(self, _next, info):
# type: (Callable[[Any, GraphQLResolveInfo, Any, Any], Any], GraphQLResolveInfo) -> bool
Expand All @@ -253,7 +244,7 @@ async def resolve(self, _next, root, info, *args, **kwargs):

field_path = "{}.{}".format(info.parent_type, info.field_name)

with self.graphql_span.start_child(
with sentry_sdk.start_span(
op=OP.GRAPHQL_RESOLVE,
name="resolving {}".format(field_path),
origin=StrawberryIntegration.origin,
Expand All @@ -274,7 +265,7 @@ def resolve(self, _next, root, info, *args, **kwargs):

field_path = "{}.{}".format(info.parent_type, info.field_name)

with self.graphql_span.start_child(
with sentry_sdk.start_span(
op=OP.GRAPHQL_RESOLVE,
name="resolving {}".format(field_path),
origin=StrawberryIntegration.origin,
Expand Down
4 changes: 2 additions & 2 deletions tests/integrations/strawberry/test_strawberry.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ def test_transaction_no_operation_name(
query_span = query_spans[0]
assert query_span["description"] == "query"
assert query_span["data"]["graphql.operation.type"] == "query"
assert query_span["data"]["graphql.operation.name"] is None
assert "graphql.operation.name" not in query_span["data"]
assert query_span["data"]["graphql.document"] == query
assert query_span["data"]["graphql.resource_name"]

Expand Down Expand Up @@ -582,7 +582,7 @@ def test_transaction_mutation(
query_span = query_spans[0]
assert query_span["description"] == "mutation"
assert query_span["data"]["graphql.operation.type"] == "mutation"
assert query_span["data"]["graphql.operation.name"] is None
assert query_span["data"]["graphql.operation.name"] == "Change"
assert query_span["data"]["graphql.document"] == query
assert query_span["data"]["graphql.resource_name"]

Expand Down

0 comments on commit 92f5391

Please sign in to comment.