Skip to content

Commit ed27661

Browse files
ref: Ariadne integration new scope API (#2850)
Fixes GH-2848
1 parent f660707 commit ed27661

File tree

1 file changed

+25
-36
lines changed

1 file changed

+25
-36
lines changed

sentry_sdk/integrations/ariadne.py

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
from importlib import import_module
22

3-
from sentry_sdk.hub import Hub, _should_send_default_pii
3+
from sentry_sdk import get_client, capture_event
44
from sentry_sdk.integrations import DidNotEnable, Integration
55
from sentry_sdk.integrations.logging import ignore_logger
66
from sentry_sdk.integrations._wsgi_common import request_body_within_bounds
7+
from sentry_sdk.scope import Scope, should_send_default_pii
78
from sentry_sdk.utils import (
89
capture_internal_exceptions,
10+
ensure_integration_enabled,
911
event_from_exception,
1012
package_version,
1113
)
@@ -51,73 +53,60 @@ def _patch_graphql():
5153
old_handle_errors = ariadne_graphql.handle_graphql_errors
5254
old_handle_query_result = ariadne_graphql.handle_query_result
5355

56+
@ensure_integration_enabled(AriadneIntegration, old_parse_query)
5457
def _sentry_patched_parse_query(context_value, query_parser, data):
5558
# type: (Optional[Any], Optional[QueryParser], Any) -> DocumentNode
56-
hub = Hub.current
57-
integration = hub.get_integration(AriadneIntegration)
58-
if integration is None:
59-
return old_parse_query(context_value, query_parser, data)
60-
61-
with hub.configure_scope() as scope:
62-
event_processor = _make_request_event_processor(data)
63-
scope.add_event_processor(event_processor)
59+
event_processor = _make_request_event_processor(data)
60+
Scope.get_isolation_scope().add_event_processor(event_processor)
6461

6562
result = old_parse_query(context_value, query_parser, data)
6663
return result
6764

65+
@ensure_integration_enabled(AriadneIntegration, old_handle_errors)
6866
def _sentry_patched_handle_graphql_errors(errors, *args, **kwargs):
6967
# type: (List[GraphQLError], Any, Any) -> GraphQLResult
70-
hub = Hub.current
71-
integration = hub.get_integration(AriadneIntegration)
72-
if integration is None:
73-
return old_handle_errors(errors, *args, **kwargs)
74-
7568
result = old_handle_errors(errors, *args, **kwargs)
7669

77-
with hub.configure_scope() as scope:
78-
event_processor = _make_response_event_processor(result[1])
79-
scope.add_event_processor(event_processor)
70+
event_processor = _make_response_event_processor(result[1])
71+
Scope.get_isolation_scope().add_event_processor(event_processor)
8072

81-
if hub.client:
73+
client = get_client()
74+
if client.is_active():
8275
with capture_internal_exceptions():
8376
for error in errors:
8477
event, hint = event_from_exception(
8578
error,
86-
client_options=hub.client.options,
79+
client_options=client.options,
8780
mechanism={
88-
"type": integration.identifier,
81+
"type": AriadneIntegration.identifier,
8982
"handled": False,
9083
},
9184
)
92-
hub.capture_event(event, hint=hint)
85+
capture_event(event, hint=hint)
9386

9487
return result
9588

89+
@ensure_integration_enabled(AriadneIntegration, old_handle_query_result)
9690
def _sentry_patched_handle_query_result(result, *args, **kwargs):
9791
# type: (Any, Any, Any) -> GraphQLResult
98-
hub = Hub.current
99-
integration = hub.get_integration(AriadneIntegration)
100-
if integration is None:
101-
return old_handle_query_result(result, *args, **kwargs)
102-
10392
query_result = old_handle_query_result(result, *args, **kwargs)
10493

105-
with hub.configure_scope() as scope:
106-
event_processor = _make_response_event_processor(query_result[1])
107-
scope.add_event_processor(event_processor)
94+
event_processor = _make_response_event_processor(query_result[1])
95+
Scope.get_isolation_scope().add_event_processor(event_processor)
10896

109-
if hub.client:
97+
client = get_client()
98+
if client.is_active():
11099
with capture_internal_exceptions():
111100
for error in result.errors or []:
112101
event, hint = event_from_exception(
113102
error,
114-
client_options=hub.client.options,
103+
client_options=client.options,
115104
mechanism={
116-
"type": integration.identifier,
105+
"type": AriadneIntegration.identifier,
117106
"handled": False,
118107
},
119108
)
120-
hub.capture_event(event, hint=hint)
109+
capture_event(event, hint=hint)
121110

122111
return query_result
123112

@@ -143,8 +132,8 @@ def inner(event, hint):
143132
except (TypeError, ValueError):
144133
return event
145134

146-
if _should_send_default_pii() and request_body_within_bounds(
147-
Hub.current.client, content_length
135+
if should_send_default_pii() and request_body_within_bounds(
136+
get_client(), content_length
148137
):
149138
request_info = event.setdefault("request", {})
150139
request_info["api_target"] = "graphql"
@@ -165,7 +154,7 @@ def _make_response_event_processor(response):
165154
def inner(event, hint):
166155
# type: (Event, dict[str, Any]) -> Event
167156
with capture_internal_exceptions():
168-
if _should_send_default_pii() and response.get("errors"):
157+
if should_send_default_pii() and response.get("errors"):
169158
contexts = event.setdefault("contexts", {})
170159
contexts["response"] = {
171160
"data": response,

0 commit comments

Comments
 (0)