o
    aiA                     @   s  d Z ddl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 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"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/ ddl0m1Z1m2Z2m3Z3 ddlm4Z4 edZ5dZ6dZ7d&ddde8ddfdd Z9d!dde:f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)StreamedSpan)SOURCE_FOR_STYLETransactionTransactionSource)has_span_streaming_enabled)	
ContextVarevent_from_exceptionHAS_REAL_CONTEXTVARSCONTEXTVARS_ERROR_MESSAGEloggertransaction_from_function_get_installed_modulesreraisecapture_internal_exceptions)TYPE_CHECKING)Any)ContextManager)Dict)Optional)Tuple)Union)
AttributesEventHint)Span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*   eventr0    r6   Z/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/sentry_sdk/integrations/asgi.py_capture_exceptionB   s   
r8   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)r9   callr6   r6   r7   _looks_like_asgi3K   s   




rD   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)r9   r;   transaction_styler*   span_originhttp_methods_to_captureFr&   r(   manualNr9   r   unsafe_context_datarF   r*   rG   rH   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   warningrF   r*   rG   r9   rH   rD   
_run_asgi3r;   
_run_asgi2)	selfr9   rJ   rF   r*   rG   rH   rK   0asgi_middleware_while_using_starlette_or_fastapir6   r6   r7   __init__d   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*   r8   r*   rU   r)   r6   r6   r7   _capture_lifespan_exception      z0SentryAsgiMiddleware._capture_lifespan_exceptionc                 C   rX   )zCapture exceptions raised in incoming request handlers.

        The separate function is needed to support overriding in derived integrations that use different catching mechanisms.
        rY   rZ   r[   r6   r6   r7   _capture_request_exception   r]   z/SentryAsgiMiddleware._capture_request_exceptionscopec                    s   d fdd}|S )Nreceiver   sendr+   c                    s   j  | |ddI d H S )NrN   rK   _run_app)r`   ra   r_   rU   r6   r7   inner      z.SentryAsgiMiddleware._run_asgi2.<locals>.inner)r`   r   ra   r   r+   r   r6   )rU   r_   rf   r6   re   r7   rT      s   zSentryAsgiMiddleware._run_asgi2r`   ra   c                    s   | j |||ddI d H S )NrM   rb   rc   )rU   r_   r`   ra   r6   r6   r7   rS      rg   zSentryAsgiMiddleware._run_asgi3c                    s  t d}|d dk}|s|ruz|dkr!| || I d H W S | || I d H W S  tyt } z=t jdi dd}|rJ| | |d t	 }	t
  | | W d    n1 saw   Y  t|	  W Y d }~nd }~ww t }
t|
j}t d zt }t|dd	 |  d
|_t| j|d}|| |d }| | j|\}}|dd }|rd }t|d|| j|d}|dv r|dks|| jv rtjt| |d|i | d|d< tjj||d}ntj   |d|i t!j"|d< tjj||d}|pt# }nFd }|dv r9|dks)|| jv r8tt|d$|||| jd}n
t%t!j"||| jd}|rL|&d| |d urZtj'|d|idn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y
 } z?t jdi dd}|r| (| |d t	 }	t
  | (| W d    n	1 sw   Y  t|	  W Y d }~nd }~ww W d    n	1 sw   Y  W d    n	1 s&w   Y  W d    n1 s6w   Y  W t d d S W t d d S t d w )!NFr,   lifespanrN   _experiments suppress_asgi_chained_exceptionsTrequest)session_moder(   )
asgi_scopemethod value)zsentry.span.sourcezsentry.origin	asgi.type)http	websocketrs   rm   z.serverz	sentry.op)name
attributesz	{}.server)oprt   sourceoriginrq   )custom_sampling_contextr5   Dict[str, Any]r+   r   c                    sr   d ur2|  ddkod| v }|r2ttr+| d dkrdnd_d| d  n| d   | I d H S )Nr,   zhttp.response.startstatusi  errorokzhttp.response.status_code)get
isinstancer   r{   set_attributeset_http_status)r5   is_http_responsera   spanr6   r7   _sentry_wrapped_send3  s"   
z;SentryAsgiMiddleware._run_app.<locals>._sentry_wrapped_send)r5   rz   r+   r   ))_asgi_middleware_appliedr~   r9   	Exceptionr1   r2   r3   r\   sysexc_infor   r   r   setisolation_scoper   clear_breadcrumbs_namer   event_processoradd_event_processor _get_transaction_name_and_sourcerF   upperrB   rG   rH   tracesr   r   set_custom_sampling_context
start_span	new_tracer   HTTP_SERVERr
   formatr   set_tagstart_transactionr^   )rU   r_   r`   ra   rK   is_recursive_asgi_middlewareis_lifespanr)   suppress_chained_exceptionsr   clientspan_streamingsentry_scope	processortytransaction_nametransaction_sourcern   segmentru   span_ctxtransactionr   r6   r   r7   rd      s*  











	

 
 

    zSentryAsgiMiddleware._run_appr5   r"   r0   r#   rm   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 )Nrk   r   transaction_inforw   )r~   updater   r   _DEFAULT_TRANSACTION_NAMEr   	COMPONENTROUTECUSTOMr   rF   )
rU   r5   r0   rm   request_datar   r   already_setrt   rw   r6   r6   r7   r   d  s,   

z$SentryAsgiMiddleware.event_processorrU   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&   ro   rr   ws)hostr'   routepath)	r   r~   r   r   r   URLrB   r   r   )	rU   rF   rm   rt   rw   r   r&   r   r   r6   r6   r7   r     s.   


z5SentryAsgiMiddleware._get_transaction_name_and_source)r_   r   r+   r   )r_   r   r`   r   ra   r   r+   r   )r5   r"   r0   r#   rm   r   r+   r   )__name__
__module____qualname__	__slots__r	   boolstrrW   r   r\   r^   rT   rS   intrd   r   r   r6   r6   r6   r7   rE   Z   sv    	
9


 1
#rE   )r(   )=__doc__r   r@   r<   copyr   	functoolsr   r1   sentry_sdk.apir   sentry_sdk.constsr   $sentry_sdk.integrations._asgi_commonr   r   r   $sentry_sdk.integrations._wsgi_commonr	   r
   sentry_sdk.sessionsr   sentry_sdk.tracesr   sentry_sdk.tracingr   r   r   sentry_sdk.tracing_utilsr   sentry_sdk.utilsr   r   r   r   r   r   r   r   r   typingr   r   r   r   r   r   r    sentry_sdk._typesr!   r"   r#   r$   r   r   rP   r   r8   r   rD   rE   r6   r6   r6   r7   <module>   s@    ,	