o
    0 i2                     @   sF  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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l&m'Z'm(Z( d dl)m*Z* d dl+m,Z,m-Z-m.Z.m/Z/ d dl0m1Z1 zd dl2m3Z3m4Z5 d dl6m7Z7 d dl0m8Z8 d dl9m:Z: W n e;y   edw e:j<Z=e3j>Z?e8j@ZAze3jBZCW n	 eDy   Y nw G dd deZEG dd deZFdd  ZGd!d" ZHd#d$ ZBd%d& ZId5d'd(ZJd)d* ZKd+d, ZLd-d. ZMd/d0 ZNeeEd1d2 ZOd3d4 ZPdS )6    N)isawaitable)urlsplit)continue_trace)OP)_check_minimum_versionIntegrationDidNotEnable)RequestExtractor_filter_headers)ignore_logger)TransactionSource)capture_internal_exceptionsensure_integration_enabledevent_from_exceptionHAS_REAL_CONTEXTVARSCONTEXTVARS_ERROR_MESSAGEparse_versionreraise)TYPE_CHECKING)	Container)Any)Callable)Optional)Union)Dict)RequestRequestParameters)BaseHTTPResponse)EventEventProcessorExcInfoHint)Route)Sanic__version__)SanicException)Router)ErrorHandlerzSanic not installedc                   @   s<   e Zd ZdZde ZdZedhfddZedd Z	dS )	SanicIntegrationsanicz
auto.http.Ni  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-   i/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/sentry_sdk/integrations/sanic.py__init__?   s   zSanicIntegration.__init__c                   C   s`   t tt_tttj tstdt tdrt	d tjd ur+tjdk r+t
  d S t  d S )NzUThe sanic integration for Sentry requires Python 3.7+  or the aiocontextvars package.z0.8.root   	   )r   SANIC_VERSIONr(   versionr   r   r   r   
startswithr   _setup_legacy_sanic_setup_sanicr-   r-   r-   r.   
setup_onceI   s   



zSanicIntegration.setup_once)
__name__
__module____qualname__
identifieroriginr5   	frozensetr/   staticmethodr9   r-   r-   r-   r.   r(   :   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_lengthk   s   z$SanicRequestExtractor.content_lengthc                 C   s   t | jjS N)dictrB   cookiesrE   r-   r-   r.   rI   q   s   zSanicRequestExtractor.cookiesc                 C      | j jS rG   )rB   rC   rE   r-   r-   r.   raw_datau      zSanicRequestExtractor.raw_datac                 C   rJ   rG   )rB   formrE   r-   r-   r.   rM   y   rL   zSanicRequestExtractor.formc                 C   s   t  rG   )NotImplementedErrorrE   r-   r-   r.   is_json}   s   zSanicRequestExtractor.is_jsonc                 C   rJ   rG   )rB   jsonrE   r-   r-   r.   rP      rL   zSanicRequestExtractor.jsonc                 C   rJ   rG   )rB   filesrE   r-   r-   r.   rQ      rL   zSanicRequestExtractor.filesc                 C   s   t |jpdS )Nr-   )rD   rC   )r,   filer-   r-   r.   size_of_file   s   z"SanicRequestExtractor.size_of_fileN)r:   r;   r<   rF   rI   rK   rM   rO   rP   rQ   rS   r-   r-   r-   r.   rA   j   s    rA   c                   C   s   t t_ tt_d S rG   )_startupr#   _sentry_error_handler_lookupr'   lookupr-   r-   r-   r.   r8      s   
r8   c                   C   s   t t_tt_tt_d S rG   )	_legacy_handle_requestr#   handle_request_legacy_router_getr&   getrU   r'   rV   r-   r-   r-   r.   r7      s   
r7   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_context_enter_context_exit_set_transactionold_startuprE   r-   r-   r.   rT      s
   rT   c                    s   t  td u| j_| jjsd S t| }t  | j_	| jj	
 }|  |t| tt| jtj| jtjtjd}t |
 | j_d S )N)opnamesourcer>   )
sentry_sdk
get_clientget_integrationr(   ctx_sentry_do_integrationweakrefrefisolation_scope_sentry_scope	__enter__clear_breadcrumbsadd_event_processor_make_request_processorr   rH   headersr   ZHTTP_SERVERpathr   URLr>   Zstart_transaction_sentry_transaction)rB   weak_requestscopeZtransactionr-   r-   r.   r\      s,   

r\   c              	      s   t  c | jjs	 W d    d S t 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 sQw   Y  | jjd d d  W d    d S 1 sjw   Y  d S rG   )r   rf   rg   rc   rd   re   r(   statusrs   Zset_http_statusZsampled
isinstancer+   __exit__rk   )rB   responseZintegrationZresponse_statusr-   r-   r.   r]      s"   

"r]   c                    sj   | j jr3t ! t }|j| jjdd}|j	|t
jd W d    d S 1 s,w   Y  d S d S )N .rb   )rf   rg   r   rc   Zget_current_scopera   replaceappstripset_transaction_namer   	COMPONENT)rB   Zroute_ru   Z
route_namer-   r-   r.   r^      s   "r^   c                    sR   t | t| |g|R i |  d u rd S t 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 )Nr1   )	r   r(   r5   r]   	Exceptionsysexc_info_capture_exceptionr   )rB   	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_lookuprc   rd   re   r(   )r,   r   argskwargsr   r-   r   r.   rU      s   rU   c                    s   t  td u rt| |g|R i |I d H S t|}t  *}|  |	t
| t| |g|R i |}t|rC|I d H }|W  d    S 1 sOw   Y  d S rG   )rc   rd   re   r(   old_handle_requestrh   ri   rj   rm   rn   ro   r   )r,   rB   r   r   rt   ru   ry   r-   r-   r.   rW     s   


$rW   c                 G   s   t | g|R  }t td urht L t }tjrEtjdkrE| jj	j
}|d j
}|d| r<|t|d d  }|j|tjd n|j|d jtjd W d    |S W d    |S 1 scw   Y  |S )N)r2      r   z%s.   r|   )old_router_getrc   rd   re   r(   r   Zget_isolation_scoper5   rf   r~   ra   r6   rD   r   r   r   r:   )r,   r   rvru   Zsanic_app_nameZsanic_router-   r-   r.   rY     s,   



rY   c                 C   s   t  : t| t jdddd\}}|r.t|d d dr.|d d jr.	 W d    d S tj||d W d    d S 1 s@w   Y  d S )	Nr)   F)typeZhandled)Zclient_optionsZ	mechanismr   r   quiet)hint)r   r   rc   rd   optionshasattrr   Zcapture_event)r   eventr   r-   r-   r.   r   =  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 A 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 sjw   Y  | S )Nr   r   rB   z	%s://%s%surlZquery_stringmethodZREMOTE_ADDRenvrp   )
issubclassr%   KeyErrorr   rA   Zextract_into_eventr   r   schemenetlocrq   queryr   remote_addrr
   rH   rp   )r   r   rB   Z	extractorZrequest_infourlpartsrt   r-   r.   sanic_processorO  s8   





z0_make_request_processor.<locals>.sanic_processorr-   )rt   r   r-   r   r.   ro   M  s   !ro   rG   )Qr   rh   inspectr   urllib.parser   rc   r   Zsentry_sdk.constsr   Zsentry_sdk.integrationsr   r   r   Z$sentry_sdk.integrations._wsgi_commonr	   r
   Zsentry_sdk.integrations.loggingr   Zsentry_sdk.tracingr   Zsentry_sdk.utilsr   r   r   r   r   r   r   typingr   collections.abcr   r   r   r   r   r   Zsanic.requestr   r   Zsanic.responser   Zsentry_sdk._typesr   r   r    r!   Zsanic.routerr"   r)   r#   r$   r4   Zsanic.exceptionsr%   r&   Zsanic.handlersr'   ImportErrorrV   r   rX   r   rZ   r   rT   r_   AttributeErrorr(   rA   r8   r7   r\   r]   r^   rU   rW   rY   r   ro   r-   r-   r-   r.   <module>   sl    $

0$
	"
