o
    h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)selfunsampled_statuses r.   `/var/www/html/aiguide_backend/venv/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__
identifierr7   	frozensetr0   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)dictrD   cookiesrG   r.   r.   r/   rK   u   s   zSanicRequestExtractor.cookiesc                 C      | j jS rI   )rD   rE   rG   r.   r.   r/   raw_datay      zSanicRequestExtractor.raw_datac                 C   rL   rI   )rD   formrG   r.   r.   r/   rO   }   rN   zSanicRequestExtractor.formc                 C   s   t  rI   )NotImplementedErrorrG   r.   r.   r/   is_json   s   zSanicRequestExtractor.is_jsonc                 C   rL   rI   )rD   jsonrG   r.   r.   r/   rR      rN   zSanicRequestExtractor.jsonc                 C   rL   rI   )rD   filesrG   r.   r.   r/   rS      rN   zSanicRequestExtractor.filesc                 C   s   t |jpdS )Nr.   )rF   rE   )r,   filer.   r.   r/   size_of_file   s   z"SanicRequestExtractor.size_of_fileN)r=   r>   r?   rH   rK   rM   rO   rQ   rR   rS   rU   r.   r.   r.   r/   rC   n   s    rC   c                   C   s   t t_ tt_d S rI   )_startupr#   _sentry_error_handler_lookupr'   lookupr.   r.   r.   r/   r;      s   
r;   c                   C   s   t t_tt_tt_d S rI   )	_legacy_handle_requestr#   handle_request_legacy_router_getr&   getrW   r'   rX   r.   r.   r.   r/   r:      s   
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
_hub_enter	_hub_exit_set_transactionold_startuprG   r.   r.   r/   rV      s
   rV   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   rJ   headersr   HTTP_SERVERpathr	   start_transaction_sentry_transaction)rD   hubweak_requestscope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 rI   )r
   rg   rh   r   re   rf   r(   statusru   set_http_statussampled
isinstancer+   __exit__rk   )rD   responseintegration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 .rd   )r   re   rg   rh   r
   rm   rc   replaceappstripset_transaction_namer   )rD   route_rv   rx   
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 )Nr3   )	r   r(   r7   r_   	Exceptionsysexc_info_capture_exceptionr   )rD   	exceptionr   r   old_error_handlerr.   r/   sentry_wrapped_error_handler   s(   

zB_sentry_error_handler_lookup.<locals>.sentry_wrapped_error_handler)r   old_error_handler_lookupr   re   rf   r(   )r,   r   argskwargsr   r.   r   r/   rW      s   rW   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 rI   )r   re   rf   r(   old_handle_requestri   rj   rm   rn   ro   rp   r   )r,   rD   r   r   rv   rw   rx   r   r.   r.   r/   rY     s   



$rY   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)r4      r   z%s.   r   )old_router_getr   re   rf   r(   r
   rm   r7   rg   r   rc   r9   rF   r   r   r=   )r,   r   rvrv   rx   sanic_app_namesanic_router.   r.   r/   r[   *  s2   





r[   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)typehandled)client_options	mechanism)hint)	r   re   rf   r(   clientr
   r   optionscapture_event)r   rv   r   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   rD   z	%s://%s%surlquery_stringmethodREMOTE_ADDRenvrq   )
issubclassr%   KeyErrorr
   rC   extract_into_eventr   urlsplitr   schemenetlocrs   queryr   remote_addrr   rJ   rq   )r   r   rD   	extractorrequest_infourlpartsrw   r.   r/   sanic_processor^  s8   




z0_make_request_processor.<locals>.sanic_processorr.   )rw   r   r.   r   r/   rp   \  s   !rp   rI   )Rr   ri   inspectr   
sentry_sdkr   sentry_sdk._compatr   r   sentry_sdk.constsr   sentry_sdk.hubr   sentry_sdk.tracingr   r	   sentry_sdk.utilsr
   r   r   r   r   sentry_sdk.integrationsr   r   $sentry_sdk.integrations._wsgi_commonr   r   sentry_sdk.integrations.loggingr   sentry_sdk._typesr   collections.abcr   typingr   r   r   r   r   r   sanic.requestr   r   sanic.responser   r   r    r!   sanic.routerr"   r)   r#   r$   r6   sanic.exceptionsr%   r&   sanic.handlersr'   ImportErrorrX   r   rZ   r   r\   r   rV   ra   AttributeErrorr(   rC   r;   r:   r^   r_   r`   rW   rY   r[   r   rp   r.   r.   r.   r/   <module>   sl    
5$
"