
    lh1                     J   d Z ddlZddlZddlmZ ddlmZ ddlZddlm	Z	 ddl
mZ ddlmZmZmZ ddlmZmZ dd	lmZ dd
lmZmZ ddlmZmZmZmZmZmZmZ ddlm Z  ddl!m"Z" e"r ddl!m#Z# ddl!m$Z$ ddl!m%Z% ddl!m&Z& ddl'm(Z(m)Z)  ed      Z*dZ+dZ,ddZ-d Z. G d d      Z/y)ze
An ASGI middleware.

Based on Tom Christie's `sentry-asgi <https://github.com/encode/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_STYLETransactionSource)
ContextVarevent_from_exceptionHAS_REAL_CONTEXTVARSCONTEXTVARS_ERROR_MESSAGEloggertransaction_from_function_get_installed_modules)Transaction)TYPE_CHECKING)Any)Dict)Optional)Tuple)EventHintsentry_asgi_middleware_appliedzgeneric ASGI request)endpointurlc                     t        | t        j                         j                  |dd      \  }}t        j                  ||       y )NF)typehandled)client_options	mechanism)hint)r   
sentry_sdk
get_clientoptionscapture_event)excmechanism_typeeventr&   s       n/var/www/pru.catia.catastroantioquia-mas.com/tasa/lib/python3.12/site-packages/sentry_sdk/integrations/asgi.py_capture_exceptionr/   :   sA     '!,,.66)e<KE4
 U.    c                     t        j                  |       rt        | d      S t        j                  |       rt	        j
                  |       S t        | dd      }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)inspectisclasshasattr
isfunctionasyncioiscoroutinefunctiongetattr)appcalls     r.   _looks_like_asgi3r=   E   s[     ssK((			C	 **3//sJ-**400r0   c                   N    e Zd ZdZddddedfdZd Zd	 Zd
 Zd Z	d Z
d Zd Zy)SentryAsgiMiddleware)r;   r3   transaction_styler,   span_originhttp_methods_to_captureFr   asgimanualNc                    |st         st        dt        z         |t        vrt	        d|dt        d      |dk(  xr dt               v }|rt        j                  d       || _        || _	        || _
        || _        || _        |t        |      rd	}nd
}|d	k(  r| j                  | _        y|d
k(  r| j                   | _        yy)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   RuntimeErrorr   TRANSACTION_STYLE_VALUES
ValueErrorr   r   warningr@   r,   rA   r;   rB   r=   
_run_asgi3r3   
_run_asgi2)	selfr;   unsafe_context_datar@   r,   rA   rB   asgi_version0asgi_middleware_while_using_starlette_or_fastapis	            r.   __init__zSentryAsgiMiddleware.__init___   s    $ #+? 13LM  $<<$&>@  f$P8N8P)P 	9 <NNa "3,&'>$ %  1 OODMQ OODM r0   c                 0    t        || j                        S )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,   rP   r+   s     r.   _capture_lifespan_exceptionz0SentryAsgiMiddleware._capture_lifespan_exception        "c$:M:MNNr0   c                 0    t        || j                        S )zCapture exceptions raised in incoming request handlers.

        The separate function is needed to support overriding in derived integrations that use different catching mechanisms.
        rV   rW   rX   s     r.   _capture_request_exceptionz/SentryAsgiMiddleware._capture_request_exception   rZ   r0   c                       fd}|S )Nc                 J   K   j                  | |d       d {   S 7 w)NrI   rR   _run_app)receivesendscoperP   s     r.   innerz.SentryAsgiMiddleware._run_asgi2.<locals>.inner   s$     ugt!LLLLs   #!# )rP   rd   re   s   `` r.   rO   zSentryAsgiMiddleware._run_asgi2   s    	M r0   c                 H   K   | j                  |||d       d {   S 7 w)NrH   r_   r`   )rP   rd   rb   rc   s       r.   rN   zSentryAsgiMiddleware._run_asgi3   s#     ]]5'4a]HHHHs   " "c           	      h  K   t         j                  d      }|d   dk(  }|s|rA	 |dk(  r  | j                  |      |       d {   S | j                  ||       d {   S t         j                  d       	 t        j                         5 }t        |d      5  |j                          d|_
        t        | j                  |	      }	|j                  |	       |d   }
| j                  | j                  |      \  }}|j                  d
d      j!                         }d |
dv rF|
dk(  s|| j"                  v rZt%        t'        |      dj)                  |
      ||| j*                        n't-        t.        j0                  ||| j*                        rj3                  d|
       t        j4                  d|i      n	t7               5  	 fd}|dk(  rP | j                  |      ||       d {   cd d d        cd d d        cd d d        t         j                  d       S | j                  |||       d {   cd d d        cd d d        cd d d        t         j                  d       S 7 07 # t        $ r}| j	                  |       |d d }~ww xY w7 7 a# t        $ r}| j9                  |       |d d }~ww xY w# 1 sw Y   nxY w	 d d d        n# 1 sw Y   nxY wd d d        n# 1 sw Y   nxY wt         j                  d       y # t         j                  d       w xY ww)NFr"   lifespanrI   Trequest)session_moderC   )
asgi_scopemethod )http	websocketrp   z	{}.server)opnamesourceoriginz	asgi.typerl   )custom_sampling_contextc                    K   0| j                  d      dk(  xr d| v }|rj                  | d           |        d {   S 7 w)Nr"   zhttp.response.startstatus)getset_http_status)r-   is_http_responserc   transactions     r.   _sentry_wrapped_sendz;SentryAsgiMiddleware._run_app.<locals>._sentry_wrapped_send   sX     #.#:(-		&(9=R(R )>,4,= %5 (8(3(C(CE(O(T-1%['8 8'8s   >AAA)_asgi_middleware_appliedrx   r;   	ExceptionrY   setr'   isolation_scoper   clear_breadcrumbs_namer   event_processoradd_event_processor _get_transaction_name_and_sourcer@   upperrB   r   r   formatrA   r   r   HTTP_SERVERset_tagstart_transactionr   r\   )rP   rd   rb   rc   rR   is_recursive_asgi_middlewareis_lifespanr+   sentry_scope	processortytransaction_nametransaction_sourcerm   r|   r{   s      `           @r.   ra   zSentryAsgiMiddleware._run_app   sI    '?'C'CE'J$Fmz1';$1$!0%$!???!%%$!??? 	!$$T*I	0++- F0"<iH E0 224)/L& '(<(< OI 44Y?vB ==..(* #YYx4::<F"&K22,$:V:V0V*8 ,U 3#.#5#5b#9%5'9'+'7'7+K '2!~~!1#5#'#3#3	' ##++K< '2	 #44'5A54I
 )] 00
9  ,q0-<TXXe_$+-A." ("/ 0  0KE0 E0F0 F0P %((/ .2XX$)74H." ("7 0  0KE0 E0F0 F0P %((/e @? $005t#$F("("  ) 0 ;;C@"%4/0= 0  0  0KE0 E0 E0F0 F0 F0P %((/$((/s+  "L2 I= I7I= L2I= "I:#I= &L2=L K6D0K!K&J&7J"
8J&;	K!	K6	L L2,J&J$
J&	K!	K6	L !L27I= :I= =	JJJL2"J&$J&&	K	/K	K	KKK!	K6!K*	&K6-	L 6K?;L L2L//L2c                    |j                  di       }|j                  t        |             t        |      |d<   |j                  d      }|j                  d      xs i j                  d      }|d uxr< |t        k7  xr1 |t
        j                  t
        j                  t
        j                  fv }|s+| j                  | j                  |      \  }}	||d<   d|	i|d<   |S )Nrj   r{   transaction_infors   )rx   updater   r   _DEFAULT_TRANSACTION_NAMEr   	COMPONENTROUTECUSTOMr   r@   )
rP   r-   r&   rl   request_datar{   r   already_setrr   rs   s
             r.   r   z$SentryAsgiMiddleware.event_processor  s    yyB/-j9:#L1i ii.#ii(:;ArFFxPt# 88"!++!''!(( 	 @@&&
LD& $(E- )16(:E$%r0   c                    d }t         |   }|j                  d      }|dk(  rI|j                  d      }|rt        |      xs d}nut        ||dk(  rdndd       }t        j
                  }nO|dk(  rJ|j                  d      }|rt        |d	d       }|(|}n%t        ||dk(  rdndd       }t        j
                  }|t        }t        j                  }||fS ||fS )
Nr"   r   rn   ro   ws)hostr    routepath)	r   rx   r   r	   r   URLr:   r   r   )	rP   r@   rl   rr   rs   r   r   r   r   s	            r.   r   z5SentryAsgiMiddleware._get_transaction_name_and_source2  s    !"34^^F#
*!~~j1H 0:@b
bFlFSWX*..%' NN7+Eufd3#D
bFlFSWX*..<,D&,,F<V|r0   )__name__
__module____qualname__	__slots__r
   rT   rY   r\   rO   rN   ra   r   r   rf   r0   r.   r?   r?   U   sL    I "$ ?8,tOOIY0vD"r0   r?   )rC   )0__doc__r8   r4   copyr   	functoolsr   r'   sentry_sdk.apir   sentry_sdk.constsr   $sentry_sdk.integrations._asgi_commonr   r   r	   $sentry_sdk.integrations._wsgi_commonr
   r   sentry_sdk.sessionsr   sentry_sdk.tracingr   r   sentry_sdk.utilsr   r   r   r   r   r   r   r   typingr   r   r   r   r   sentry_sdk._typesr   r   r}   r   rK   r/   r=   r?   rf   r0   r.   <module>r      s         )   
 .   +  - &&FG 2 . /1  r0   