|
10 | 10 | from functools import partial
|
11 | 11 |
|
12 | 12 | import sentry_sdk
|
13 |
| -from sentry_sdk.api import continue_trace |
14 | 13 | from sentry_sdk.consts import OP
|
15 | 14 |
|
16 | 15 | from sentry_sdk.integrations._asgi_common import (
|
|
34 | 33 | transaction_from_function,
|
35 | 34 | _get_installed_modules,
|
36 | 35 | )
|
37 |
| -from sentry_sdk.tracing import Transaction |
38 | 36 |
|
39 | 37 | from typing import TYPE_CHECKING
|
40 | 38 |
|
@@ -185,66 +183,47 @@ async def _run_app(self, scope, receive, send, asgi_version):
|
185 | 183 | scope,
|
186 | 184 | )
|
187 | 185 |
|
188 |
| - if ty in ("http", "websocket"): |
189 |
| - transaction = continue_trace( |
190 |
| - _get_headers(scope), |
191 |
| - op="{}.server".format(ty), |
| 186 | + with sentry_sdk.continue_trace(_get_headers(scope)): |
| 187 | + with sentry_sdk.start_transaction( |
| 188 | + op=( |
| 189 | + OP.WEBSOCKET_SERVER |
| 190 | + if ty == "websocket" |
| 191 | + else OP.HTTP_SERVER |
| 192 | + ), |
192 | 193 | name=transaction_name,
|
193 | 194 | source=transaction_source,
|
194 | 195 | origin=self.span_origin,
|
195 |
| - ) |
196 |
| - logger.debug( |
197 |
| - "[ASGI] Created transaction (continuing trace): %s", |
198 |
| - transaction, |
199 |
| - ) |
200 |
| - else: |
201 |
| - transaction = Transaction( |
202 |
| - op=OP.HTTP_SERVER, |
203 |
| - name=transaction_name, |
204 |
| - source=transaction_source, |
205 |
| - origin=self.span_origin, |
206 |
| - ) |
207 |
| - logger.debug( |
208 |
| - "[ASGI] Created transaction (new): %s", transaction |
209 |
| - ) |
210 |
| - |
211 |
| - transaction.set_tag("asgi.type", ty) |
212 |
| - logger.debug( |
213 |
| - "[ASGI] Set transaction name and source on transaction: '%s' / '%s'", |
214 |
| - transaction.name, |
215 |
| - transaction.source, |
216 |
| - ) |
217 |
| - |
218 |
| - with sentry_sdk.start_transaction( |
219 |
| - transaction, |
220 |
| - custom_sampling_context={"asgi_scope": scope}, |
221 |
| - ): |
222 |
| - logger.debug("[ASGI] Started transaction: %s", transaction) |
223 |
| - try: |
224 |
| - |
225 |
| - async def _sentry_wrapped_send(event): |
226 |
| - # type: (Dict[str, Any]) -> Any |
227 |
| - is_http_response = ( |
228 |
| - event.get("type") == "http.response.start" |
229 |
| - and transaction is not None |
230 |
| - and "status" in event |
231 |
| - ) |
232 |
| - if is_http_response: |
233 |
| - transaction.set_http_status(event["status"]) |
234 |
| - |
235 |
| - return await send(event) |
236 |
| - |
237 |
| - if asgi_version == 2: |
238 |
| - return await self.app(scope)( |
239 |
| - receive, _sentry_wrapped_send |
240 |
| - ) |
241 |
| - else: |
242 |
| - return await self.app( |
243 |
| - scope, receive, _sentry_wrapped_send |
| 196 | + custom_sampling_context={"asgi_scope": scope}, |
| 197 | + ) as transaction: |
| 198 | + logger.debug("[ASGI] Started transaction: %s", transaction) |
| 199 | + transaction.set_tag("asgi.type", ty) |
| 200 | + try: |
| 201 | + |
| 202 | + async def _sentry_wrapped_send(event): |
| 203 | + # type: (Dict[str, Any]) -> Any |
| 204 | + is_http_response = ( |
| 205 | + event.get("type") == "http.response.start" |
| 206 | + and transaction is not None |
| 207 | + and "status" in event |
| 208 | + ) |
| 209 | + if is_http_response: |
| 210 | + transaction.set_http_status(event["status"]) |
| 211 | + |
| 212 | + return await send(event) |
| 213 | + |
| 214 | + if asgi_version == 2: |
| 215 | + return await self.app(scope)( |
| 216 | + receive, _sentry_wrapped_send |
| 217 | + ) |
| 218 | + else: |
| 219 | + return await self.app( |
| 220 | + scope, receive, _sentry_wrapped_send |
| 221 | + ) |
| 222 | + except Exception as exc: |
| 223 | + _capture_exception( |
| 224 | + exc, mechanism_type=self.mechanism_type |
244 | 225 | )
|
245 |
| - except Exception as exc: |
246 |
| - _capture_exception(exc, mechanism_type=self.mechanism_type) |
247 |
| - raise exc from None |
| 226 | + raise exc from None |
248 | 227 | finally:
|
249 | 228 | _asgi_middleware_applied.set(False)
|
250 | 229 |
|
|
0 commit comments