6
6
7
7
import asyncio
8
8
import inspect
9
- from contextlib import nullcontext
10
9
from copy import deepcopy
11
10
from functools import partial
12
11
@@ -169,20 +168,24 @@ async def _run_asgi3(self, scope, receive, send):
169
168
# type: (Any, Any, Any) -> Any
170
169
return await self ._run_app (scope , receive , send , asgi_version = 3 )
171
170
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
+
172
183
async def _run_app (self , scope , receive , send , asgi_version ):
173
184
# type: (Any, Any, Any, Any, int) -> Any
174
185
is_recursive_asgi_middleware = _asgi_middleware_applied .get (False )
175
186
is_lifespan = scope ["type" ] == "lifespan"
176
187
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 )
186
189
187
190
_asgi_middleware_applied .set (True )
188
191
try :
@@ -209,52 +212,42 @@ async def _run_app(self, scope, receive, send, asgi_version):
209
212
210
213
method = scope .get ("method" , "" ).upper ()
211
214
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
+
212
220
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 ),
227
231
) as span :
228
232
if span is not None :
229
233
logger .debug ("[ASGI] Started transaction: %s" , span )
230
234
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
256
242
)
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
+ )
258
251
finally :
259
252
_asgi_middleware_applied .set (False )
260
253
0 commit comments