Skip to content

Commit 58da093

Browse files
ref(integrations): Use new scopes in Graphene, Strawberry (#2864)
--------- Co-authored-by: Anton Pirker <[email protected]>
1 parent 7c43f6f commit 58da093

File tree

2 files changed

+49
-54
lines changed

2 files changed

+49
-54
lines changed

sentry_sdk/integrations/graphene.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from sentry_sdk.hub import Hub, _should_send_default_pii
1+
import sentry_sdk
22
from sentry_sdk.integrations import DidNotEnable, Integration
3+
from sentry_sdk.scope import Scope, should_send_default_pii
34
from sentry_sdk.utils import (
45
capture_internal_exceptions,
56
event_from_exception,
@@ -46,53 +47,55 @@ def _patch_graphql():
4647

4748
def _sentry_patched_graphql_sync(schema, source, *args, **kwargs):
4849
# type: (GraphQLSchema, Union[str, Source], Any, Any) -> ExecutionResult
49-
hub = Hub.current
50-
integration = hub.get_integration(GrapheneIntegration)
50+
client = sentry_sdk.get_client()
51+
integration = client.get_integration(GrapheneIntegration)
5152
if integration is None:
5253
return old_graphql_sync(schema, source, *args, **kwargs)
5354

54-
with hub.configure_scope() as scope:
55-
scope.add_event_processor(_event_processor)
55+
scope = Scope.get_isolation_scope()
56+
scope.generate_propagation_context()
57+
scope.add_event_processor(_event_processor)
5658

5759
result = old_graphql_sync(schema, source, *args, **kwargs)
5860

5961
with capture_internal_exceptions():
6062
for error in result.errors or []:
6163
event, hint = event_from_exception(
6264
error,
63-
client_options=hub.client.options if hub.client else None,
65+
client_options=client.options,
6466
mechanism={
6567
"type": integration.identifier,
6668
"handled": False,
6769
},
6870
)
69-
hub.capture_event(event, hint=hint)
71+
sentry_sdk.capture_event(event, hint=hint)
7072

7173
return result
7274

7375
async def _sentry_patched_graphql_async(schema, source, *args, **kwargs):
7476
# type: (GraphQLSchema, Union[str, Source], Any, Any) -> ExecutionResult
75-
hub = Hub.current
76-
integration = hub.get_integration(GrapheneIntegration)
77+
client = sentry_sdk.get_client()
78+
integration = client.get_integration(GrapheneIntegration)
7779
if integration is None:
7880
return await old_graphql_async(schema, source, *args, **kwargs)
7981

80-
with hub.configure_scope() as scope:
81-
scope.add_event_processor(_event_processor)
82+
scope = Scope.get_isolation_scope()
83+
scope.generate_propagation_context()
84+
scope.add_event_processor(_event_processor)
8285

8386
result = await old_graphql_async(schema, source, *args, **kwargs)
8487

8588
with capture_internal_exceptions():
8689
for error in result.errors or []:
8790
event, hint = event_from_exception(
8891
error,
89-
client_options=hub.client.options if hub.client else None,
92+
client_options=client.options,
9093
mechanism={
9194
"type": integration.identifier,
9295
"handled": False,
9396
},
9497
)
95-
hub.capture_event(event, hint=hint)
98+
sentry_sdk.capture_event(event, hint=hint)
9699

97100
return result
98101

@@ -102,7 +105,7 @@ async def _sentry_patched_graphql_async(schema, source, *args, **kwargs):
102105

103106
def _event_processor(event, hint):
104107
# type: (Event, Dict[str, Any]) -> Event
105-
if _should_send_default_pii():
108+
if should_send_default_pii():
106109
request_info = event.setdefault("request", {})
107110
request_info["api_target"] = "graphql"
108111

sentry_sdk/integrations/strawberry.py

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import hashlib
22
from inspect import isawaitable
33

4-
from sentry_sdk import configure_scope, start_span
4+
import sentry_sdk
55
from sentry_sdk.consts import OP
66
from sentry_sdk.integrations import Integration, DidNotEnable
77
from sentry_sdk.integrations.logging import ignore_logger
8-
from sentry_sdk.hub import Hub, _should_send_default_pii
8+
from sentry_sdk.scope import Scope, should_send_default_pii
99
from sentry_sdk.utils import (
1010
capture_internal_exceptions,
11+
ensure_integration_enabled,
12+
ensure_integration_enabled_async,
1113
event_from_exception,
1214
logger,
1315
package_version,
@@ -85,7 +87,7 @@ def _patch_schema_init():
8587

8688
def _sentry_patched_schema_init(self, *args, **kwargs):
8789
# type: (Schema, Any, Any) -> None
88-
integration = Hub.current.get_integration(StrawberryIntegration)
90+
integration = sentry_sdk.get_client().get_integration(StrawberryIntegration)
8991
if integration is None:
9092
return old_schema_init(self, *args, **kwargs)
9193

@@ -165,21 +167,20 @@ def on_operation(self):
165167
if self._operation_name:
166168
description += " {}".format(self._operation_name)
167169

168-
Hub.current.add_breadcrumb(
170+
sentry_sdk.add_breadcrumb(
169171
category="graphql.operation",
170172
data={
171173
"operation_name": self._operation_name,
172174
"operation_type": operation_type,
173175
},
174176
)
175177

176-
with configure_scope() as scope:
177-
if scope.span:
178-
self.graphql_span = scope.span.start_child(
179-
op=op, description=description
180-
)
181-
else:
182-
self.graphql_span = start_span(op=op, description=description)
178+
scope = Scope.get_isolation_scope()
179+
scope.generate_propagation_context()
180+
if scope.span:
181+
self.graphql_span = scope.span.start_child(op=op, description=description)
182+
else:
183+
self.graphql_span = sentry_sdk.start_span(op=op, description=description)
183184

184185
self.graphql_span.set_data("graphql.operation.type", operation_type)
185186
self.graphql_span.set_data("graphql.operation.name", self._operation_name)
@@ -265,39 +266,29 @@ def _patch_execute():
265266
old_execute_async = strawberry_schema.execute
266267
old_execute_sync = strawberry_schema.execute_sync
267268

269+
@ensure_integration_enabled_async(StrawberryIntegration, old_execute_async)
268270
async def _sentry_patched_execute_async(*args, **kwargs):
269271
# type: (Any, Any) -> ExecutionResult
270-
hub = Hub.current
271-
integration = hub.get_integration(StrawberryIntegration)
272-
if integration is None:
273-
return await old_execute_async(*args, **kwargs)
274-
275272
result = await old_execute_async(*args, **kwargs)
276273

277274
if "execution_context" in kwargs and result.errors:
278-
with hub.configure_scope() as scope:
279-
event_processor = _make_request_event_processor(
280-
kwargs["execution_context"]
281-
)
282-
scope.add_event_processor(event_processor)
275+
scope = Scope.get_isolation_scope()
276+
scope.generate_propagation_context()
277+
event_processor = _make_request_event_processor(kwargs["execution_context"])
278+
scope.add_event_processor(event_processor)
283279

284280
return result
285281

282+
@ensure_integration_enabled(StrawberryIntegration, old_execute_sync)
286283
def _sentry_patched_execute_sync(*args, **kwargs):
287284
# type: (Any, Any) -> ExecutionResult
288-
hub = Hub.current
289-
integration = hub.get_integration(StrawberryIntegration)
290-
if integration is None:
291-
return old_execute_sync(*args, **kwargs)
292-
293285
result = old_execute_sync(*args, **kwargs)
294286

295287
if "execution_context" in kwargs and result.errors:
296-
with hub.configure_scope() as scope:
297-
event_processor = _make_request_event_processor(
298-
kwargs["execution_context"]
299-
)
300-
scope.add_event_processor(event_processor)
288+
scope = Scope.get_isolation_scope()
289+
scope.generate_propagation_context()
290+
event_processor = _make_request_event_processor(kwargs["execution_context"])
291+
scope.add_event_processor(event_processor)
301292

302293
return result
303294

@@ -322,29 +313,30 @@ def _sentry_patched_sync_view_handle_errors(self, errors, response_data):
322313

323314
def _sentry_patched_handle_errors(self, errors, response_data):
324315
# type: (Any, List[GraphQLError], GraphQLHTTPResponse) -> None
325-
hub = Hub.current
326-
integration = hub.get_integration(StrawberryIntegration)
316+
client = sentry_sdk.get_client()
317+
integration = client.get_integration(StrawberryIntegration)
327318
if integration is None:
328319
return
329320

330321
if not errors:
331322
return
332323

333-
with hub.configure_scope() as scope:
334-
event_processor = _make_response_event_processor(response_data)
335-
scope.add_event_processor(event_processor)
324+
scope = Scope.get_isolation_scope()
325+
scope.generate_propagation_context()
326+
event_processor = _make_response_event_processor(response_data)
327+
scope.add_event_processor(event_processor)
336328

337329
with capture_internal_exceptions():
338330
for error in errors:
339331
event, hint = event_from_exception(
340332
error,
341-
client_options=hub.client.options if hub.client else None,
333+
client_options=client.options,
342334
mechanism={
343335
"type": integration.identifier,
344336
"handled": False,
345337
},
346338
)
347-
hub.capture_event(event, hint=hint)
339+
sentry_sdk.capture_event(event, hint=hint)
348340

349341
async_base_view.AsyncBaseHTTPView._handle_errors = (
350342
_sentry_patched_async_view_handle_errors
@@ -360,7 +352,7 @@ def _make_request_event_processor(execution_context):
360352
def inner(event, hint):
361353
# type: (Event, dict[str, Any]) -> Event
362354
with capture_internal_exceptions():
363-
if _should_send_default_pii():
355+
if should_send_default_pii():
364356
request_data = event.setdefault("request", {})
365357
request_data["api_target"] = "graphql"
366358

@@ -391,7 +383,7 @@ def _make_response_event_processor(response_data):
391383
def inner(event, hint):
392384
# type: (Event, dict[str, Any]) -> Event
393385
with capture_internal_exceptions():
394-
if _should_send_default_pii():
386+
if should_send_default_pii():
395387
contexts = event.setdefault("contexts", {})
396388
contexts["response"] = {"data": response_data}
397389

0 commit comments

Comments
 (0)