Skip to content

Commit f228f70

Browse files
authored
ref(asgi): Use new scopes API (#2874)
1 parent 63fb50d commit f228f70

File tree

1 file changed

+19
-25
lines changed

1 file changed

+19
-25
lines changed

sentry_sdk/integrations/asgi.py

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@
99
from copy import deepcopy
1010
from functools import partial
1111

12+
import sentry_sdk
1213
from sentry_sdk._types import TYPE_CHECKING
1314
from sentry_sdk.api import continue_trace
1415
from sentry_sdk.consts import OP
15-
from sentry_sdk.hub import Hub
1616

1717
from sentry_sdk.integrations._asgi_common import (
1818
_get_headers,
1919
_get_request_data,
2020
_get_url,
2121
)
22-
from sentry_sdk.sessions import auto_session_tracking
22+
from sentry_sdk.sessions import auto_session_tracking_scope
2323
from sentry_sdk.tracing import (
2424
SOURCE_FOR_STYLE,
2525
TRANSACTION_SOURCE_ROUTE,
@@ -54,17 +54,15 @@
5454
TRANSACTION_STYLE_VALUES = ("endpoint", "url")
5555

5656

57-
def _capture_exception(hub, exc, mechanism_type="asgi"):
58-
# type: (Hub, Any, str) -> None
57+
def _capture_exception(exc, mechanism_type="asgi"):
58+
# type: (Any, str) -> None
5959

60-
# Check client here as it might have been unset while streaming response
61-
if hub.client is not None:
62-
event, hint = event_from_exception(
63-
exc,
64-
client_options=hub.client.options,
65-
mechanism={"type": mechanism_type, "handled": False},
66-
)
67-
hub.capture_event(event, hint=hint)
60+
event, hint = event_from_exception(
61+
exc,
62+
client_options=sentry_sdk.get_client().options,
63+
mechanism={"type": mechanism_type, "handled": False},
64+
)
65+
sentry_sdk.capture_event(event, hint=hint)
6866

6967

7068
def _looks_like_asgi3(app):
@@ -157,19 +155,17 @@ async def _run_app(self, scope, receive, send, asgi_version):
157155
return await self.app(scope, receive, send)
158156

159157
except Exception as exc:
160-
_capture_exception(Hub.current, exc, mechanism_type=self.mechanism_type)
158+
_capture_exception(exc, mechanism_type=self.mechanism_type)
161159
raise exc from None
162160

163161
_asgi_middleware_applied.set(True)
164162
try:
165-
hub = Hub(Hub.current)
166-
with hub:
167-
with auto_session_tracking(hub, session_mode="request"):
168-
with hub.configure_scope() as sentry_scope:
169-
sentry_scope.clear_breadcrumbs()
170-
sentry_scope._name = "asgi"
171-
processor = partial(self.event_processor, asgi_scope=scope)
172-
sentry_scope.add_event_processor(processor)
163+
with sentry_sdk.isolation_scope() as sentry_scope:
164+
with auto_session_tracking_scope(sentry_scope, session_mode="request"):
165+
sentry_scope.clear_breadcrumbs()
166+
sentry_scope._name = "asgi"
167+
processor = partial(self.event_processor, asgi_scope=scope)
168+
sentry_scope.add_event_processor(processor)
173169

174170
ty = scope["type"]
175171
(
@@ -208,7 +204,7 @@ async def _run_app(self, scope, receive, send, asgi_version):
208204
transaction.source,
209205
)
210206

211-
with hub.start_transaction(
207+
with sentry_sdk.start_transaction(
212208
transaction, custom_sampling_context={"asgi_scope": scope}
213209
):
214210
logger.debug("[ASGI] Started transaction: %s", transaction)
@@ -235,9 +231,7 @@ async def _sentry_wrapped_send(event):
235231
scope, receive, _sentry_wrapped_send
236232
)
237233
except Exception as exc:
238-
_capture_exception(
239-
hub, exc, mechanism_type=self.mechanism_type
240-
)
234+
_capture_exception(exc, mechanism_type=self.mechanism_type)
241235
raise exc from None
242236
finally:
243237
_asgi_middleware_applied.set(False)

0 commit comments

Comments
 (0)