o
    i1                     @   sX  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de-ddfddZ.ddde/fddZ0G d d! d!Z1d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asgiexcr   mechanism_typereturnc                 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)r!   r"   eventr(    r.   Y/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/sentry_sdk/integrations/asgi.py_capture_exception:   s   
r0   appc                 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)r1   callr.   r.   r/   _looks_like_asgi3C   s   




r<   c                   @   s   e Zd ZdZddddedfddd	ed
edededdddddfddZdeddfddZ	deddfddZ
d.ddZd/ddZdddddddeddf
d d!Z	"	#	$	%	&			'd0d(d)Zd*d d
ed&ddd+fd,d-ZdS )1SentryAsgiMiddleware)r1   r3   transaction_styler"   span_originhttp_methods_to_captureFr   r    manualNr1   r   unsafe_context_datar>   r"   r?   r@   zTuple[str, ...]asgi_versionzOptional[int]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 || _|| _	|| _
|| _|| _|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?   r1   r@   r<   
_run_asgi3r3   
_run_asgi2)	selfr1   rB   r>   r"   r?   r@   rC   0asgi_middleware_while_using_starlette_or_fastapir.   r.   r/   __init__\   s@   zSentryAsgiMiddleware.__init__r!   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"   r0   r"   rM   r!   r.   r.   r/   _capture_lifespan_exception      z0SentryAsgiMiddleware._capture_lifespan_exceptionc                 C   rP   )zCapture exceptions raised in incoming request handlers.

        The separate function is needed to support overriding in derived integrations that use different catching mechanisms.
        rQ   rR   rS   r.   r.   r/   _capture_request_exception   rU   z/SentryAsgiMiddleware._capture_request_exceptionscopec                    s   d fdd}|S )Nreceiver   sendr#   c                    s   j  | |ddI d H S )NrF   rC   _run_app)rX   rY   rW   rM   r.   r/   inner      z.SentryAsgiMiddleware._run_asgi2.<locals>.inner)rX   r   rY   r   r#   r   r.   )rM   rW   r^   r.   r]   r/   rL      s   zSentryAsgiMiddleware._run_asgi2rX   rY   c                    s   | j |||ddI d H S )NrE   rZ   r[   )rM   rW   rX   rY   r.   r.   r/   rK      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 }|t z]				d 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y2 } z| | |d d }~ww 1 s7w   Y  W d    n	1 sGw   Y  W d    n1 sWw   Y  W t d d S W t d d S t d w )NFr$   lifespanrF   Trequest)session_moder    )
asgi_scopemethod )http	websocketrg   z	{}.server)opnamesourceoriginz	asgi.typerc   )custom_sampling_contextr-   Dict[str, Any]r#   r   c                    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rY   transactionr.   r/   _sentry_wrapped_send   s   z;SentryAsgiMiddleware._run_app.<locals>._sentry_wrapped_send)r-   rm   r#   r   )_asgi_middleware_appliedro   r1   	ExceptionrT   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
   rV   )rM   rW   rX   rY   rC   is_recursive_asgi_middlewareis_lifespanr!   sentry_scope	processortytransaction_nametransaction_sourcerd   transaction_contextrt   r.   rr   r/   r\      s   




J
J
 JJzSentryAsgiMiddleware._run_appr-   r   r(   r   rc   Optional[Event]c           
      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 )Nra   rs   transaction_inforj   )ro   updater   r   _DEFAULT_TRANSACTION_NAMEr   	COMPONENTROUTECUSTOMr}   r>   )
rM   r-   r(   rc   request_datars   r   already_setri   rj   r.   r.   r/   r{   
  s,   

z$SentryAsgiMiddleware.event_processorrM   zTuple[str, str]c           	      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   re   rf   ws)hostr   routepath)	r   ro   r   r   r   URLr:   r   r   )	rM   r>   rc   ri   rj   r   r   r   r   r.   r.   r/   r}   -  s.   


z5SentryAsgiMiddleware._get_transaction_name_and_source)rW   r   r#   r   )rW   r   rX   r   rY   r   r#   r   )r-   r   r(   r   rc   r   r#   r   )__name__
__module____qualname__	__slots__r	   boolstrrO   rv   rT   rV   rL   rK   intr\   r{   r}   r.   r.   r.   r/   r=   R   st    	
9


^
#r=   )r    )2__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   ru   r   rH   r   r0   r   r<   r=   r.   r.   r.   r/   <module>   s6    $		