|
12 | 12 | from sentry_sdk._types import MYPY |
13 | 13 | from sentry_sdk.hub import Hub, _should_send_default_pii |
14 | 14 | from sentry_sdk.integrations._wsgi_common import _filter_headers |
| 15 | +from sentry_sdk.sessions import auto_session_tracking |
15 | 16 | from sentry_sdk.utils import ( |
16 | 17 | ContextVar, |
17 | 18 | event_from_exception, |
@@ -119,37 +120,38 @@ async def _run_app(self, scope, callback): |
119 | 120 | _asgi_middleware_applied.set(True) |
120 | 121 | try: |
121 | 122 | hub = Hub(Hub.current) |
122 | | - with hub: |
123 | | - with hub.configure_scope() as sentry_scope: |
124 | | - sentry_scope.clear_breadcrumbs() |
125 | | - sentry_scope._name = "asgi" |
126 | | - processor = partial(self.event_processor, asgi_scope=scope) |
127 | | - sentry_scope.add_event_processor(processor) |
128 | | - |
129 | | - ty = scope["type"] |
130 | | - |
131 | | - if ty in ("http", "websocket"): |
132 | | - transaction = Transaction.continue_from_headers( |
133 | | - self._get_headers(scope), |
134 | | - op="{}.server".format(ty), |
135 | | - ) |
136 | | - else: |
137 | | - transaction = Transaction(op="asgi.server") |
138 | | - |
139 | | - transaction.name = _DEFAULT_TRANSACTION_NAME |
140 | | - transaction.set_tag("asgi.type", ty) |
141 | | - |
142 | | - with hub.start_transaction( |
143 | | - transaction, custom_sampling_context={"asgi_scope": scope} |
144 | | - ): |
145 | | - # XXX: Would be cool to have correct span status, but we |
146 | | - # would have to wrap send(). That is a bit hard to do with |
147 | | - # the current abstraction over ASGI 2/3. |
148 | | - try: |
149 | | - return await callback() |
150 | | - except Exception as exc: |
151 | | - _capture_exception(hub, exc) |
152 | | - raise exc from None |
| 123 | + with auto_session_tracking(hub, session_mode="request"): |
| 124 | + with hub: |
| 125 | + with hub.configure_scope() as sentry_scope: |
| 126 | + sentry_scope.clear_breadcrumbs() |
| 127 | + sentry_scope._name = "asgi" |
| 128 | + processor = partial(self.event_processor, asgi_scope=scope) |
| 129 | + sentry_scope.add_event_processor(processor) |
| 130 | + |
| 131 | + ty = scope["type"] |
| 132 | + |
| 133 | + if ty in ("http", "websocket"): |
| 134 | + transaction = Transaction.continue_from_headers( |
| 135 | + self._get_headers(scope), |
| 136 | + op="{}.server".format(ty), |
| 137 | + ) |
| 138 | + else: |
| 139 | + transaction = Transaction(op="asgi.server") |
| 140 | + |
| 141 | + transaction.name = _DEFAULT_TRANSACTION_NAME |
| 142 | + transaction.set_tag("asgi.type", ty) |
| 143 | + |
| 144 | + with hub.start_transaction( |
| 145 | + transaction, custom_sampling_context={"asgi_scope": scope} |
| 146 | + ): |
| 147 | + # XXX: Would be cool to have correct span status, but we |
| 148 | + # would have to wrap send(). That is a bit hard to do with |
| 149 | + # the current abstraction over ASGI 2/3. |
| 150 | + try: |
| 151 | + return await callback() |
| 152 | + except Exception as exc: |
| 153 | + _capture_exception(hub, exc) |
| 154 | + raise exc from None |
153 | 155 | finally: |
154 | 156 | _asgi_middleware_applied.set(False) |
155 | 157 |
|
|
0 commit comments