o
    ٰi1                  
   @   s  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ZFdId d!ZGdId"d#ZHdJd&d'ZBdKd*d+ZI	dL	)	,	-		dMd.d/ZJdNd4d5ZKd$d3d6eLd7d3d8d3dd9f
d:d;ZM	$	3	(	)	7	3	8	3		3dOd<d=ZNdPd?d@ZOeeEdQdBdCZPdRdGdHZQdS )S    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                   @   sF   e Zd ZdZde ZdZedhf			ddd	Zedd
dZ	dS )SanicIntegrationsanicz
auto.http.Ni  unsampled_statusesOptional[Container[int]]returnc                 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)selfr*    r0   Q/home/ubuntu/.local/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_sanicr0   r0   r0   r1   
setup_onceJ   s   



zSanicIntegration.setup_once)r*   r+   r,   Nr,   N)
__name__
__module____qualname__
identifieroriginr8   	frozensetr2   staticmethodr<   r0   r0   r0   r1   r(   :   s    

r(   c                   @   sp   e Zd ZdefddZdddZdefddZdd
dZde	fddZ
dddZdddZdddefddZdS )SanicRequestExtractorr,   c                 C   s   | j jd u rdS t| j jS )Nr   )requestbodylenr/   r0   r0   r1   content_lengthk   s   z$SanicRequestExtractor.content_lengthDict[str, str]c                 C   s   t | jjS N)dictrF   cookiesrI   r0   r0   r1   rN   p   s   zSanicRequestExtractor.cookiesc                 C      | j jS rL   )rF   rG   rI   r0   r0   r1   raw_datas      zSanicRequestExtractor.raw_datar   c                 C   rO   rL   )rF   formrI   r0   r0   r1   rR   v   rQ   zSanicRequestExtractor.formc                 C   s   t  rL   )NotImplementedErrorrI   r0   r0   r1   is_jsony   s   zSanicRequestExtractor.is_jsonOptional[Any]c                 C   rO   rL   )rF   jsonrI   r0   r0   r1   rV   |   rQ   zSanicRequestExtractor.jsonc                 C   rO   rL   )rF   filesrI   r0   r0   r1   rW      rQ   zSanicRequestExtractor.filesfiler   c                 C   s   t |jpdS )Nr0   )rH   rG   )r/   rX   r0   r0   r1   size_of_file   s   z"SanicRequestExtractor.size_of_fileN)r,   rK   )r,   r   )r,   rU   )r>   r?   r@   intrJ   rN   bytesrP   rR   boolrT   rV   rW   rY   r0   r0   r0   r1   rE   j   s    



rE   r,   c                   C   s   t t_ tt_d S rL   )_startupr#   _sentry_error_handler_lookupr'   lookupr0   r0   r0   r1   r;      s   
r;   c                   C   s   t t_tt_tt_d S rL   )	_legacy_handle_requestr#   handle_request_legacy_router_getr&   getr^   r'   r_   r0   r0   r0   r1   r:      s   
r:   r/   r#   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_startuprI   r0   r0   r1   r]      s
   r]   rF   r   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sourcerB   )
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   rM   headersr   HTTP_SERVERpathr   URLrB   start_transaction_sentry_transaction)rF   weak_requestscopetransactionr0   r0   r1   re      s,   

re   responseOptional[BaseHTTPResponse]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 rL   )r   ro   rp   rl   rm   rn   r(   statusr~   set_http_statussampled
isinstancer.   __exit__rt   )rF   r   integrationresponse_statusr0   r0   r1   rf      s"   

"rf   router"   _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 .rk   )ro   rp   r   rl   get_current_scoperj   replaceappstripset_transaction_namer   	COMPONENT)rF   r   r   r   
route_namer0   r0   r1   rg      s   "rg   	exceptionargskwargszOptional[object]c                    s`   t | t| |g|R i |  d u rd S t td u r! S dddtddf fdd}|S )NrF   r   r   r,   r   c                    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 )Nr4   )	r   r(   r8   rf   	Exceptionsysexc_info_capture_exceptionr   )rF   r   r   r   old_error_handlerr0   r1   sentry_wrapped_error_handler   s(   

zB_sentry_error_handler_lookup.<locals>.sentry_wrapped_error_handler)r   old_error_handler_lookuprl   rm   rn   r(   r   )r/   r   r   r   r   r0   r   r1   r^      s   r^   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 rL   )rl   rm   rn   r(   old_handle_requestrq   rr   rs   rv   rw   rx   r   )r/   rF   r   r   r   r   r   r0   r0   r1   r`     s   


$r`   Union[Any, Request]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)r5      r   z%s.   r   )old_router_getrl   rm   rn   r(   r   get_isolation_scoper8   ro   r   rj   r9   rH   r   r   r   r>   )r/   r   rvr   sanic_app_namesanic_router0   r0   r1   rb     s,   



rb   Union[ExcInfo, BaseException]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)typehandled)client_options	mechanismr   r   quiet)hint)r   r   rl   rm   optionshasattrr   capture_event)r   eventr   r0   r0   r1   r   3  s   
$
"r   r   Callable[[], Request]r   c                    s   d	 fdd}|S )
Nr   r   r   Optional[Hint]r,   Optional[Event]c                    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   rF   z	%s://%s%surlquery_stringmethodREMOTE_ADDRenvry   )
issubclassr%   KeyErrorr   rE   extract_into_eventr   r   schemenetlocr{   queryr   remote_addrr
   rM   ry   )r   r   rF   	extractorrequest_infourlpartsr   r0   r1   sanic_processorC  s8   





z0_make_request_processor.<locals>.sanic_processor)r   r   r   r   r,   r   r0   )r   r   r0   r   r1   rx   B  s   rx   r=   )r/   r#   r,   N)rF   r   r,   NrL   )rF   r   r   r   r,   N)rF   r   r   r"   r   r   r,   N)
r/   r   rF   r   r   r   r   r   r,   r   )r/   r   r   r   r,   r   )r   r   r,   N)r   r   r,   r   )Rr   rq   inspectr   urllib.parser   rl   r   sentry_sdk.constsr   sentry_sdk.integrationsr   r   r   $sentry_sdk.integrations._wsgi_commonr	   r
   sentry_sdk.integrations.loggingr   sentry_sdk.tracingr   sentry_sdk.utilsr   r   r   r   r   r   r   typingr   collections.abcr   r   r   r   r   r   sanic.requestr   r   sanic.responser   sentry_sdk._typesr   r   r    r!   sanic.routerr"   r)   r#   r$   r7   sanic.exceptionsr%   r&   sanic.handlersr'   ImportErrorr_   r   ra   r   rc   r   r]   rh   AttributeErrorr(   rE   r;   r:   re   rf   rg   r   r^   r`   rb   r   rx   r0   r0   r0   r1   <module>   s    $

0






$

