0i2JdZddlZddlZddlmZddlmZddlZddlm Z ddl m Z ddl m Z mZmZddlmZmZdd lmZdd lmZmZdd lmZmZmZmZmZmZmZdd lm Z dd l!m"Z"e"r ddl!m#Z#ddl!m$Z$ddl!m%Z%ddl!m&Z&ddl'm(Z(m)Z)edZ*dZ+dZ,ddZ-dZ.GddZ/y)ze An ASGI middleware. Based on Tom Christie's `sentry-asgi `. N)deepcopy)partial)continue_trace)OP) _get_headers_get_request_data_get_url)DEFAULT_HTTP_METHODS_TO_CAPTURE nullcontext) track_session)SOURCE_FOR_STYLETransactionSource) ContextVarevent_from_exceptionHAS_REAL_CONTEXTVARSCONTEXTVARS_ERROR_MESSAGEloggertransaction_from_function_get_installed_modules) Transaction) TYPE_CHECKING)Any)Dict)Optional)Tuple)EventHintsentry_asgi_middleware_appliedzgeneric ASGI request)endpointurlct|tjj|dd\}}tj||y)NF)typehandled)client_options mechanism)hint)r sentry_sdk get_clientoptions capture_event)excmechanism_typeeventr&s K/opt/hc_python/lib/python3.12/site-packages/sentry_sdk/integrations/asgi.py_capture_exceptionr/:sA' !,,.66)e<KE4 U.ctj|r t|dStj|rt j |St |dd}t j |S)z Try to figure out if an application object supports ASGI3. This is how uvicorn figures out the application version as well. __await____call__N)inspectisclasshasattr isfunctionasyncioiscoroutinefunctiongetattr)appcalls r._looks_like_asgi3r=Es[ssK((   C **3//sJ-**400r0cNeZdZdZddddedfdZdZd Zd Zd Z d Z d Z dZ y)SentryAsgiMiddleware)r;r3transaction_styler, span_originhttp_methods_to_captureFrasgimanualNc|ststdtz|tvrt d|dtd|dk(xr dt v}|rt jd||_||_ ||_ ||_ ||_ |t|rd }nd }|d k(r|j|_y|d k(r|j |_yy) ab Instrument an ASGI application with Sentry. Provides HTTP/websocket data to sent events and basic handling for exceptions bubbling up through the middleware. :param unsafe_context_data: Disable errors when a proper contextvars installation could not be found. We do not recommend changing this from the default. zRThe ASGI middleware for Sentry requires Python 3.7+ or the aiocontextvars package.z%Invalid value for transaction_style: z (must be in )rC starlettezThe Sentry Python SDK can now automatically support ASGI frameworks like Starlette and FastAPI. Please remove 'SentryAsgiMiddleware' from your project. See https://docs.sentry.io/platforms/python/guides/asgi/ for more information.N)r RuntimeErrorrTRANSACTION_STYLE_VALUES ValueErrorrrwarningr@r,rAr;rBr= _run_asgi3r3 _run_asgi2) selfr;unsafe_context_datar@r,rArB asgi_version0asgi_middleware_while_using_starlette_or_fastapis r.__init__zSentryAsgiMiddleware.__init___s$#+?13LM  $< <$&>@  f $ P8N8P)P 9 < NNa  "3,&'>$   %  1  OODM Q  OODMr0c0t||jS)zCapture exceptions raise in application lifespan handlers. The separate function is needed to support overriding in derived integrations that use different catching mechanisms. r+r,r/r,rPr+s r._capture_lifespan_exceptionz0SentryAsgiMiddleware._capture_lifespan_exception "c$:M:MNNr0c0t||jS)zCapture exceptions raised in incoming request handlers. The separate function is needed to support overriding in derived integrations that use different catching mechanisms. rVrWrXs r._capture_request_exceptionz/SentryAsgiMiddleware._capture_request_exceptionrZr0cfd}|S)NcJKj||dd{S7w)NrIrR_run_app)receivesendscoperPs r.innerz.SentryAsgiMiddleware._run_asgi2..inners$ugt!LL LLs #!#)rPrdres`` r.rOzSentryAsgiMiddleware._run_asgi2s M r0cHK|j|||dd{S7w)NrHr_r`)rPrdrbrcs r.rNzSentryAsgiMiddleware._run_asgi3s#]]5'4a]HHHHs " "c lKtjd}|ddk(}|s|rA |dk(r |j||d{S|j||d{Stj d t j5}t|d5|jd|_ t|j| } |j| |d} |j|j|\} } |jd d j!} d| d vrF| d k(s| |j"vrZt%t'|dj)| | | |j*n't-t.j0| | |j*rj3d| t j4d|in t7}|5 fd}|dk(rP|j|||d{cdddcdddcdddtj dS|j|||d{cdddcdddcdddtj dS727#t$r}|j ||dd}~wwxYw77a#t$r}|j9||dd}~wwxYw#1swYnxYw dddn #1swYnxYwdddn #1swYnxYwtj dy#tj dwxYww)NFr"lifespanrITrequest) session_moderC) asgi_scopemethod)http websocketrpz {}.server)opnamesourceoriginz asgi.typerl)custom_sampling_contextcK0|jddk(xrd|v}|rj|d|d{S7w)Nr"zhttp.response.startstatus)getset_http_status)r-is_http_responserc transactions r._sentry_wrapped_sendz;SentryAsgiMiddleware._run_app.._sentry_wrapped_sendsX#.#:(- &(9=R(R)>,4,=%5(8(3(C(CE(O(T-1%['8 8'8s>AAA)_asgi_middleware_appliedrxr; ExceptionrYsetr'isolation_scoper clear_breadcrumbs_namerevent_processoradd_event_processor _get_transaction_name_and_sourcer@upperrBrrformatrArr HTTP_SERVERset_tagstart_transactionr r\)rPrdrbrcrRis_recursive_asgi_middleware is_lifespanr+ sentry_scope processortytransaction_nametransaction_sourcermtransaction_contextr|r{s ` @r.razSentryAsgiMiddleware._run_apps'?'C'CE'J$Fmz1 '; $1$!0%$!???!%%$!??? !$$T*J 0++-"rs )  .+ -&&FG2./ 1 @@r0