o
    ٰiZ                     @   s  d dl Z d dlZd dlmZ d dlZd dlmZ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 erXd d
lmZ d dlmZ d dlmZmZ zd dlZW n eyi   dZY nw z d dlmZmZm Z m!Z!m"Z"m#Z# d dl$m%Z%m&Z&m'Z'm(Z(m)Z) W n ey   edw zd dl*m+Z+ W n ey   d dl,m+Z+ Y nw dZ-G dd deZ.d3ddZ/d3ddZ0ddde1ddddfddZ2d4d$d%Z3	 	!			&			'd5d(d)Z4	*	!	+	,	"	#		d6d-d.Z5d7d1d2Z6dS )8    N)wraps)DidNotEnableIntegration)_filter_headers)SentryAsgiMiddleware)should_send_default_pii)SOURCE_FOR_STYLE)capture_internal_exceptionsensure_integration_enabledevent_from_exception)TYPE_CHECKING)Any)Union)EventEventProcessor)has_request_contexthas_websocket_contextRequestQuartrequest	websocket)got_background_exceptiongot_request_exceptiongot_websocket_exceptionrequest_startedwebsocket_startedzQuart is not installed)Scaffold)endpointurlc                   @   s@   e Zd ZdZde ZdZddeddfdd	Zedd
dZ	dS )QuartIntegrationquartz
auto.http. r   transaction_stylereturnNc                 C   s"   |t vrtd|t f || _d S )Nz7Invalid value for transaction_style: %s (must be in %s))TRANSACTION_STYLE_VALUES
ValueErrorr"   )selfr"    r'   Q/home/ubuntu/.local/lib/python3.10/site-packages/sentry_sdk/integrations/quart.py__init__?   s   
zQuartIntegration.__init__c                   C   sB   t t tt tt tt tt t  t	  d S N)
r   connect_request_websocket_startedr   r   _capture_exceptionr   r   patch_asgi_apppatch_scaffold_router'   r'   r'   r(   
setup_onceG   s   





zQuartIntegration.setup_once)r   r#   N)
__name__
__module____qualname__
identifieroriginr"   strr)   staticmethodr0   r'   r'   r'   r(   r   9   s    
r   r#   c                     s2   t j 										d	 fdd} | t _d S )
Nr&   r   scopereceivesendr#   c                    sT   t  td u r |||I d H S t fddtjdd}||||I d H S )Nc                     s    g| R i |S r*   r'   )akw)old_appr&   r'   r(   <lambda>]   s    zApatch_asgi_app.<locals>.sentry_patched_asgi_app.<locals>.<lambda>   )span_originasgi_version)
sentry_sdk
get_clientget_integrationr   r   r6   )r&   r9   r:   r;   
middlewarer>   )r&   r(   sentry_patched_asgi_appV   s   z/patch_asgi_app.<locals>.sentry_patched_asgi_app)
r&   r   r9   r   r:   r   r;   r   r#   r   )r   __call__)rH   r'   rG   r(   r.   S   s   
r.   c                     s   t j d fdd} | t _d S )Nargsr   kwargsr#   c                     s    | i | d fdd}|S )Nold_funcr   r#   c                    sD   t  rt st tt d fdd}|S  S )NrJ   r   rK   r#   c                     sF   t  }|jd ur|j  t  }|jd ur|j   | i |S r*   )rC   get_current_scopetransactionupdate_active_threadget_isolation_scopeprofileupdate_active_thread_id)rJ   rK   current_scopesentry_scoperL   r'   r(   _sentry_funcq   s   



zTpatch_scaffold_route.<locals>._sentry_route.<locals>.decorator.<locals>._sentry_funcrJ   r   rK   r   r#   r   )inspect
isfunctionasyncioiscoroutinefunctionr   r
   r   )rL   rV   old_decoratorrU   r(   	decoratorl   s   z>patch_scaffold_route.<locals>._sentry_route.<locals>.decorator)rL   r   r#   r   r'   )rJ   rK   r^   	old_router\   r(   _sentry_routei   s   z+patch_scaffold_route.<locals>._sentry_routerW   )r   route)ra   r'   r_   r(   r/   f   s   
r/   r9   zsentry_sdk.Scoper"   r   r   c                 C   sD   z|j j|j jd}| j|| t| d W d S  ty!   Y d S w )N)r   r   )source)url_ruleruler   set_transaction_namer   	Exception)r9   r"   r   name_for_styler'   r'   r(    _set_transaction_name_and_source   s   ri   appr   rK   r   c                    sl   t  t}|d u rd S t rt }t rt }t	t 
 |j| t  }t| ||}|| d S r*   )rC   rD   rE   r   r   r   _get_current_objectr   r   ri   rM   r"   rP   _make_request_event_processoradd_event_processor)rj   rK   integrationrequest_websocketr9   evt_processorr'   r'   r(   r,      s   r,   rn   r   c                    s   d fdd}|S )	Neventr   hintdict[str, Any]r#   c                    s    d u r| S t  ? | di } j|d<  j|d<  j|d< tt j|d< t r@d j	d i|d< t
|  W d    | S W d    | S 1 sKw   Y  | S )	Nr   r   query_stringmethodheadersREMOTE_ADDRr   env)r	   
setdefaultr   rt   ru   r   dictrv   r   access_route_add_user_to_event)rq   rr   request_infor   r'   r(   inner   s$   





z,_make_request_event_processor.<locals>.inner)rq   r   rr   rs   r#   r   r'   )rj   r   rn   r   r'   r~   r(   rl      s   rl   sender	exception Union[ValueError, BaseException]c                    sL   t  t}|d u rd S t|t  jdddd\}}t j||d d S )Nr    F)typehandled)client_options	mechanism)rr   )rC   rD   rE   r   r   optionscapture_event)r   r   rK   rn   rq   rr   r'   r'   r(   r-      s   
r-   rq   r   c                 C   sb   t d u rd S t j}|d u rd S t  | di }t jj|d< W d    d S 1 s*w   Y  d S )Nuserid)
quart_authcurrent_userr	   ry   _auth_id)rq   r   	user_infor'   r'   r(   r|      s   "r|   r1   )rj   r   rK   r   r#   N)rj   r   r   r   rn   r   r#   r   )r   r   r   r   rK   r   r#   N)rq   r   r#   N)7rZ   rX   	functoolsr   rC   sentry_sdk.integrationsr   r   $sentry_sdk.integrations._wsgi_commonr   sentry_sdk.integrations.asgir   sentry_sdk.scoper   sentry_sdk.tracingr   sentry_sdk.utilsr	   r
   r   typingr   r   r   sentry_sdk._typesr   r   r   ImportErrorr    r   r   r   r   r   r   quart.signalsr   r   r   r   r   quart.scaffoldr   flask.sansio.scaffoldr$   r   r.   r/   r7   ri   r,   rl   r-   r|   r'   r'   r'   r(   <module>   s      

!



