66
77import asyncio
88import inspect
9- from contextlib import nullcontext
109from copy import deepcopy
1110from functools import partial
1211
@@ -169,20 +168,24 @@ async def _run_asgi3(self, scope, receive, send):
169168 # type: (Any, Any, Any) -> Any
170169 return await self ._run_app (scope , receive , send , asgi_version = 3 )
171170
171+ async def _run_original_app (self , scope , receive , send , asgi_version ):
172+ # type: (Any, Any, Any, Any, int) -> Any
173+ try :
174+ if asgi_version == 2 :
175+ return await self .app (scope )(receive , send )
176+ else :
177+ return await self .app (scope , receive , send )
178+
179+ except Exception as exc :
180+ _capture_exception (exc , mechanism_type = self .mechanism_type )
181+ raise exc from None
182+
172183 async def _run_app (self , scope , receive , send , asgi_version ):
173184 # type: (Any, Any, Any, Any, int) -> Any
174185 is_recursive_asgi_middleware = _asgi_middleware_applied .get (False )
175186 is_lifespan = scope ["type" ] == "lifespan"
176187 if is_recursive_asgi_middleware or is_lifespan :
177- try :
178- if asgi_version == 2 :
179- return await self .app (scope )(receive , send )
180- else :
181- return await self .app (scope , receive , send )
182-
183- except Exception as exc :
184- _capture_exception (exc , mechanism_type = self .mechanism_type )
185- raise exc from None
188+ return await self ._run_original_app (scope , receive , send , asgi_version )
186189
187190 _asgi_middleware_applied .set (True )
188191 try :
@@ -209,52 +212,42 @@ async def _run_app(self, scope, receive, send, asgi_version):
209212
210213 method = scope .get ("method" , "" ).upper ()
211214 should_trace = method in self .http_methods_to_capture
215+ if not should_trace :
216+ return await self ._run_original_app (
217+ scope , receive , send , asgi_version
218+ )
219+
212220 with sentry_sdk .continue_trace (_get_headers (scope )):
213- with (
214- sentry_sdk .start_span (
215- op = (
216- OP .WEBSOCKET_SERVER
217- if ty == "websocket"
218- else OP .HTTP_SERVER
219- ),
220- name = transaction_name ,
221- source = transaction_source ,
222- origin = self .span_origin ,
223- attributes = _prepopulate_attributes (scope ),
224- )
225- if should_trace
226- else nullcontext ()
221+ with sentry_sdk .start_span (
222+ op = (
223+ OP .WEBSOCKET_SERVER
224+ if ty == "websocket"
225+ else OP .HTTP_SERVER
226+ ),
227+ name = transaction_name ,
228+ source = transaction_source ,
229+ origin = self .span_origin ,
230+ attributes = _prepopulate_attributes (scope ),
227231 ) as span :
228232 if span is not None :
229233 logger .debug ("[ASGI] Started transaction: %s" , span )
230234 span .set_tag ("asgi.type" , ty )
231- try :
232-
233- async def _sentry_wrapped_send (event ):
234- # type: (Dict[str, Any]) -> Any
235- is_http_response = (
236- event .get ("type" ) == "http.response.start"
237- and span is not None
238- and "status" in event
239- )
240- if is_http_response :
241- span .set_http_status (event ["status" ])
242-
243- return await send (event )
244-
245- if asgi_version == 2 :
246- return await self .app (scope )(
247- receive , _sentry_wrapped_send
248- )
249- else :
250- return await self .app (
251- scope , receive , _sentry_wrapped_send
252- )
253- except Exception as exc :
254- _capture_exception (
255- exc , mechanism_type = self .mechanism_type
235+
236+ async def _sentry_wrapped_send (event ):
237+ # type: (Dict[str, Any]) -> Any
238+ is_http_response = (
239+ event .get ("type" ) == "http.response.start"
240+ and span is not None
241+ and "status" in event
256242 )
257- raise exc from None
243+ if is_http_response :
244+ span .set_http_status (event ["status" ])
245+
246+ return await send (event )
247+
248+ return await self ._run_original_app (
249+ scope , receive , _sentry_wrapped_send , asgi_version
250+ )
258251 finally :
259252 _asgi_middleware_applied .set (False )
260253
0 commit comments