66
77import sentry_sdk
88from sentry_sdk ._werkzeug import _get_headers , get_host
9- from sentry_sdk .api import continue_trace
109from sentry_sdk .consts import OP , SPANDATA
1110from sentry_sdk .data_collection import _apply_data_collection_filtering_to_query_string
1211from sentry_sdk .integrations ._wsgi_common import (
1514)
1615from sentry_sdk .scope import Scope , should_send_default_pii , use_isolation_scope
1716from sentry_sdk .sessions import track_session
18- from sentry_sdk .traces import SegmentNameSource , StreamedSpan
19- from sentry_sdk .tracing import Span , TransactionSource
20- from sentry_sdk .tracing_utils import has_span_streaming_enabled
17+ from sentry_sdk .traces import SegmentNameSource
2118from sentry_sdk .utils import (
2219 capture_internal_exceptions ,
2320 event_from_exception ,
4037 )
4138
4239 from sentry_sdk ._types import Event , EventProcessor
40+ from sentry_sdk .traces import StreamedSpan
4341 from sentry_sdk .utils import ExcInfo
4442
4543 WsgiResponseIter = TypeVar ("WsgiResponseIter" )
@@ -113,7 +111,6 @@ def __call__(
113111 return self .app (environ , start_response )
114112
115113 client = sentry_sdk .get_client ()
116- span_streaming = has_span_streaming_enabled (client .options )
117114
118115 _wsgi_middleware_applied .set (True )
119116 try :
@@ -130,60 +127,42 @@ def __call__(
130127
131128 method = environ .get ("REQUEST_METHOD" , "" ).upper ()
132129
133- span_ctx : "Optional[ContextManager[Union[Span, StreamedSpan, None]]]" = None
130+ span_ctx : "Optional[ContextManager[Union[StreamedSpan, None]]]" = (
131+ None
132+ )
133+
134134 if method in self .http_methods_to_capture :
135- if span_streaming :
136- sentry_sdk .traces .continue_trace (
137- dict (_get_headers (environ ))
138- )
139- Scope .set_custom_sampling_context ({"wsgi_environ" : environ })
140-
141- if has_data_collection_enabled (client .options ):
142- if client .options ["data_collection" ]["user_info" ]:
143- client_ip = get_client_ip (environ )
144- if client_ip :
145- scope .set_attribute (
146- SPANDATA .USER_IP_ADDRESS , client_ip
147- )
148- elif should_send_default_pii ():
135+ sentry_sdk .traces .continue_trace (dict (_get_headers (environ )))
136+ Scope .set_custom_sampling_context ({"wsgi_environ" : environ })
137+
138+ if has_data_collection_enabled (client .options ):
139+ if client .options ["data_collection" ]["user_info" ]:
149140 client_ip = get_client_ip (environ )
150141 if client_ip :
151142 scope .set_attribute (
152143 SPANDATA .USER_IP_ADDRESS , client_ip
153144 )
145+ elif should_send_default_pii ():
146+ client_ip = get_client_ip (environ )
147+ if client_ip :
148+ scope .set_attribute (SPANDATA .USER_IP_ADDRESS , client_ip )
149+
150+ span_ctx = sentry_sdk .traces .start_span (
151+ name = _DEFAULT_TRANSACTION_NAME ,
152+ attributes = {
153+ "sentry.segment.name.source" : SegmentNameSource .ROUTE ,
154+ "sentry.origin" : self .span_origin ,
155+ "sentry.op" : OP .HTTP_SERVER ,
156+ },
157+ parent_span = None ,
158+ )
154159
155- span_ctx = sentry_sdk .traces .start_span (
156- name = _DEFAULT_TRANSACTION_NAME ,
157- attributes = {
158- "sentry.segment.name.source" : SegmentNameSource .ROUTE ,
159- "sentry.origin" : self .span_origin ,
160- "sentry.op" : OP .HTTP_SERVER ,
161- },
162- parent_span = None ,
163- )
164- else :
165- transaction = continue_trace (
166- environ ,
167- op = OP .HTTP_SERVER ,
168- name = _DEFAULT_TRANSACTION_NAME ,
169- source = TransactionSource .ROUTE ,
170- origin = self .span_origin ,
171- )
172-
173- span_ctx = sentry_sdk .start_transaction (
174- transaction ,
175- custom_sampling_context = {"wsgi_environ" : environ },
176- )
177-
178- span_ctx = span_ctx or nullcontext ()
179-
180- with span_ctx as span :
181- if isinstance (span , StreamedSpan ):
182- with capture_internal_exceptions ():
183- for attr , value in _get_request_attributes (
184- environ , self .use_x_forwarded_for
185- ).items ():
186- span .set_attribute (attr , value )
160+ with span_ctx or nullcontext () as span :
161+ with capture_internal_exceptions ():
162+ for attr , value in _get_request_attributes (
163+ environ , self .use_x_forwarded_for
164+ ).items ():
165+ span .set_attribute (attr , value )
187166
188167 try :
189168 response = self .app (
@@ -223,19 +202,16 @@ def __call__(
223202
224203def _sentry_start_response (
225204 old_start_response : "StartResponse" ,
226- span : "Optional[Union[Span, StreamedSpan] ]" ,
205+ span : "Optional[StreamedSpan]" ,
227206 status : str ,
228207 response_headers : "WsgiResponseHeaders" ,
229208 exc_info : "Optional[WsgiExcInfo]" = None ,
230209) -> "WsgiResponseIter" : # type: ignore[type-var]
231210 with capture_internal_exceptions ():
232211 status_int = int (status .split (" " , 1 )[0 ])
233212 if span is not None :
234- if isinstance (span , StreamedSpan ):
235- span .status = "error" if status_int >= 400 else "ok"
236- span .set_attribute ("http.response.status_code" , status_int )
237- else :
238- span .set_http_status (status_int )
213+ span .status = "error" if status_int >= 400 else "ok"
214+ span .set_attribute ("http.response.status_code" , status_int )
239215
240216 if exc_info is None :
241217 # The Django Rest Framework WSGI test client, and likely other
0 commit comments