o
    Zha.                     @   sH  d Z ddlZddlZ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 ddlmZ dd	lmZmZmZ dd
lmZ ddlmZmZ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" 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( 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Z0dS )ze
An ASGI middleware.

Based on Tom Christie's `sentry-asgi <https://github.com/encode/sentry-asgi>`.
    N)deepcopy)partial)TYPE_CHECKING)continue_trace)OP)Hub)_get_headers_get_request_data_get_url)auto_session_tracking)SOURCE_FOR_STYLETRANSACTION_SOURCE_ROUTETRANSACTION_SOURCE_URLTRANSACTION_SOURCE_COMPONENT)
ContextVarevent_from_exceptionHAS_REAL_CONTEXTVARSCONTEXTVARS_ERROR_MESSAGEloggertransaction_from_function_get_installed_modules)Transaction)Any)Callable)Dict)Optional)Tuple)EventHintZsentry_asgi_middleware_appliedzgeneric ASGI request)endpointurlasgic                 C   s<   | j d urt|| j j|ddd\}}| j||d d S d S )NF)typeZhandled)Zclient_optionsZ	mechanism)hint)clientr   optionsZcapture_event)hubexcmechanism_typeeventr#    r*   S/var/www/html/lang_env/lib/python3.10/site-packages/sentry_sdk/integrations/asgi.py_capture_exception9   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_asgi3F   s   




r8   c                   @   sH   e Zd ZdZ			dddZdd Zd	d
 Zdd Zdd Zdd Z	dS )SentryAsgiMiddleware)r6   r.   transaction_styler(   Fr   r!   c                 C   s~   |s
t s
tdt |tvrtd|tf |dkodt v }|r&td || _|| _	|| _
t|r9| j| _dS | j| _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   warningr:   r(   r6   r8   
_run_asgi3r.   
_run_asgi2)selfr6   Zunsafe_context_datar:   r(   Z0asgi_middleware_while_using_starlette_or_fastapir*   r*   r+   __init__Y   s0   zSentryAsgiMiddleware.__init__c                    s    fdd}|S )Nc                    s   j  | |ddI d H S )N   asgi_version_run_app)receivesendscoperA   r*   r+   inner      z.SentryAsgiMiddleware._run_asgi2.<locals>.innerr*   )rA   rK   rL   r*   rJ   r+   r@      s   zSentryAsgiMiddleware._run_asgi2c                    s   | j |||ddI d H S )N   rD   rF   )rA   rK   rH   rI   r*   r*   r+   r?      rM   zSentryAsgiMiddleware._run_asgi3c                    s
  t d}|d dk}|s|rDz|dkr!| || I d H W S | || I d H W S  tyC } zttj|| jd |d d }~ww t d z4ttj}t	|dd | |
 }	|	  d	|	_t| j|d
}
|	|
 W d    n1 s}w   Y  |d }| | j|\}}|dv rtt|d|||dtd nttj||dtd d| tdjj |jd|idy td zY 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yE } zt||| jd |d d }~ww 1 sJw   Y  W d    n	1 sZw   Y  W d    n1 sjw   Y  W t d d S W t d d S t d w )NFr"   ZlifespanrC   )r(   Trequest)Zsession_moder!   )
asgi_scope)httpZ	websocketz	{}.server)opnamesourcez1[ASGI] Created transaction (continuing trace): %sz$[ASGI] Created transaction (new): %sz	asgi.typezB[ASGI] Set transaction name and source on transaction: '%s' / '%s'rP   )Zcustom_sampling_contextz[ASGI] Started transaction: %sc                    s@   |  ddkod uod| v }|r| d   | I d H S )Nr"   zhttp.response.startstatus)getZset_http_status)r)   Zis_http_responserI   transactionr*   r+   _sentry_wrapped_send   s   z;SentryAsgiMiddleware._run_app.<locals>._sentry_wrapped_send)_asgi_middleware_appliedrV   r6   	Exceptionr,   r   currentr(   setr   Zconfigure_scopeZclear_breadcrumbs_namer   event_processorZadd_event_processor _get_transaction_name_and_sourcer:   r   r   formatr   debugr   r   ZHTTP_SERVERZset_tagrS   rT   Zstart_transaction)rA   rK   rH   rI   rE   Zis_recursive_asgi_middlewareZis_lifespanr'   r&   Zsentry_scope	processortyZtransaction_nameZtransaction_sourcerY   r*   rW   r+   rG      s   





M
M MMzSentryAsgiMiddleware._run_appc                 C   s   | di }|t| t||d< |d tko#|d  dttfv }|sF| | j|\}}||d< d|i|d< t	
d|d |d d  |S )NrO   rX   Ztransaction_inforT   zF[ASGI] Set transaction name and source in event_processor: '%s' / '%s')rV   updater	   r   _DEFAULT_TRANSACTION_NAMEr   r   r`   r:   r   rb   )rA   r)   r#   rP   Zrequest_dataZalready_setrS   rT   r*   r*   r+   r_      s.   
z$SentryAsgiMiddleware.event_processorc           	      C   s   d }t | }|d}|dkr-|d}|rt|pd}n7t||dkr%dndd d}t}n'|dkrT|d}|rEt|d	d }|d urD|}nt||dkrMdndd d}t}|d u r`t}t}||fS ||fS )
Nr"   r    rQ   ws)hostr    routepath)r   rV   r   r
   r   r5   rf   r   )	rA   r:   rP   rS   rT   rd   r   rj   rk   r*   r*   r+   r`     s.   


z5SentryAsgiMiddleware._get_transaction_name_and_sourceN)Fr   r!   )
__name__
__module____qualname__	__slots__rB   r@   r?   rG   r_   r`   r*   r*   r*   r+   r9   V   s    
/a"r9   )r!   )1__doc__r3   r/   copyr   Zsentry_sdk._functoolsr   Zsentry_sdk._typesr   Zsentry_sdk.apir   Zsentry_sdk.constsr   Zsentry_sdk.hubr   Z$sentry_sdk.integrations._asgi_commonr   r	   r
   Zsentry_sdk.sessionsr   Zsentry_sdk.tracingr   r   r   r   Zsentry_sdk.utilsr   r   r   r   r   r   r   r   typingr   r   r   r   r   r   r   rZ   rf   r<   r,   r8   r9   r*   r*   r*   r+   <module>   s6    $	
