Skip to content

Commit 37adf4a

Browse files
authored
ref(quart): Use new scopes API (#2870)
1 parent 6591432 commit 37adf4a

File tree

1 file changed

+21
-27
lines changed

1 file changed

+21
-27
lines changed

sentry_sdk/integrations/quart.py

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
import threading
44
from functools import wraps
55

6-
from sentry_sdk.hub import _should_send_default_pii, Hub
6+
import sentry_sdk
77
from sentry_sdk.integrations import DidNotEnable, Integration
88
from sentry_sdk.integrations._wsgi_common import _filter_headers
99
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
10-
from sentry_sdk.scope import Scope
10+
from sentry_sdk.scope import Scope, should_send_default_pii
1111
from sentry_sdk.tracing import SOURCE_FOR_STYLE
1212
from sentry_sdk.utils import (
1313
capture_internal_exceptions,
14+
ensure_integration_enabled,
1415
event_from_exception,
1516
)
1617
from sentry_sdk._types import TYPE_CHECKING
@@ -86,11 +87,9 @@ def patch_asgi_app():
8687
# type: () -> None
8788
old_app = Quart.__call__
8889

90+
@ensure_integration_enabled(QuartIntegration, old_app)
8991
async def sentry_patched_asgi_app(self, scope, receive, send):
9092
# type: (Any, Any, Any, Any) -> Any
91-
if Hub.current.get_integration(QuartIntegration) is None:
92-
return await old_app(self, scope, receive, send)
93-
9493
middleware = SentryAsgiMiddleware(lambda *a, **kw: old_app(self, *a, **kw))
9594
middleware.__call__ = middleware._run_asgi3
9695
return await middleware(scope, receive, send)
@@ -116,18 +115,19 @@ def decorator(old_func):
116115
@wraps(old_func)
117116
def _sentry_func(*args, **kwargs):
118117
# type: (*Any, **Any) -> Any
119-
hub = Hub.current
120-
integration = hub.get_integration(QuartIntegration)
118+
integration = sentry_sdk.get_client().get_integration(
119+
QuartIntegration
120+
)
121121
if integration is None:
122122
return old_func(*args, **kwargs)
123123

124-
with hub.configure_scope() as sentry_scope:
125-
if sentry_scope.profile is not None:
126-
sentry_scope.profile.active_thread_id = (
127-
threading.current_thread().ident
128-
)
124+
scope = Scope.get_isolation_scope()
125+
if scope.profile is not None:
126+
scope.profile.active_thread_id = (
127+
threading.current_thread().ident
128+
)
129129

130-
return old_func(*args, **kwargs)
130+
return old_func(*args, **kwargs)
131131

132132
return old_decorator(_sentry_func)
133133

@@ -156,8 +156,7 @@ def _set_transaction_name_and_source(scope, transaction_style, request):
156156

157157
async def _request_websocket_started(app, **kwargs):
158158
# type: (Quart, **Any) -> None
159-
hub = Hub.current
160-
integration = hub.get_integration(QuartIntegration)
159+
integration = sentry_sdk.get_client().get_integration(QuartIntegration)
161160
if integration is None:
162161
return
163162

@@ -172,11 +171,9 @@ async def _request_websocket_started(app, **kwargs):
172171
Scope.get_current_scope(), integration.transaction_style, request_websocket
173172
)
174173

175-
with hub.configure_scope() as scope:
176-
evt_processor = _make_request_event_processor(
177-
app, request_websocket, integration
178-
)
179-
scope.add_event_processor(evt_processor)
174+
scope = Scope.get_isolation_scope()
175+
evt_processor = _make_request_event_processor(app, request_websocket, integration)
176+
scope.add_event_processor(evt_processor)
180177

181178

182179
def _make_request_event_processor(app, request, integration):
@@ -199,7 +196,7 @@ def inner(event, hint):
199196
request_info["method"] = request.method
200197
request_info["headers"] = _filter_headers(dict(request.headers))
201198

202-
if _should_send_default_pii():
199+
if should_send_default_pii():
203200
request_info["env"] = {"REMOTE_ADDR": request.access_route[0]}
204201
_add_user_to_event(event)
205202

@@ -210,20 +207,17 @@ def inner(event, hint):
210207

211208
async def _capture_exception(sender, exception, **kwargs):
212209
# type: (Quart, Union[ValueError, BaseException], **Any) -> None
213-
hub = Hub.current
214-
if hub.get_integration(QuartIntegration) is None:
210+
client = sentry_sdk.get_client()
211+
if client.get_integration(QuartIntegration) is None:
215212
return
216213

217-
# If an integration is there, a client has to be there.
218-
client = hub.client # type: Any
219-
220214
event, hint = event_from_exception(
221215
exception,
222216
client_options=client.options,
223217
mechanism={"type": "quart", "handled": False},
224218
)
225219

226-
hub.capture_event(event, hint=hint)
220+
sentry_sdk.capture_event(event, hint=hint)
227221

228222

229223
def _add_user_to_event(event):

0 commit comments

Comments
 (0)