
    \j1              
          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rDd 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 	 d dl2m3Z3m4Z5 d dl6m7Z7 d dl0m8Z8 d dl9m:Z: e:jx                  Z=e3j|                  Z?e8j                  ZA	 e3j                  ZC G d de      ZE G d de      ZFd1d ZGd1d!ZHd2d#ZBd3d$ZI	 d4	 	 	 	 	 d5d%ZJd6d'ZKd"d&d(eLd)d&d*d&dd+f
d,ZM	 	 	 	 	 	 	 	 	 	 d7d-ZNd8d.ZO eeE      d9d/       ZPd:d0ZQy# e;$ r	  ed      w xY w# eD$ r Y w xY w);    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                   P    e Zd ZdZde ZdZ edh      f	 	 	 ddZedd       Z	y)	SanicIntegrationsanicz
auto.http.Ni  c                 *    |xs
 t               | _        y)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_statusess     G/root/env/lib/python3.12/site-packages/sentry_sdk/integrations/sanic.py__init__zSanicIntegration.__init__?   s     $6#>     c                  P   t        t              t        _        t	        t        t        j                         t
        st        dt        z         t        j                  d      rt        d       t        j                  t        j                  dk  rt                y t                y )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_sanic r3   r1   
setup_oncezSanicIntegration.setup_onceJ   s    #0#? /1A1I1IJ# 24MN 
 ##F+ &!##/4D4L4Lw4V!r3   )r0   zOptional[Container[int]]returnNr@   N)
__name__
__module____qualname__
identifieroriginr:   	frozensetr2   staticmethodr?   r>   r3   r1   r*   r*   :   sO    J*&FG @I#?O	?"<	?		?  r3   r*   c                   `    e Zd ZdefdZddZdefdZddZde	fdZ
ddZddZd	d
defdZy)SanicRequestExtractorr@   c                 n    | j                   j                  yt        | j                   j                        S )Nr   )requestbodylenr/   s    r1   content_lengthz$SanicRequestExtractor.content_lengthk   s*    <<$4<<$$%%r3   c                 @    t        | j                  j                        S N)dictrL   cookiesrO   s    r1   rT   zSanicRequestExtractor.cookiesp   s    DLL(())r3   c                 .    | j                   j                  S rR   )rL   rM   rO   s    r1   raw_datazSanicRequestExtractor.raw_datas       ||   r3   c                 .    | j                   j                  S rR   )rL   formrO   s    r1   rY   zSanicRequestExtractor.formv   rW   r3   c                     t               rR   )NotImplementedErrorrO   s    r1   is_jsonzSanicRequestExtractor.is_jsony   s    !##r3   c                 .    | j                   j                  S rR   )rL   jsonrO   s    r1   r^   zSanicRequestExtractor.json|   rW   r3   c                 .    | j                   j                  S rR   )rL   filesrO   s    r1   r`   zSanicRequestExtractor.files   s    ||!!!r3   filer   c                 4    t        |j                  xs d      S )Nr>   )rN   rM   )r/   ra   s     r1   size_of_filez"SanicRequestExtractor.size_of_file   s    499?##r3   N)r@   zDict[str, str])r@   r   )r@   zOptional[Any])rB   rC   rD   intrP   rT   bytesrV   rY   boolr\   r^   r`   rc   r>   r3   r1   rJ   rJ   j   sM    & &
*!% !!$ $!"$ $3 $r3   rJ   r@   c                  @    t         t        _         t        t        _        y rR   )_startupr$   _sentry_error_handler_lookupr(   lookupr>   r3   r1   r=   r=      s    EN6Lr3   c                  ^    t         t        _        t        t        _        t        t        _        y rR   )	_legacy_handle_requestr$   handle_request_legacy_router_getr'   getri   r(   rj   r>   r3   r1   r<   r<      s    1E#FJ6Lr3   r/   c                    K    | j                  d      t                | j                  d      t                | j                  d      t               t	        |        d {    y 7 w)Nzhttp.lifecycle.requestzhttp.lifecycle.responsezhttp.routing.after)signal_context_enter_context_exit_set_transactionold_startuprO   s    r1   rh   rh      sY      *DKK().9 +DKK)*=9 &DKK$%&67 d
s   A A*"A(#A*c                   K   t        j                         j                  t              d u| j                  _        | j                  j
                  sy t        j                  |       }t        j                         | j                  _	        | j                  j                  j                         }|j                          |j                  t        |             t        t        | j                         t"        j$                  | j&                  t(        j*                  t        j,                        }t        j.                  |      j                         | j                  _        y w)N)opnamesourcerF   )
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   rS   headersr   HTTP_SERVERpathr   URLrF   start_transaction_sentry_transaction)rL   weak_requestscopetransactions       r1   rr   rr      s     //0@AM KK& ;;--;;w'L * : : <GKKKK%%//1E		5lCD W__>>\\ $$&&K '1&B&B'ik KK#s   E E"c                   K   t               5  | j                  j                  s
	 d d d        y t        j                         j                  t              }|d n|j                  }t               5  | j                  j                  j                  |       | j                  j                  xj                  t        |t              xr ||j                  vz  c_
        | j                  j                  j                  d d d        d d d        | j                  j                  j                  d d d        d d d        y # 1 sw Y   9xY w# 1 sw Y   y xY wwrR   )r   r}   r~   rz   r{   r|   r*   statusr   set_http_statussampled
isinstancer.   __exit__r   )rL   responseintegrationresponse_statuss       r1   rs   rs      s     
%	& ={{11= = !++-==>NO"*"2$ )* 	GKK++;;OLKK++33;(89 K#;+J+JJ3 KK++44T4F	G 	!!**4t<%= =	G 	G= =s@   EE	EAE/BD;/E2	E;E	 EEEr   c                 `  K   | j                   j                  rt               5  t        j                         }|j
                  j                  | j                  j
                  d      j                  d      }|j                  |t        j                         d d d        y y # 1 sw Y   y xY ww)N .ry   )r}   r~   r   rz   get_current_scoperx   replaceappstripset_transaction_namer   	COMPONENT)rL   route_r   
route_names        r1   rt   rt      s     {{))(* 	W002E++GKK,<,<bAGGLJ&&z:K:U:U&V	W 	W *	W 	Ws   !B.A5B"
B."B+'B.	exceptionargskwargszOptional[object]c                     t        |       t        | |g|i |y t        j                         j	                  t
              S dddt        ddffd}|S )NrL   r   r   r@   r   c                 $  K   	  | |      }t        |      r
| d {   }|t        j                  r(t        j                  dk(  rt        |        d {    S S S 7 >7 	# t        $ r* t        j                         }t        |       t        |  Y nw xY w	 t        j                  r)t        j                  dk(  rt        |        d {  7   y y y # t        j                  r)t        j                  dk(  rt        |        d {  7   w w w xY ww)Nr6   )	r   r*   r:   rs   	Exceptionsysexc_info_capture_exceptionr   )rL   r   r   r   old_error_handlers       r1   sentry_wrapped_error_handlerzB_sentry_error_handler_lookup.<locals>.sentry_wrapped_error_handler   s     	-()<H8$!)>  '',<,D,D,O#G,,, -P' * -  	 ||~Hx(X	   '',<,D,D,O#G,,, -P''',<,D,D,O#G,,, -P'ss   DA! AA! 1DADA! D!0BC BC 1D	C
D2DDDD)r   old_error_handler_lookuprz   r{   r|   r*   r   )r/   r   r   r   r   r   s        @r1   ri   ri      ss     y!0yR4R6R ../?@H  --'0-	-* ('r3   c                   K   t        j                         j                  t              t	        | |g|i | d {   S t        j                  |      }t        j                         5 }|j                          |j                  t        |             t	        | |g|i |}t        |      r
| d {   }|cd d d        S 7 7 # 1 sw Y   y xY wwrR   )rz   r{   r|   r*   old_handle_requestr   r   r   r   r   r   r   )r/   rL   r   r   r   r   r   s          r1   rl   rl     s      ../?@H'gGGGGG;;w'L		#	#	% !!!"9,"GH%dGEdEfEx %~H 	 H & s:   ;CC-C+AC
6C7C
<CC

CCc                 f   t        | g| }t        j                         j                  t              t               5  t        j                         }t        j                  rt        j                  dk\  rv| j                  j                  j                  }|d   j                  }|j                  d|z        r|t        |      dz   d  }|j                  |t        j                         n.|j                  |d   j                   t        j                         d d d        |S |S # 1 sw Y   |S xY w)N)r7      r   z%s.   r   )old_router_getrz   r{   r|   r*   r   get_isolation_scoper:   r}   r   rx   r;   rN   r   r   r   rB   )r/   r   rvr   sanic_app_namesanic_routes         r1   rn   rn     s   		$t	$B../?@L(* 	224E'',<,D,D,O "&!2!2 ejj))%.*@A #.c..AA.E.G"HK**(9(C(C +  **qENN+<+F+F + '	. I2I/	. Is   CD&&D0c                 0   t               5  t        | t        j                         j                  ddd      \  }}|r.t        |d   d   d      r|d   d   j                  r
	 d d d        y t        j                  ||       d d d        y # 1 sw Y   y xY w)	Nr+   F)typehandled)client_options	mechanismr   r   quiet)hint)r   r   rz   r{   optionshasattrr   capture_event)r   eventr   s      r1   r   r   3  s    	$	& 
3*%002::&59
t GD,Q/9d:>Nq>Q>W>W
3 
3 	  T2
3 
3 
3s   AB,BBc                      d fd}|S )Nc                    	 |rt        |d   d   t              ry         }|| S t               5  t	        |      }|j                  |        | d   }t        |j                        }|j                  d|j                  |j                  |d<   |j                  |d<   |j                  |d<   d|j                  i|d	<   t        t        |j                               |d
<   d d d        | S # t        $ r Y w xY w# 1 sw Y   | S xY w)Nr   r   rL   z://urlquery_stringmethodREMOTE_ADDRenvr   )
issubclassr&   KeyErrorr   rJ   extract_into_eventr   r   schemenetlocr   queryr   remote_addrr   rS   r   )r   r   rL   	extractorrequest_infourlpartsr   s         r1   sanic_processorz0_make_request_processor.<locals>.sanic_processorC  s   	
4
#3A#6G .?L(* 	M-g6I((/ +L,H #L ,4>>L(%,^^L"#0'2E2E"FL&5d7??6K&LL#!	M$ 3  			M$ s   C, B1C;,	C87C8;D)r   r   r   zOptional[Hint]r@   zOptional[Event]r>   )r   r   s   ` r1   r   r   B  s    > r3   rA   )r/   r$   r@   N)rL   r   r@   NrR   )rL   r   r   zOptional[BaseHTTPResponse]r@   N)rL   r   r   r#   r   r   r@   N)
r/   r   rL   r   r   r   r   r   r@   r   )r/   r   r   zUnion[Any, Request]r@   r   )r   zUnion[ExcInfo, BaseException]r@   N)r   zCallable[[], Request]r@   r    )Rr   r   inspectr   urllib.parser   rz   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%   r9   sanic.exceptionsr&   r'   sanic.handlersr(   ImportErrorrj   r   rm   r   ro   r   rh   ru   AttributeErrorr*   rJ   r=   r<   rr   rs   rt   r   ri   rl   rn   r   r   r>   r3   r1   <module>r      s   
   !  %   U U R 9 0   !)8/FF".9/#+ (.. )) 	..K
-{ -`$, $87
7&8 BF==">=	=0W!(
!(%!(.3!(?D!(!(H
#,1=B
&: ,-3 .3 m  .
,
--.  		s   "E !E' E$'E/.E/