Skip to content

Commit 74402a2

Browse files
authored
chore(strawberry): Remove transaction-based tracing (#7166)
### Description Hide whitespace for a much easier time reviewing #### Issues Closes PY-2722 Closes #7118
1 parent c0a19b2 commit 74402a2

2 files changed

Lines changed: 233 additions & 685 deletions

File tree

sentry_sdk/integrations/strawberry.py

Lines changed: 71 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import functools
2-
import hashlib
32
import warnings
43
from inspect import isawaitable
54

@@ -9,8 +8,6 @@
98
from sentry_sdk.integrations.logging import ignore_logger
109
from sentry_sdk.scope import should_send_default_pii
1110
from sentry_sdk.traces import SegmentNameSource
12-
from sentry_sdk.tracing import Span, TransactionSource
13-
from sentry_sdk.tracing_utils import StreamedSpan, has_span_streaming_enabled
1411
from sentry_sdk.utils import (
1512
capture_internal_exceptions,
1613
ensure_integration_enabled,
@@ -133,18 +130,6 @@ def __init__(
133130
if execution_context:
134131
self.execution_context = execution_context
135132

136-
@functools.cached_property
137-
def _resource_name(self) -> str:
138-
query_hash = self.hash_query(self.execution_context.query) # type: ignore
139-
140-
if self.execution_context.operation_name:
141-
return "{}:{}".format(self.execution_context.operation_name, query_hash)
142-
143-
return query_hash
144-
145-
def hash_query(self, query: str) -> str:
146-
return hashlib.md5(query.encode("utf-8")).hexdigest()
147-
148133
def on_operation(self) -> "Generator[None, None, None]":
149134
operation_name = self.execution_context.operation_name
150135

@@ -178,138 +163,81 @@ def on_operation(self) -> "Generator[None, None, None]":
178163
scope.add_event_processor(event_processor)
179164

180165
client = sentry_sdk.get_client()
181-
is_span_streaming_enabled = has_span_streaming_enabled(client.options)
182-
if is_span_streaming_enabled:
183-
if sentry_sdk.traces.get_current_span() is None:
184-
yield
185-
return
186-
187-
additional_attributes: "dict[str, Any]" = {}
188-
if has_data_collection_enabled(client.options):
189-
if client.options["data_collection"]["graphql"]["document"]:
190-
additional_attributes["graphql.document"] = (
191-
self.execution_context.query
192-
)
193166

194-
elif should_send_default_pii():
195-
additional_attributes["graphql.document"] = self.execution_context.query
167+
if sentry_sdk.traces.get_current_span() is None:
168+
yield
169+
return
196170

197-
if operation_name:
198-
additional_attributes["graphql.operation.name"] = operation_name
199-
200-
graphql_span = sentry_sdk.traces.start_span(
201-
name=description,
202-
attributes={
203-
"sentry.origin": StrawberryIntegration.origin,
204-
"sentry.op": op,
205-
"graphql.operation.type": operation_type,
206-
**additional_attributes,
207-
},
208-
)
209-
else:
210-
graphql_span = sentry_sdk.start_span(
211-
op=op,
212-
name=description,
213-
origin=StrawberryIntegration.origin,
214-
)
215-
graphql_span.__enter__()
171+
additional_attributes: "dict[str, Any]" = {}
172+
if has_data_collection_enabled(client.options):
173+
if client.options["data_collection"]["graphql"]["document"]:
174+
additional_attributes["graphql.document"] = self.execution_context.query
216175

217-
if type(graphql_span) is Span:
218-
if has_data_collection_enabled(client.options):
219-
if client.options["data_collection"]["graphql"]["document"]:
220-
graphql_span.set_data(
221-
"graphql.document", self.execution_context.query
222-
)
223-
elif should_send_default_pii():
224-
graphql_span.set_data("graphql.document", self.execution_context.query)
176+
elif should_send_default_pii():
177+
additional_attributes["graphql.document"] = self.execution_context.query
225178

226-
graphql_span.set_data("graphql.operation.type", operation_type)
227-
graphql_span.set_data("graphql.operation.name", operation_name)
228-
# This attribute is being removed in streamed spans
229-
graphql_span.set_data("graphql.resource_name", self._resource_name)
179+
if operation_name:
180+
additional_attributes["graphql.operation.name"] = operation_name
181+
182+
graphql_span = sentry_sdk.traces.start_span(
183+
name=description,
184+
attributes={
185+
"sentry.origin": StrawberryIntegration.origin,
186+
"sentry.op": op,
187+
"graphql.operation.type": operation_type,
188+
**additional_attributes,
189+
},
190+
)
230191

231192
yield
232193

233-
if type(graphql_span) is StreamedSpan:
234-
if self.execution_context.operation_name:
235-
segment = graphql_span._segment
236-
segment.set_attribute(
237-
"sentry.segment.name.source", SegmentNameSource.COMPONENT
238-
)
239-
segment.set_attribute("sentry.op", op)
240-
segment.name = self.execution_context.operation_name
241-
elif isinstance(graphql_span, Span):
242-
transaction = graphql_span.containing_transaction
243-
if transaction and self.execution_context.operation_name:
244-
transaction.name = self.execution_context.operation_name
245-
transaction.source = TransactionSource.COMPONENT
246-
transaction.op = op
194+
if self.execution_context.operation_name:
195+
segment = graphql_span._segment
196+
segment.set_attribute(
197+
"sentry.segment.name.source", SegmentNameSource.COMPONENT
198+
)
199+
segment.set_attribute("sentry.op", op)
200+
segment.name = self.execution_context.operation_name
247201

248-
graphql_span.__exit__(None, None, None)
202+
graphql_span.end()
249203

250204
def on_validate(self) -> "Generator[None, None, None]":
251-
client = sentry_sdk.get_client()
252-
is_span_streaming_enabled = has_span_streaming_enabled(client.options)
253-
254-
if is_span_streaming_enabled:
255-
if sentry_sdk.traces.get_current_span() is None:
256-
yield
257-
return
258-
259-
validation_span = sentry_sdk.traces.start_span(
260-
name="validation",
261-
attributes={
262-
"sentry.op": OP.GRAPHQL_VALIDATE,
263-
"sentry.origin": StrawberryIntegration.origin,
264-
},
265-
)
266-
else:
267-
validation_span = sentry_sdk.start_span(
268-
op=OP.GRAPHQL_VALIDATE,
269-
name="validation",
270-
origin=StrawberryIntegration.origin,
271-
)
205+
if sentry_sdk.traces.get_current_span() is None:
206+
yield
207+
return
208+
209+
validation_span = sentry_sdk.traces.start_span(
210+
name="validation",
211+
attributes={
212+
"sentry.op": OP.GRAPHQL_VALIDATE,
213+
"sentry.origin": StrawberryIntegration.origin,
214+
},
215+
)
272216

273217
# If an exception is raised during validation, we still need to close the span
274218
try:
275219
yield
276220
finally:
277-
if isinstance(validation_span, StreamedSpan):
278-
validation_span.end()
279-
else:
280-
validation_span.finish()
221+
validation_span.end()
281222

282223
def on_parse(self) -> "Generator[None, None, None]":
283-
client = sentry_sdk.get_client()
284-
is_span_streaming_enabled = has_span_streaming_enabled(client.options)
285-
286-
if is_span_streaming_enabled:
287-
if sentry_sdk.traces.get_current_span() is None:
288-
yield
289-
return
290-
291-
parsing_span = sentry_sdk.traces.start_span(
292-
name="parsing",
293-
attributes={
294-
"sentry.op": OP.GRAPHQL_PARSE,
295-
"sentry.origin": StrawberryIntegration.origin,
296-
},
297-
)
298-
else:
299-
parsing_span = sentry_sdk.start_span(
300-
op=OP.GRAPHQL_PARSE,
301-
name="parsing",
302-
origin=StrawberryIntegration.origin,
303-
)
224+
if sentry_sdk.traces.get_current_span() is None:
225+
yield
226+
return
227+
228+
parsing_span = sentry_sdk.traces.start_span(
229+
name="parsing",
230+
attributes={
231+
"sentry.op": OP.GRAPHQL_PARSE,
232+
"sentry.origin": StrawberryIntegration.origin,
233+
},
234+
)
304235

305236
# If an exception is raised during parsing, we still need to close the span
306237
try:
307238
yield
308239
finally:
309-
if isinstance(parsing_span, StreamedSpan):
310-
parsing_span.end()
311-
else:
312-
parsing_span.finish()
240+
parsing_span.end()
313241

314242
def should_skip_tracing(
315243
self,
@@ -346,31 +274,16 @@ async def resolve(
346274

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

349-
client = sentry_sdk.get_client()
350-
is_span_streaming_enabled = has_span_streaming_enabled(client.options)
351-
if is_span_streaming_enabled:
352-
if sentry_sdk.traces.get_current_span() is None:
353-
return await self._resolve(_next, root, info, *args, **kwargs)
354-
355-
with sentry_sdk.traces.start_span(
356-
name=f"resolving {field_path}",
357-
attributes={
358-
"sentry.origin": StrawberryIntegration.origin,
359-
"sentry.op": OP.GRAPHQL_RESOLVE,
360-
},
361-
):
362-
return await self._resolve(_next, root, info, *args, **kwargs)
363-
364-
with sentry_sdk.start_span(
365-
op=OP.GRAPHQL_RESOLVE,
366-
name="resolving {}".format(field_path),
367-
origin=StrawberryIntegration.origin,
368-
) as span:
369-
span.set_data("graphql.field_name", info.field_name)
370-
span.set_data("graphql.parent_type", info.parent_type.name)
371-
span.set_data("graphql.field_path", field_path)
372-
span.set_data("graphql.path", ".".join(map(str, info.path.as_list())))
277+
if sentry_sdk.traces.get_current_span() is None:
278+
return await self._resolve(_next, root, info, *args, **kwargs)
373279

280+
with sentry_sdk.traces.start_span(
281+
name=f"resolving {field_path}",
282+
attributes={
283+
"sentry.origin": StrawberryIntegration.origin,
284+
"sentry.op": OP.GRAPHQL_RESOLVE,
285+
},
286+
):
374287
return await self._resolve(_next, root, info, *args, **kwargs)
375288

376289

@@ -388,31 +301,16 @@ def resolve(
388301

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

391-
client = sentry_sdk.get_client()
392-
is_span_streaming_enabled = has_span_streaming_enabled(client.options)
393-
if is_span_streaming_enabled:
394-
if sentry_sdk.traces.get_current_span() is None:
395-
return _next(root, info, *args, **kwargs)
396-
397-
with sentry_sdk.traces.start_span(
398-
name=f"resolving {field_path}",
399-
attributes={
400-
"sentry.origin": StrawberryIntegration.origin,
401-
"sentry.op": OP.GRAPHQL_RESOLVE,
402-
},
403-
):
404-
return _next(root, info, *args, **kwargs)
405-
406-
with sentry_sdk.start_span(
407-
op=OP.GRAPHQL_RESOLVE,
408-
name="resolving {}".format(field_path),
409-
origin=StrawberryIntegration.origin,
410-
) as span:
411-
span.set_data("graphql.field_name", info.field_name)
412-
span.set_data("graphql.parent_type", info.parent_type.name)
413-
span.set_data("graphql.field_path", field_path)
414-
span.set_data("graphql.path", ".".join(map(str, info.path.as_list())))
304+
if sentry_sdk.traces.get_current_span() is None:
305+
return _next(root, info, *args, **kwargs)
415306

307+
with sentry_sdk.traces.start_span(
308+
name=f"resolving {field_path}",
309+
attributes={
310+
"sentry.origin": StrawberryIntegration.origin,
311+
"sentry.op": OP.GRAPHQL_RESOLVE,
312+
},
313+
):
416314
return _next(root, info, *args, **kwargs)
417315

418316

0 commit comments

Comments
 (0)