o
    0 i 2                     @   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HintZsentry_asgi_middleware_appliedzgeneric ASGI request)endpointurlasgic                 C   s0   t | t j|ddd\}}tj||d d S )NF)typeZhandled)Zclient_optionsZ	mechanism)hint)r   
sentry_sdkZ
get_clientoptionsZcapture_event)excmechanism_typeeventr!    r'   h/home/app/PaddleOCR-VL-test/.venv_paddleocr/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   




r5   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)r3   r+   transaction_styler%   span_originhttp_methods_to_captureFr   r   Z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   Z	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   warningr7   r%   r8   r3   r9   r5   
_run_asgi3r+   
_run_asgi2)	selfr3   Zunsafe_context_datar7   r%   r8   r9   asgi_versionZ0asgi_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%   rB   r$   r'   r'   r(   _capture_lifespan_exception      z0SentryAsgiMiddleware._capture_lifespan_exceptionc                 C   rE   )zCapture exceptions raised in incoming request handlers.

        The separate function is needed to support overriding in derived integrations that use different catching mechanisms.
        rF   rG   rH   r'   r'   r(   _capture_request_exception   rJ   z/SentryAsgiMiddleware._capture_request_exceptionc                    s    fdd}|S )Nc                    s   j  | |ddI d H S )Nr;   rC   _run_app)receivesendscoperB   r'   r(   inner      z.SentryAsgiMiddleware._run_asgi2.<locals>.innerr'   )rB   rR   rS   r'   rQ   r(   rA      s   zSentryAsgiMiddleware._run_asgi2c                    s   | j |||ddI d H S )Nr:   rL   rM   )rB   rR   rO   rP   r'   r'   r(   r@      rT   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 s2w   Y  W d    n	1 sBw   Y  W d    n1 sRw   Y  W t d d S W t d d S t d w )NFr    Zlifespanr;   Trequest)Zsession_moder   )
asgi_scopemethod )http	websocketrZ   z	{}.server)opnamesourceoriginz	asgi.typerV   )Z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)getZset_http_status)r&   Zis_http_responserP   transactionr'   r(   _sentry_wrapped_send   s   z;SentryAsgiMiddleware._run_app.<locals>._sentry_wrapped_send)_asgi_middleware_appliedr`   r3   	ExceptionrI   setr"   Zisolation_scoper   Zclear_breadcrumbs_namer   event_processorZadd_event_processor _get_transaction_name_and_sourcer7   upperr9   r   r   formatr8   r   r   ZHTTP_SERVERZset_tagZstart_transactionr
   rK   )rB   rR   rO   rP   rC   Zis_recursive_asgi_middlewareZis_lifespanr$   Zsentry_scope	processortyZtransaction_nametransaction_sourcerW   Ztransaction_contextrc   r'   ra   r(   rN      s   




I
I
 IIz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 )NrU   rb   Ztransaction_infor]   )r`   updater   r   _DEFAULT_TRANSACTION_NAMEr   Z	COMPONENTROUTEZCUSTOMri   r7   )
rB   r&   r!   rV   Zrequest_datarb   rn   Zalready_setr\   r]   r'   r'   r(   rh     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   rX   rY   ws)hostr   routepath)	r   r`   r   r   r   URLr2   rp   rq   )	rB   r7   rV   r\   r]   rm   r   rt   ru   r'   r'   r(   ri   3  s.   


z5SentryAsgiMiddleware._get_transaction_name_and_source)__name__
__module____qualname__	__slots__r	   rD   rI   rK   rA   r@   rN   rh   ri   r'   r'   r'   r(   r6   U   s     
:\"r6   )r   )0__doc__r0   r,   copyr   	functoolsr   r"   Zsentry_sdk.apir   Zsentry_sdk.constsr   Z$sentry_sdk.integrations._asgi_commonr   r   r   Z$sentry_sdk.integrations._wsgi_commonr	   r
   Zsentry_sdk.sessionsr   Zsentry_sdk.tracingr   r   Zsentry_sdk.utilsr   r   r   r   r   r   r   r   typingr   r   r   r   r   Zsentry_sdk._typesr   r   rd   rp   r=   r)   r5   r6   r'   r'   r'   r(   <module>   s6    $	
