o
    Zh4                     @   sF  d dl Z d dlZd dlmZ d dl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 d dlmZmZmZmZmZ d d	lmZmZ d d
l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& d dl"m'Z' d dl"m(Z( d dl)m*Z*m+Z+ d dl,m-Z- d dlm.Z.m/Z/m0Z0 d dl1m2Z2 zd dl3m4Z4m5Z6 d dl7m8Z8 d dl1m9Z9 d dl:m;Z; W n e<y   edw e;j=Z>e4j?Z@e9jAZBze4jCZDW n	 eEy   Y nw G dd deZFG dd  d eZGd!d" ZHd#d$ ZId%d& ZCd'd( ZJd7d)d*ZKd+d, ZLd-d. ZMd/d0 ZNd1d2 ZOd3d4 ZPd5d6 ZQdS )8    N)isawaitable)continue_trace)urlparsereraise)OP)Hub)TRANSACTION_SOURCE_COMPONENTTRANSACTION_SOURCE_URL)capture_internal_exceptionsevent_from_exceptionHAS_REAL_CONTEXTVARSCONTEXTVARS_ERROR_MESSAGEparse_version)IntegrationDidNotEnable)RequestExtractor_filter_headers)ignore_logger)TYPE_CHECKING)	Container)Any)Callable)Optional)Union)Tuple)Dict)RequestRequestParameters)BaseHTTPResponse)EventEventProcessorHint)Route)Sanic__version__)SanicException)Router)ErrorHandlerzSanic not installedc                   @   s2   e Zd ZdZdZedhfddZedd ZdS )SanicIntegrationsanicNi  c                 C   s   |pt  | _dS )aG  
        The unsampled_statuses parameter can be used to specify for which HTTP statuses the
        transactions should not be sent to Sentry. By default, transactions are sent for all
        HTTP statuses, except 404. Set unsampled_statuses to None to send transactions for all
        HTTP statuses, including 404.
        N)set_unsampled_statuses)selfZunsampled_statuses r-   T/var/www/html/lang_env/lib/python3.10/site-packages/sentry_sdk/integrations/sanic.py__init__=   s   zSanicIntegration.__init__c                   C   st   t tt_tjd u rtdttjdk rtdts"tdt tdr+t	d tjdk r5t
  d S t  d S )NzUnparsable Sanic version: {})r      zSanic 0.8 or newer required.zUThe sanic integration for Sentry requires Python 3.7+  or the aiocontextvars package.z0.8.root   	   )r   SANIC_VERSIONr(   versionr   formatr   r   
startswithr   _setup_legacy_sanic_setup_sanicr-   r-   r-   r.   
setup_onceG   s"   






zSanicIntegration.setup_once)	__name__
__module____qualname__
identifierr6   	frozensetr/   staticmethodr;   r-   r-   r-   r.   r(   9   s    
r(   c                   @   sL   e Zd Z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 )SanicRequestExtractorc                 C   s   | j jd u rdS t| j jS )Nr   )requestbodylenr,   r-   r-   r.   content_lengtho   s   z$SanicRequestExtractor.content_lengthc                 C   s   t | jjS N)dictrC   cookiesrF   r-   r-   r.   rJ   u   s   zSanicRequestExtractor.cookiesc                 C      | j jS rH   )rC   rD   rF   r-   r-   r.   raw_datay      zSanicRequestExtractor.raw_datac                 C   rK   rH   )rC   formrF   r-   r-   r.   rN   }   rM   zSanicRequestExtractor.formc                 C   s   t  rH   )NotImplementedErrorrF   r-   r-   r.   is_json   s   zSanicRequestExtractor.is_jsonc                 C   rK   rH   )rC   jsonrF   r-   r-   r.   rQ      rM   zSanicRequestExtractor.jsonc                 C   rK   rH   )rC   filesrF   r-   r-   r.   rR      rM   zSanicRequestExtractor.filesc                 C   s   t |jpdS )Nr-   )rE   rD   )r,   filer-   r-   r.   size_of_file   s   z"SanicRequestExtractor.size_of_fileN)r<   r=   r>   rG   rJ   rL   rN   rP   rQ   rR   rT   r-   r-   r-   r.   rB   n   s    rB   c                   C   s   t t_ tt_d S rH   )_startupr#   _sentry_error_handler_lookupr'   lookupr-   r-   r-   r.   r:      s   
r:   c                   C   s   t t_tt_tt_d S rH   )	_legacy_handle_requestr#   handle_request_legacy_router_getr&   getrV   r'   rW   r-   r-   r-   r.   r9      s   
r9   c                    s>   |  dt |  dt |  dt t| I d H  d S )Nzhttp.lifecycle.requestzhttp.lifecycle.responsezhttp.routing.after)signal
_hub_enter	_hub_exit_set_transactionold_startuprF   r-   r-   r.   rU      s
   rU   c                    s   t j}|td u| j_| jjsd S t| }t || j_| jj	  | jj
 }|  |t| W d    n1 s@w   Y  tt| jtj| jtd}| jj|	 | j_d S )N)opnamesource)r   currentget_integrationr(   ctx_sentry_do_integrationweakrefref_sentry_hub	__enter__configure_scopeclear_breadcrumbsadd_event_processor_make_request_processorr   rI   headersr   ZHTTP_SERVERpathr	   Zstart_transaction_sentry_transaction)rC   hubweak_requestscopeZtransactionr-   r-   r.   r]      s0   

r]   c              	      s   t  b | jjs	 W d    d S tjt}|d u rd n|j}t  ) | jj	| | jj j
t|to:||jvM  _
| jjd d d  W d    n1 sPw   Y  | jjd d d  W d    d S 1 siw   Y  d S rH   )r
   rf   rg   r   rd   re   r(   statusrr   Zset_http_statusZsampled
isinstancer+   __exit__rj   )rC   responseintegrationZresponse_statusr-   r-   r.   r^      s"   

"r^   c              	      s   t j}| jjrMt 8 | }|j| jjd	d}|j
|td W d    n1 s.w   Y  W d    d S W d    d S 1 sFw   Y  d S d S )N .rc   )r   rd   rf   rg   r
   rl   rb   replaceappstripset_transaction_namer   )rC   Zroute_rs   ru   Z
route_namer-   r-   r.   r_      s   
"r_   c                    sP   t | t| |g|R i |  d u rd S tjtd u r  S  fdd}|S )Nc                    s   zOz# | |}t |r|I d H }|W W tjr$tjdkr%t| I d H  S S S  ty:   t }t| t|  Y nw W tjrMtjdkrOt| I d H  d S d S d S tjratjdkrbt| I d H  w w w )Nr2   )	r   r(   r6   r^   	Exceptionsysexc_info_capture_exceptionr   )rC   	exceptionry   r   Zold_error_handlerr-   r.   sentry_wrapped_error_handler   s(   

zB_sentry_error_handler_lookup.<locals>.sentry_wrapped_error_handler)r   old_error_handler_lookupr   rd   re   r(   )r,   r   argskwargsr   r-   r   r.   rV      s   rV   c              	      s   t j}|td u rt| |g|R i |S t|}t |>}| }|  |	t
| W d    n1 s;w   Y  t| |g|R i |}t|rU|I d H }|W  d    S 1 saw   Y  d S rH   )r   rd   re   r(   old_handle_requestrh   ri   rl   rm   rn   ro   r   )r,   rC   r   r   rs   rt   ru   ry   r-   r-   r.   rX     s   



$rX   c              	   G   s   t | g|R  }tj}|td urwt Z | =}tjrFtjdkrF| jj	j
}|d j
}|d| r>|t|d d  }|j|td n
|j|d jtd W d    n1 sZw   Y  W d    |S W d    |S 1 srw   Y  |S )N)r3      r   z%s.   r}   )old_router_getr   rd   re   r(   r
   rl   r6   rf   r   rb   r8   rE   r   r   r<   )r,   r   rvrs   ru   Zsanic_app_nameZsanic_router-   r-   r.   rZ   *  s2   





rZ   c                 C   sv   t j}|t}|d u rd S |j}t  t| |jdddd\}}|j||d W d    d S 1 s4w   Y  d S )Nr)   F)typeZhandled)Zclient_optionsZ	mechanism)hint)	r   rd   re   r(   clientr
   r   optionsZcapture_event)r   rs   rz   r   eventr   r-   r-   r.   r   I  s   

"r   c                    s    fdd}|S )Nc                    s   z|rt |d d trW d S W n	 ty   Y nw   }|d u r#| S t B t|}||  | d }t|j}d|j	|j
|jf |d< |j|d< |j|d< d|ji|d	< tt|j|d
< W d    | S 1 skw   Y  | S )Nr   r   rC   z	%s://%s%surlZquery_stringmethodZREMOTE_ADDRenvrp   )
issubclassr%   KeyErrorr
   rB   Zextract_into_eventr   urlsplitr   schemenetlocrq   queryr   Zremote_addrr   rI   rp   )r   r   rC   Z	extractorZrequest_infourlpartsrt   r-   r.   sanic_processor^  s8   




z0_make_request_processor.<locals>.sanic_processorr-   )rt   r   r-   r   r.   ro   \  s   !ro   rH   )Rr   rh   inspectr   Z
sentry_sdkr   Zsentry_sdk._compatr   r   Zsentry_sdk.constsr   Zsentry_sdk.hubr   Zsentry_sdk.tracingr   r	   Zsentry_sdk.utilsr
   r   r   r   r   Zsentry_sdk.integrationsr   r   Z$sentry_sdk.integrations._wsgi_commonr   r   Zsentry_sdk.integrations.loggingr   Zsentry_sdk._typesr   collections.abcr   typingr   r   r   r   r   r   Zsanic.requestr   r   Zsanic.responser   r   r    r!   Zsanic.routerr"   r)   r#   r$   r5   Zsanic.exceptionsr%   r&   Zsanic.handlersr'   ImportErrorrW   r   rY   r   r[   r   rU   r`   AttributeErrorr(   rB   r:   r9   r]   r^   r_   rV   rX   rZ   r   ro   r-   r-   r-   r.   <module>   sl    
5$
"