1
1
import hashlib
2
2
from inspect import isawaitable
3
3
4
- from sentry_sdk import configure_scope , start_span
4
+ import sentry_sdk
5
5
from sentry_sdk .consts import OP
6
6
from sentry_sdk .integrations import Integration , DidNotEnable
7
7
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
9
9
from sentry_sdk .utils import (
10
10
capture_internal_exceptions ,
11
+ ensure_integration_enabled ,
12
+ ensure_integration_enabled_async ,
11
13
event_from_exception ,
12
14
logger ,
13
15
package_version ,
@@ -85,7 +87,7 @@ def _patch_schema_init():
85
87
86
88
def _sentry_patched_schema_init (self , * args , ** kwargs ):
87
89
# type: (Schema, Any, Any) -> None
88
- integration = Hub . current .get_integration (StrawberryIntegration )
90
+ integration = sentry_sdk . get_client () .get_integration (StrawberryIntegration )
89
91
if integration is None :
90
92
return old_schema_init (self , * args , ** kwargs )
91
93
@@ -165,21 +167,20 @@ def on_operation(self):
165
167
if self ._operation_name :
166
168
description += " {}" .format (self ._operation_name )
167
169
168
- Hub . current .add_breadcrumb (
170
+ sentry_sdk .add_breadcrumb (
169
171
category = "graphql.operation" ,
170
172
data = {
171
173
"operation_name" : self ._operation_name ,
172
174
"operation_type" : operation_type ,
173
175
},
174
176
)
175
177
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 )
183
184
184
185
self .graphql_span .set_data ("graphql.operation.type" , operation_type )
185
186
self .graphql_span .set_data ("graphql.operation.name" , self ._operation_name )
@@ -265,39 +266,29 @@ def _patch_execute():
265
266
old_execute_async = strawberry_schema .execute
266
267
old_execute_sync = strawberry_schema .execute_sync
267
268
269
+ @ensure_integration_enabled_async (StrawberryIntegration , old_execute_async )
268
270
async def _sentry_patched_execute_async (* args , ** kwargs ):
269
271
# 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
-
275
272
result = await old_execute_async (* args , ** kwargs )
276
273
277
274
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 )
283
279
284
280
return result
285
281
282
+ @ensure_integration_enabled (StrawberryIntegration , old_execute_sync )
286
283
def _sentry_patched_execute_sync (* args , ** kwargs ):
287
284
# 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
-
293
285
result = old_execute_sync (* args , ** kwargs )
294
286
295
287
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 )
301
292
302
293
return result
303
294
@@ -322,29 +313,30 @@ def _sentry_patched_sync_view_handle_errors(self, errors, response_data):
322
313
323
314
def _sentry_patched_handle_errors (self , errors , response_data ):
324
315
# 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 )
327
318
if integration is None :
328
319
return
329
320
330
321
if not errors :
331
322
return
332
323
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 )
336
328
337
329
with capture_internal_exceptions ():
338
330
for error in errors :
339
331
event , hint = event_from_exception (
340
332
error ,
341
- client_options = hub . client .options if hub . client else None ,
333
+ client_options = client .options ,
342
334
mechanism = {
343
335
"type" : integration .identifier ,
344
336
"handled" : False ,
345
337
},
346
338
)
347
- hub .capture_event (event , hint = hint )
339
+ sentry_sdk .capture_event (event , hint = hint )
348
340
349
341
async_base_view .AsyncBaseHTTPView ._handle_errors = (
350
342
_sentry_patched_async_view_handle_errors
@@ -360,7 +352,7 @@ def _make_request_event_processor(execution_context):
360
352
def inner (event , hint ):
361
353
# type: (Event, dict[str, Any]) -> Event
362
354
with capture_internal_exceptions ():
363
- if _should_send_default_pii ():
355
+ if should_send_default_pii ():
364
356
request_data = event .setdefault ("request" , {})
365
357
request_data ["api_target" ] = "graphql"
366
358
@@ -391,7 +383,7 @@ def _make_response_event_processor(response_data):
391
383
def inner (event , hint ):
392
384
# type: (Event, dict[str, Any]) -> Event
393
385
with capture_internal_exceptions ():
394
- if _should_send_default_pii ():
386
+ if should_send_default_pii ():
395
387
contexts = event .setdefault ("contexts" , {})
396
388
contexts ["response" ] = {"data" : response_data }
397
389
0 commit comments