o
    h1                     @   s@  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"rd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dZ-dd Z.G dd dZ/dS )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urlasgic                 C   s0   t | t j|ddd\}}tj||d d S )NF)typehandled)client_options	mechanism)hint)r   
sentry_sdk
get_clientoptionscapture_event)excmechanism_typeeventr%    r-   ^/var/www/html/pro-man-master/venv/lib/python3.10/site-packages/sentry_sdk/integrations/asgi.py_capture_exception:   s   
r/   c                 C   s>   t | r
t| dS t | rt| S t| dd}t|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callr-   r-   r.   _looks_like_asgi3E   s   




r;   c                   @   s^   e Zd ZdZddddedfddZd	d
 Zdd Zdd Zdd Z	dd Z
dd Zdd ZdS )SentryAsgiMiddleware)r9   r1   transaction_styler+   span_originhttp_methods_to_captureFr   r    manualNc           	      C   s   |s
t s
tdt |tvrtd|tf |dkodt v }|r&td || _|| _	|| _
|| _|| _|du rBt|r@d}nd}|dkrL| j| _dS |dkrV| j| _dS dS )	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.z7Invalid value for transaction_style: %s (must be in %s)r    	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+   r>   r9   r?   r;   
_run_asgi3r1   
_run_asgi2)	selfr9   unsafe_context_datar=   r+   r>   r?   asgi_version0asgi_middleware_while_using_starlette_or_fastapir-   r-   r.   __init___   s@   zSentryAsgiMiddleware.__init__c                 C      t || jd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+   rJ   r*   r-   r-   r.   _capture_lifespan_exception      z0SentryAsgiMiddleware._capture_lifespan_exceptionc                 C   rO   )zCapture exceptions raised in incoming request handlers.

        The separate function is needed to support overriding in derived integrations that use different catching mechanisms.
        rP   rQ   rR   r-   r-   r.   _capture_request_exception   rT   z/SentryAsgiMiddleware._capture_request_exceptionc                    s    fdd}|S )Nc                    s   j  | |ddI d H S )NrC   rL   _run_app)receivesendscoperJ   r-   r.   inner      z.SentryAsgiMiddleware._run_asgi2.<locals>.innerr-   )rJ   r\   r]   r-   r[   r.   rI      s   zSentryAsgiMiddleware._run_asgi2c                    s   | j |||ddI d H S )NrB   rV   rW   )rJ   r\   rY   rZ   r-   r-   r.   rH      r^   zSentryAsgiMiddleware._run_asgi3c                    s  t d}|d dk}|s|r@z|dkr!| || I d H W S | || I d H W S  ty? } z| | |d d }~ww t d zt  }t|dd |	  d|_
t| j|d	}	||	 |d }
| | j|\}}|d
d }d |
dv r|
dks|| jv rtt|d|
||| jdn
ttj||| jdrd|
 d urtjd|idnt o zX fdd}|dkr| |||I d H W W  d    W  d    W  d    W t d S | |||I d H W W  d    W  d    W  d    W t d S  ty+ } z| | |d d }~ww 1 s0w   Y  W d    n	1 s@w   Y  W d    n1 sPw   Y  W t d d S W t d d S t d w )NFr!   lifespanrC   Trequest)session_moder    )
asgi_scopemethod )http	websocketrf   z	{}.server)opnamesourceoriginz	asgi.typerb   )custom_sampling_contextc                    s@   d ur|  ddkod| v }|r| d   | I d H S )Nr!   zhttp.response.startstatus)getset_http_status)r,   is_http_responserZ   transactionr-   r.   _sentry_wrapped_send   s   z;SentryAsgiMiddleware._run_app.<locals>._sentry_wrapped_send)_asgi_middleware_appliedrm   r9   	ExceptionrS   setr&   isolation_scoper   clear_breadcrumbs_namer   event_processoradd_event_processor _get_transaction_name_and_sourcer=   upperr?   r   r   formatr>   r   r   HTTP_SERVERset_tagstart_transactionr
   rU   )rJ   r\   rY   rZ   rL   is_recursive_asgi_middlewareis_lifespanr*   sentry_scope	processortytransaction_nametransaction_sourcerc   rr   r-   rp   r.   rX      s   




H
H
 HHzSentryAsgiMiddleware._run_appc           
      C   s   | di }|t| t||d< | d}| dpi  d}|d uo3|tko3|tjtjtjfv }|sI| 	| j
|\}}	||d< d|	i|d< |S )Nr`   rq   transaction_infori   )rm   updater   r   _DEFAULT_TRANSACTION_NAMEr   	COMPONENTROUTECUSTOMr{   r=   )
rJ   r,   r%   rb   request_datarq   r   already_setrh   ri   r-   r-   r.   ry     s,   

z$SentryAsgiMiddleware.event_processorc           	      C   s   d }t | }|d}|dkr.|d}|rt|pd}n9t||dkr%dndd d}tj}n(|dkrV|d}|rFt|d	d }|d urE|}nt||dkrNdndd d}tj}|d u rct}tj}||fS ||fS )
Nr!   r   rd   re   ws)hostr   routepath)	r   rm   r   r   r   URLr8   r   r   )	rJ   r=   rb   rh   ri   r   r   r   r   r-   r-   r.   r{   2  s.   


z5SentryAsgiMiddleware._get_transaction_name_and_source)__name__
__module____qualname__	__slots__r	   rN   rS   rU   rI   rH   rX   ry   r{   r-   r-   r-   r.   r<   U   s     
:["r<   )r    )0__doc__r6   r2   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   rs   r   rE   r/   r;   r<   r-   r-   r-   r.   <module>   s6    $	
