1212import sentry_sdk
1313from sentry_sdk .api import continue_trace
1414from sentry_sdk .consts import OP
15-
1615from sentry_sdk .integrations ._asgi_common import (
1716 _get_headers ,
1817 _get_request_data ,
4241
4342if TYPE_CHECKING :
4443 from typing import Any
45- from typing import Callable
4644 from typing import Dict
4745 from typing import Optional
4846 from typing import Tuple
@@ -102,6 +100,7 @@ def __init__(
102100 mechanism_type = "asgi" , # type: str
103101 span_origin = "manual" , # type: str
104102 http_methods_to_capture = DEFAULT_HTTP_METHODS_TO_CAPTURE , # type: Tuple[str, ...]
103+ asgi_version = None , # type: Optional[int]
105104 ):
106105 # type: (...) -> None
107106 """
@@ -140,10 +139,16 @@ def __init__(
140139 self .app = app
141140 self .http_methods_to_capture = http_methods_to_capture
142141
143- if _looks_like_asgi3 (app ):
144- self .__call__ = self ._run_asgi3 # type: Callable[..., Any]
145- else :
146- self .__call__ = self ._run_asgi2
142+ if asgi_version is None :
143+ if _looks_like_asgi3 (app ):
144+ asgi_version = 3
145+ else :
146+ asgi_version = 2
147+
148+ if asgi_version == 3 :
149+ self .__call__ = self ._run_asgi3
150+ elif asgi_version == 2 :
151+ self .__call__ = self ._run_asgi2 # type: ignore
147152
148153 def _capture_lifespan_exception (self , exc ):
149154 # type: (Exception) -> None
@@ -217,28 +222,16 @@ async def _run_app(self, scope, receive, send, asgi_version):
217222 source = transaction_source ,
218223 origin = self .span_origin ,
219224 )
220- logger .debug (
221- "[ASGI] Created transaction (continuing trace): %s" ,
222- transaction ,
223- )
224225 else :
225226 transaction = Transaction (
226227 op = OP .HTTP_SERVER ,
227228 name = transaction_name ,
228229 source = transaction_source ,
229230 origin = self .span_origin ,
230231 )
231- logger .debug (
232- "[ASGI] Created transaction (new): %s" , transaction
233- )
234232
235233 if transaction :
236234 transaction .set_tag ("asgi.type" , ty )
237- logger .debug (
238- "[ASGI] Set transaction name and source on transaction: '%s' / '%s'" ,
239- transaction .name ,
240- transaction .source ,
241- )
242235
243236 with (
244237 sentry_sdk .start_transaction (
@@ -248,7 +241,6 @@ async def _run_app(self, scope, receive, send, asgi_version):
248241 if transaction is not None
249242 else nullcontext ()
250243 ):
251- logger .debug ("[ASGI] Started transaction: %s" , transaction )
252244 try :
253245
254246 async def _sentry_wrapped_send (event ):
@@ -303,12 +295,6 @@ def event_processor(self, event, hint, asgi_scope):
303295 event ["transaction" ] = name
304296 event ["transaction_info" ] = {"source" : source }
305297
306- logger .debug (
307- "[ASGI] Set transaction name and source in event_processor: '%s' / '%s'" ,
308- event ["transaction" ],
309- event ["transaction_info" ]["source" ],
310- )
311-
312298 return event
313299
314300 # Helper functions.
0 commit comments