From 175340eb3df526861b8dd6d0742174fd246c4578 Mon Sep 17 00:00:00 2001 From: Ivana Kellyerova Date: Thu, 21 Mar 2024 13:18:45 +0100 Subject: [PATCH 1/2] ref(asgi): Use new scopes API --- sentry_sdk/integrations/asgi.py | 44 ++++++++++++++------------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/sentry_sdk/integrations/asgi.py b/sentry_sdk/integrations/asgi.py index c874d6d3ef..126a509422 100644 --- a/sentry_sdk/integrations/asgi.py +++ b/sentry_sdk/integrations/asgi.py @@ -9,17 +9,17 @@ from copy import deepcopy from functools import partial +import sentry_sdk from sentry_sdk._types import TYPE_CHECKING from sentry_sdk.api import continue_trace from sentry_sdk.consts import OP -from sentry_sdk.hub import Hub from sentry_sdk.integrations._asgi_common import ( _get_headers, _get_request_data, _get_url, ) -from sentry_sdk.sessions import auto_session_tracking +from sentry_sdk.sessions import auto_session_tracking_scope from sentry_sdk.tracing import ( SOURCE_FOR_STYLE, TRANSACTION_SOURCE_ROUTE, @@ -54,17 +54,15 @@ TRANSACTION_STYLE_VALUES = ("endpoint", "url") -def _capture_exception(hub, exc, mechanism_type="asgi"): - # type: (Hub, Any, str) -> None +def _capture_exception(exc, mechanism_type="asgi"): + # type: (Any, str) -> None - # Check client here as it might have been unset while streaming response - if hub.client is not None: - event, hint = event_from_exception( - exc, - client_options=hub.client.options, - mechanism={"type": mechanism_type, "handled": False}, - ) - hub.capture_event(event, hint=hint) + event, hint = event_from_exception( + exc, + client_options=sentry_sdk.get_client().options, + mechanism={"type": mechanism_type, "handled": False}, + ) + sentry_sdk.capture_event(event, hint=hint) def _looks_like_asgi3(app): @@ -157,19 +155,17 @@ async def _run_app(self, scope, receive, send, asgi_version): return await self.app(scope, receive, send) except Exception as exc: - _capture_exception(Hub.current, exc, mechanism_type=self.mechanism_type) + _capture_exception(exc, mechanism_type=self.mechanism_type) raise exc from None _asgi_middleware_applied.set(True) try: - hub = Hub(Hub.current) - with hub: - with auto_session_tracking(hub, session_mode="request"): - with hub.configure_scope() as sentry_scope: - sentry_scope.clear_breadcrumbs() - sentry_scope._name = "asgi" - processor = partial(self.event_processor, asgi_scope=scope) - sentry_scope.add_event_processor(processor) + with sentry_sdk.isolation_scope() as scope: + with auto_session_tracking_scope(scope, session_mode="request"): + scope.clear_breadcrumbs() + scope._name = "asgi" + processor = partial(self.event_processor, asgi_scope=scope) + scope.add_event_processor(processor) ty = scope["type"] ( @@ -208,7 +204,7 @@ async def _run_app(self, scope, receive, send, asgi_version): transaction.source, ) - with hub.start_transaction( + with sentry_sdk.start_transaction( transaction, custom_sampling_context={"asgi_scope": scope} ): logger.debug("[ASGI] Started transaction: %s", transaction) @@ -235,9 +231,7 @@ async def _sentry_wrapped_send(event): scope, receive, _sentry_wrapped_send ) except Exception as exc: - _capture_exception( - hub, exc, mechanism_type=self.mechanism_type - ) + _capture_exception(exc, mechanism_type=self.mechanism_type) raise exc from None finally: _asgi_middleware_applied.set(False) From c59e2b68d605bd12f69f0c3e03adc865d955cf2e Mon Sep 17 00:00:00 2001 From: Ivana Kellyerova Date: Thu, 21 Mar 2024 13:30:15 +0100 Subject: [PATCH 2/2] fix wrong scope --- sentry_sdk/integrations/asgi.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sentry_sdk/integrations/asgi.py b/sentry_sdk/integrations/asgi.py index 126a509422..8aca37ea40 100644 --- a/sentry_sdk/integrations/asgi.py +++ b/sentry_sdk/integrations/asgi.py @@ -160,12 +160,12 @@ async def _run_app(self, scope, receive, send, asgi_version): _asgi_middleware_applied.set(True) try: - with sentry_sdk.isolation_scope() as scope: - with auto_session_tracking_scope(scope, session_mode="request"): - scope.clear_breadcrumbs() - scope._name = "asgi" + with sentry_sdk.isolation_scope() as sentry_scope: + with auto_session_tracking_scope(sentry_scope, session_mode="request"): + sentry_scope.clear_breadcrumbs() + sentry_scope._name = "asgi" processor = partial(self.event_processor, asgi_scope=scope) - scope.add_event_processor(processor) + sentry_scope.add_event_processor(processor) ty = scope["type"] (