From 64a46ce07380bc01b88a546c040207865e1df5e1 Mon Sep 17 00:00:00 2001 From: Ivana Kellyerova Date: Thu, 21 Mar 2024 11:45:55 +0100 Subject: [PATCH] ref(quart): Use new scopes in Quart integration --- sentry_sdk/integrations/quart.py | 48 ++++++++++++++------------------ 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/sentry_sdk/integrations/quart.py b/sentry_sdk/integrations/quart.py index 52fc169008..baa975f12e 100644 --- a/sentry_sdk/integrations/quart.py +++ b/sentry_sdk/integrations/quart.py @@ -3,14 +3,15 @@ import threading from functools import wraps -from sentry_sdk.hub import _should_send_default_pii, Hub +import sentry_sdk from sentry_sdk.integrations import DidNotEnable, Integration from sentry_sdk.integrations._wsgi_common import _filter_headers from sentry_sdk.integrations.asgi import SentryAsgiMiddleware -from sentry_sdk.scope import Scope +from sentry_sdk.scope import Scope, should_send_default_pii from sentry_sdk.tracing import SOURCE_FOR_STYLE from sentry_sdk.utils import ( capture_internal_exceptions, + ensure_integration_enabled, event_from_exception, ) from sentry_sdk._types import TYPE_CHECKING @@ -86,11 +87,9 @@ def patch_asgi_app(): # type: () -> None old_app = Quart.__call__ + @ensure_integration_enabled(QuartIntegration, old_app) async def sentry_patched_asgi_app(self, scope, receive, send): # type: (Any, Any, Any, Any) -> Any - if Hub.current.get_integration(QuartIntegration) is None: - return await old_app(self, scope, receive, send) - middleware = SentryAsgiMiddleware(lambda *a, **kw: old_app(self, *a, **kw)) middleware.__call__ = middleware._run_asgi3 return await middleware(scope, receive, send) @@ -116,18 +115,19 @@ def decorator(old_func): @wraps(old_func) def _sentry_func(*args, **kwargs): # type: (*Any, **Any) -> Any - hub = Hub.current - integration = hub.get_integration(QuartIntegration) + integration = sentry_sdk.get_client().get_integration( + QuartIntegration + ) if integration is None: return old_func(*args, **kwargs) - with hub.configure_scope() as sentry_scope: - if sentry_scope.profile is not None: - sentry_scope.profile.active_thread_id = ( - threading.current_thread().ident - ) + scope = Scope.get_isolation_scope() + if scope.profile is not None: + scope.profile.active_thread_id = ( + threading.current_thread().ident + ) - return old_func(*args, **kwargs) + return old_func(*args, **kwargs) return old_decorator(_sentry_func) @@ -156,8 +156,7 @@ def _set_transaction_name_and_source(scope, transaction_style, request): async def _request_websocket_started(app, **kwargs): # type: (Quart, **Any) -> None - hub = Hub.current - integration = hub.get_integration(QuartIntegration) + integration = sentry_sdk.get_client().get_integration(QuartIntegration) if integration is None: return @@ -172,11 +171,9 @@ async def _request_websocket_started(app, **kwargs): Scope.get_current_scope(), integration.transaction_style, request_websocket ) - with hub.configure_scope() as scope: - evt_processor = _make_request_event_processor( - app, request_websocket, integration - ) - scope.add_event_processor(evt_processor) + scope = Scope.get_isolation_scope() + evt_processor = _make_request_event_processor(app, request_websocket, integration) + scope.add_event_processor(evt_processor) def _make_request_event_processor(app, request, integration): @@ -199,7 +196,7 @@ def inner(event, hint): request_info["method"] = request.method request_info["headers"] = _filter_headers(dict(request.headers)) - if _should_send_default_pii(): + if should_send_default_pii(): request_info["env"] = {"REMOTE_ADDR": request.access_route[0]} _add_user_to_event(event) @@ -210,20 +207,17 @@ def inner(event, hint): async def _capture_exception(sender, exception, **kwargs): # type: (Quart, Union[ValueError, BaseException], **Any) -> None - hub = Hub.current - if hub.get_integration(QuartIntegration) is None: + client = sentry_sdk.get_client() + if client.get_integration(QuartIntegration) is None: return - # If an integration is there, a client has to be there. - client = hub.client # type: Any - event, hint = event_from_exception( exception, client_options=client.options, mechanism={"type": "quart", "handled": False}, ) - hub.capture_event(event, hint=hint) + sentry_sdk.capture_event(event, hint=hint) def _add_user_to_event(event):