1313from typing import TYPE_CHECKING
1414
1515import sentry_sdk
16- from sentry_sdk .api import continue_trace
1716from sentry_sdk .consts import OP , SPANDATA
1817from sentry_sdk .integrations ._asgi_common import (
1918 _get_headers ,
3837)
3938from sentry_sdk .tracing import (
4039 SOURCE_FOR_STYLE ,
41- Transaction ,
4240 TransactionSource ,
4341)
44- from sentry_sdk .tracing_utils import has_span_streaming_enabled
4542from sentry_sdk .utils import (
4643 _get_installed_modules ,
4744 capture_internal_exceptions ,
5451)
5552
5653if TYPE_CHECKING :
57- from typing import Any , ContextManager , Dict , Optional , Tuple , Union
54+ from typing import Any , ContextManager , Dict , Optional , Tuple
5855
5956 from sentry_sdk ._types import Attributes , Event , Hint
60- from sentry_sdk .tracing import Span
6157
6258
6359_asgi_middleware_applied : "ContextVar[bool]" = ContextVar (
@@ -202,9 +198,6 @@ async def _run_app(
202198 self ._capture_lifespan_exception (exc )
203199 reraise (* exc_info )
204200
205- client = sentry_sdk .get_client ()
206- span_streaming = has_span_streaming_enabled (client .options )
207-
208201 _asgi_middleware_applied .set (True )
209202 try :
210203 with sentry_sdk .isolation_scope () as sentry_scope :
@@ -225,95 +218,55 @@ async def _run_app(
225218
226219 method = scope .get ("method" , "" ).upper ()
227220
228- span_ctx : "ContextManager[Union[Span, StreamedSpan, None]]"
229- if span_streaming :
230- segment : "Optional[StreamedSpan]" = None
231- attributes : "Attributes" = {
232- "sentry.segment.name.source" : getattr (
233- transaction_source , "value" , transaction_source
234- ),
235- "sentry.origin" : self .span_origin ,
236- "network.protocol.name" : ty ,
237- }
238-
239- if scope .get ("client" ):
240- client_options = sentry_sdk .get_client ().options
241- if has_data_collection_enabled (client_options ):
242- if client_options ["data_collection" ]["user_info" ]:
243- sentry_scope .set_attribute (
244- SPANDATA .USER_IP_ADDRESS , _get_ip (scope )
245- )
246- elif should_send_default_pii ():
221+ span : "Optional[ContextManager[Optional[StreamedSpan]]]" = None
222+ attributes : "Attributes" = {
223+ "sentry.segment.name.source" : getattr (
224+ transaction_source , "value" , transaction_source
225+ ),
226+ "sentry.origin" : self .span_origin ,
227+ "network.protocol.name" : ty ,
228+ }
229+
230+ if scope .get ("client" ):
231+ client_options = sentry_sdk .get_client ().options
232+ if has_data_collection_enabled (client_options ):
233+ if client_options ["data_collection" ]["user_info" ]:
247234 sentry_scope .set_attribute (
248235 SPANDATA .USER_IP_ADDRESS , _get_ip (scope )
249236 )
237+ elif should_send_default_pii ():
238+ sentry_scope .set_attribute (
239+ SPANDATA .USER_IP_ADDRESS , _get_ip (scope )
240+ )
250241
251- if ty in ("http" , "websocket" ):
252- if (
253- ty == "websocket"
254- or method in self .http_methods_to_capture
255- ):
256- sentry_sdk .traces .continue_trace (_get_headers (scope ))
257-
258- Scope .set_custom_sampling_context ({"asgi_scope" : scope })
259-
260- attributes ["sentry.op" ] = f"{ ty } .server"
261- segment = sentry_sdk .traces .start_span (
262- name = transaction_name ,
263- attributes = attributes ,
264- parent_span = None ,
265- )
266- sentry_scope .get_current_scope ()._server_segment_span = segment
267- else :
268- sentry_sdk .traces .new_trace ()
242+ if ty in ("http" , "websocket" ):
243+ if ty == "websocket" or method in self .http_methods_to_capture :
244+ sentry_sdk .traces .continue_trace (_get_headers (scope ))
269245
270246 Scope .set_custom_sampling_context ({"asgi_scope" : scope })
271247
272- attributes ["sentry.op" ] = OP . HTTP_SERVER
273- segment = sentry_sdk .traces .start_span (
248+ attributes ["sentry.op" ] = f" { ty } .server"
249+ span = sentry_sdk .traces .start_span (
274250 name = transaction_name ,
275251 attributes = attributes ,
276252 parent_span = None ,
277253 )
278- sentry_scope .get_current_scope ()._server_segment_span = (
279- segment
280- )
281-
282- span_ctx = segment or nullcontext ()
283-
254+ sentry_scope .get_current_scope ()._server_segment_span = span
284255 else :
285- transaction = None
286- if ty in ("http" , "websocket" ):
287- if (
288- ty == "websocket"
289- or method in self .http_methods_to_capture
290- ):
291- transaction = continue_trace (
292- _get_headers (scope ),
293- op = "{}.server" .format (ty ),
294- name = transaction_name ,
295- source = transaction_source ,
296- origin = self .span_origin ,
297- )
298- else :
299- transaction = Transaction (
300- op = OP .HTTP_SERVER ,
301- name = transaction_name ,
302- source = transaction_source ,
303- origin = self .span_origin ,
304- )
256+ sentry_sdk .traces .new_trace ()
305257
306- span_ctx = (
307- sentry_sdk . start_transaction (
308- transaction ,
309- custom_sampling_context = { "asgi_scope" : scope },
310- )
311- if transaction is not None
312- else nullcontext ()
258+ Scope . set_custom_sampling_context ({ "asgi_scope" : scope })
259+
260+ attributes [ "sentry.op" ] = OP . HTTP_SERVER
261+ span = sentry_sdk . traces . start_span (
262+ name = transaction_name ,
263+ attributes = attributes ,
264+ parent_span = None ,
313265 )
266+ sentry_scope .get_current_scope ()._server_segment_span = span
314267
315- with span_ctx as span :
316- if isinstance ( span , StreamedSpan ) :
268+ with span or nullcontext () as span :
269+ if span is not None :
317270 for attribute , value in _get_request_attributes (
318271 scope ,
319272 root_path_in_path = self .root_path_in_path ,
@@ -331,18 +284,13 @@ async def _sentry_wrapped_send(
331284 and "status" in event
332285 )
333286 if is_http_response :
334- if isinstance (span , StreamedSpan ):
335- span .status = (
336- "error"
337- if event ["status" ] >= 400
338- else "ok"
339- )
340- span .set_attribute (
341- "http.response.status_code" ,
342- event ["status" ],
343- )
344- else :
345- span .set_http_status (event ["status" ])
287+ span .status = (
288+ "error" if event ["status" ] >= 400 else "ok"
289+ )
290+ span .set_attribute (
291+ "http.response.status_code" ,
292+ event ["status" ],
293+ )
346294
347295 return await send (event )
348296
@@ -362,7 +310,7 @@ async def _sentry_wrapped_send(
362310 reraise (* exc_info )
363311
364312 finally :
365- if isinstance ( span , StreamedSpan ) :
313+ if span is not None :
366314 already_set = (
367315 span is not None
368316 and span .name != _DEFAULT_TRANSACTION_NAME
0 commit comments