
    gh                        S SK r S SKrS SKrS SKrS SKrS SKrS SKJr  S SKJ	r	  S SK
JrJrJrJr  S SKJr  S SKJrJr  S SKrS SKrS SKJrJrJr  S SKJrJrJrJrJrJ r J!r!J"r"J#r#J$r$J%r%J&r&J'r'  S S	K(J)r)  \)(       a$  S S
K(J*r*  S SK(J+r+  S SK(J,r,  S SK(J-r-  S SK(J.r.  S SK/J0r0  \Rb                  " S5      r2Sr3 " S S\5      r4S r5\ Rl                    S2S j5       r7S r8S r9S r:S r;S r<S r= " S S5      r> " S S5      r?S  r@S! rASSS\R                  4S" jrCS3S# jrDS$S%.S& jrES' rFS( rGS3S) jrHS* rIS+ rJS, rKS- rLS. rMS/ rNS S0KOJPrPJQrQJRrR  \)(       a  S S1KOJSrS  gg)4    N)Mapping)	timedelta)
ROUND_DOWNDecimalDefaultContextlocalcontext)Random)quoteunquote)OPSPANDATASPANTEMPLATE)capture_internal_exceptionsfilename_for_moduleDsnloggermatch_regex_listqualname_from_function	safe_repr	to_stringtry_convertis_sentry_url_is_external_source_is_in_project_root_module_in_list)TYPE_CHECKING)Any)Dict)	Generator)Optional)Union)	FrameTypez5^[ 	]*([0-9a-f]{32})?-?([0-9a-f]{16})?-?([01])?[ 	]*$z(([a-zA-Z0-9+/]{4})*([a-zA-Z0-9+/]{2,3})?c                   2    \ rS rSr SS jrS rS rS rSrg)	EnvironHeadersA   c                     Xl         X l        g Nenvironprefix)selfr)   r*   s      [/home/kali/devsecops-assessor/venv/lib/python3.13/site-packages/sentry_sdk/tracing_utils.py__init__EnvironHeaders.__init__B   s         c                 v    U R                   U R                  UR                  SS5      R                  5       -      $ N-_)r)   r*   replaceupper)r+   keys     r,   __getitem__EnvironHeaders.__getitem__K   s.    ||DKK#++c3*?*E*E*GGHHr/   c                 8    [        S [        U 5       5       5      $ )Nc              3   &   #    U  H  nS v   M	     g7f)   N ).0r3   s     r,   	<genexpr>)EnvironHeaders.__len__.<locals>.<genexpr>Q   s     )j1js   )sumiterr+   s    r,   __len__EnvironHeaders.__len__O   s    )d4j)))r/   c              #     #    U R                    Hv  n[        U[        5      (       d  M  UR                  SS5      R	                  5       nUR                  U R                  5      (       d  M\  U[        U R                  5      S  v   Mx     g 7fr1   )r)   
isinstancestrr4   r5   
startswithr*   len)r+   ks     r,   __iter__EnvironHeaders.__iter__S   si     Aa%%		#s#))+A<<,,C$&'' s   BB
r(   N)HTTP_)	__name__
__module____qualname____firstlineno__r-   r7   rC   rK   __static_attributes__r<   r/   r,   r$   r$   A   s     I*
(r/   r$   c                     U c  g[        U R                  S5      SL=(       a-    U R                  S5      SL=(       d    U R                  S5      SL5      $ )zq
Returns True if either traces_sample_rate or traces_sampler is
defined and enable_tracing is set and not false.
NFenable_tracingtraces_sample_ratetraces_sampler)boolget)optionss    r,   has_tracing_enabledrZ   `   sY     $%U2 	
KK,-T9 9{{+,D8	 r/   c              #   j  #    [         R                  " 5       R                  S   R                  SS5      (       a  U(       a  US /:X  a  S nUS:X  a  SnOS nS n[	        X5      n0 nUb  X'S'   Ub  X7S'   U(       a  SUS	'   U(       a  U b  XS
'   [        5          [         R                  " USUS9  S S S 5        [         R                  " [        R                  UUS9 nUR                  5        H  u  pUR                  X5        M     Uv   S S S 5        g ! , (       d  f       Nj= f! , (       d  f       g = f7f)N_experimentsrecord_sql_paramsFpyformatformatz	db.paramszdb.paramstyleTzdb.executemanyz	db.cursorquery)messagecategorydata)opnameorigin)
sentry_sdk
get_clientrY   rX   _format_sqlr   add_breadcrumb
start_spanr   DBitemsset_data)cursorr`   params_list
paramstyleexecutemanyrecord_cursor_reprspan_originrc   spanrJ   vs              r,   record_sql_queriesrw   r   s!     &&~6::;NPUVVkdV3K#!J
&ED'[ *_!%f0"[	$	&!!%'M 
' 
		55
 
JJLDAMM! !

 
 
'	&
 
s6   BD3D-,D3/D"	D3
DD3"
D0,D3c                 f   UR                   [        R                  :X  a'  U R                  UR                  SSUR
                  S9  g UR                   [        R                  :X  a  S nUR                  R                  [        R                  5      nU(       a%  SUs=::  a  S::  a  O  OSnOSUs=::  a  S::  a  O  OSnU(       a  U R                  S	S
UR                  US9  g U R                  S	S
UR                  S9  g UR                   S:X  a'  U R                  SSUR                  UR                  S9  g g )Nredis)ra   typerb   rc   i  iW  errori  i  warninghttphttplib)rz   rb   rc   level)rz   rb   rc   
subprocess)rz   rb   ra   rc   )rd   r   DB_REDISrj   description_tagsHTTP_CLIENT_datarX   r   HTTP_STATUS_CODE)scoperu   r   status_codes       r,   "maybe_create_breadcrumbs_from_spanr      s   ww"++$$7W4:: 	 	
 
BNN	"jjnnX%>%>?k(S(*s*!  idjj !    fytzz R	L	 !$$	 	 	
 
!r/   c                 P     U R                   R                  $ ! [         a     g f = fr'   )f_codeco_filename	Exception)frames    r,   _get_frame_module_abs_pathr      s)    ||''' s    
%%c                     [        X5      n[        U5      =(       d    [        X5      nU (       + =(       a"    U=(       d    [        XE5      =(       a    U(       + $ r'   )r   r   r   )is_sentry_sdk_frame	namespacein_app_includein_app_excludeabs_pathproject_rootshould_be_includedshould_be_excludeds           r,   _should_be_includedr      sU     )C,X6 /; #"  	T7R@R<Rr/   c           	      B   [         R                  " 5       nUR                  5       (       d  gU R                  b  U R                  c  gUR
                  R                  SS5      nU(       d  gU R                  U R                  -
  nUR
                  R                  SS5      nU[        SS9-  U:  nU(       d  gUR
                  S   nUR
                  R                  S	5      nUR
                  R                  S
5      n[        R                  " 5       n	U	bj  [        U	5      n
 U	R                  R                  S5      nUSL=(       a    UR                  S5      n[        UUUUU
US9nU(       a  OU	R                  n	U	b  Mj  Sn	U	GbQ   U	R                   nUb*  U R#                  [$        R&                  U	R                   5         U	R                  R                  S5      nUb   U R#                  [$        R(                  U5        [        U	5      nUbz  Ub  [+        X5      nOKUbF  UR                  U5      (       a0  UR-                  US5      R/                  [0        R2                  5      nOUnU R#                  [$        R4                  U5         U	R6                  R8                  nUb5  U R#                  [$        R:                  U	R6                  R8                  5        ggg! [         a    Sn GNf = f! [         a    Sn GNgf = f! [         a    Sn GN1f = f! [         a    Sn Nf = f)z:
Adds OTel compatible source code information to the span
Nenable_db_query_sourceTdb_query_source_threshold_msr   r;   )millisecondsr   r   r   rN   zsentry_sdk.)r   r   r   r   r   r    )rg   rh   	is_active	timestampstart_timestamprY   rX   r   sys	_getframer   	f_globalsr   rH   r   f_backf_linenorn   r   CODE_LINENOCODE_NAMESPACEr   r4   lstripossepCODE_FILEPATHr   co_nameCODE_FUNCTION)ru   clientshould_add_query_sourceduration	threshold
slow_queryr   r   r   r   r   r   r   r   linenofilepathin_app_pathcode_functions                     r,   add_query_sourcer      s   
 ""$F~~!5!5!=$nn001I4P"~~ 4 44H""#A1EII155	AJ>>.1L^^''(89N^^''(89N MMOE

-e4	++J7I (t3 
	8L8L9
 1 3))%
 / 
2  	^^F MM(..?	++J7I  MM(119=-e4$1)F)h.A.A,.O.O&..|R@GGO&MM(00+>	!!LL00M $MM(00%,,2F2FG %= /  	I	4  	F	  	I	"  	! M	!sH    K K) ?K< L K&%K&)K98K9<LLLLc                    U (       d  gU R                  S5      (       a  U R                  S5      (       a  U SS n [        R                  U 5      nU(       d  gUR	                  5       u  p#nSnU(       a  SR                  [        US5      5      nU(       a  SR                  [        US5      5      nU(       a  US	:g  nUUUS
.$ )zD
Given a `sentry-trace` header string, return a dictionary of data.
Nz00-z-00   z{:032x}   z{:016x}0)trace_idparent_span_idparent_sampled)rH   endswithSENTRY_TRACE_REGEXmatchgroupsr_   int)headerr   r   r   sampled_strr   s         r,   extract_sentrytrace_datar   9  s    
 FOOE$:$:"$$V,E,1LLN)HkN##C"$56"))#nb*AB$+ (( r/   c                    S n [        U S5      (       aK  U R                  U5      n[        U[        5      (       a%  UR	                  U R
                  R                  5      nU=(       d    [        U5      $ ! [         a    S n N"f = f)Nmogrify)	hasattrr   rF   bytesdecode
connectionencodingr   r   )ro   sqlreal_sqls      r,   ri   ri   Y  sw     H69%%~~c*H(E**#??6+<+<+E+EF %y~%  s   AA4 4BBc                       \ rS rSrSrSr     SS jr\S 5       r\	S 5       r
\
R                  S 5       r
\	S	 5       r\R                  S
 5       rS rS rS rS rSrg)PropagationContextim  zB
The PropagationContext represents the data of a trace in Sentry.
	_trace_id_span_idr   r   dynamic_sampling_contextNc                 H    Xl          X l         X0l         X@l         XPl        g r'   r   )r+   r   span_idr   r   r   s         r,   r-   PropagationContext.__init__z  s:     "/:,	H -	Q )A%?r/   c                 r   S n[        U5      nUR                  [        5      nU(       a2  [        5       n[        R                  U5      R                  5       Ul        UR                  [        5      nU(       a,  [        U5      nUb  Uc
  [        5       nUR                  U5        Ub  UR                  5         U$ r'   )normalize_incoming_datarX   BAGGAGE_HEADER_NAMEr   Baggagefrom_incoming_headerr   SENTRY_TRACE_HEADER_NAMEr   update_fill_sample_rand)clsincoming_datapropagation_contextnormalized_databaggage_headersentry_trace_headersentrytrace_datas          r,   from_incoming_data%PropagationContext.from_incoming_data  s     #1-@(,,-@A"4"6;B;W;W<&&(  8 .112JK78KL+&.*<*>'#**+;<*113""r/   c                     U R                   (       d$  [        R                  " 5       R                  U l         U R                   $ )z!The trace id of the Sentry trace.)r   uuiduuid4hexrB   s    r,   r   PropagationContext.trace_id  s(     ~~!ZZ\--DN~~r/   c                     Xl         g r'   )r   r+   values     r,   r   r     s	     r/   c                     U R                   (       d'  [        R                  " 5       R                  SS U l         U R                   $ )z+The span id of the currently executed span.r   N)r   r   r   r   rB   s    r,   r   PropagationContext.span_id  s/     }} JJL,,RS1DM}}r/   c                     Xl         g r'   )r   r   s     r,   r   r     s	     r/   c                 r    UR                  5        H  u  p# [        XU5        M     g! [         a     M$  f = f)zE
Updates the PropagationContext with data from the given dictionary.
N)rm   setattrAttributeError)r+   
other_dictr6   r   s       r,   r   PropagationContext.update  s<    
 %**,JC5) - " s   (
66c                     SR                  U R                  U R                  U R                  U R                  U R
                  5      $ )Nzm<PropagationContext _trace_id={} _span_id={} parent_span_id={} parent_sampled={} dynamic_sampling_context={}>)r_   r   r   r   r   r   rB   s    r,   __repr__PropagationContext.__repr__  sD    ~  F  FNNMM))
 	
r/   c                    U R                   c  g[        [        U R                   R                  S5      5      nUb  SUs=::  a  S:  a   g  [        [        U R                   R                  S5      5      n[        U R                  U5      u  p4 [        U R                  X44S9nUS
 U R                   S'   g! [         a*    [        R                  " SU R                   SU S	35         gf = f)a  
Ensure that there is a valid sample_rand value in the dynamic_sampling_context.

If there is a valid sample_rand value in the dynamic_sampling_context, we keep it.
Otherwise, we generate a sample_rand value according to the following:

  - If we have a parent_sampled value and a sample_rate in the DSC, we compute
    a sample_rand value randomly in the range:
        - [0, sample_rate) if parent_sampled is True,
        - or, in the range [sample_rate, 1) if parent_sampled is False.

  - If either parent_sampled or sample_rate is missing, we generate a random
    value in the range [0, 1).

The sample_rand is deterministically generated from the trace_id, if present.

This function does nothing if there is no dynamic_sampling_context.
Nsample_randr   r;   sample_rateintervalz5Could not backfill sample_rand, since parent_sampled=z and sample_rate=.z.6f)r   r   r   rX   float_sample_rand_ranger   _generate_sample_randr   
ValueErrorr   debug)r+   r   r  lowerr5   s        r,   r   $PropagationContext._fill_sample_rand  s    ( ((0!T2266}E
 "qK';!'; (< "40044]C
 *$*=*={K	/WK 3 	%%m4  		
 LLGH[H[G\ ]##.-q2 		s   B4 41C('C(c                 T    U R                   c  gU R                   R                  S5      $ )zRConvenience method to get the sample_rand value from the dynamic_sampling_context.Nr   )r   rX   rB   s    r,   _sample_randPropagationContext._sample_rand  s*     ((0,,00??r/   )r   r   r   r   r   )NNNNN)rN   rO   rP   rQ   __doc__	__slots__r-   classmethodr   propertyr   setterr   r   r   r   r  rR   r<   r/   r,   r   r   m  s    I !%@6 # #2   __    ^^ 	
4
l@r/   r   c                       \ rS rSrSrSrSr\R                  " S5      r	  SS jr
\SS.S	 j5       r\S
 5       r\S 5       rS rS rSS jr\S 5       rS rS rSrg)r   i   a2  
The W3C Baggage header information (see https://www.w3.org/TR/baggage/).

Before mutating a `Baggage` object, calling code must check that `mutable` is `True`.
Mutating a `Baggage` object that has `mutable` set to `False` is not allowed, but
it is the caller's responsibility to enforce this restriction.
sentry_itemsthird_party_itemsmutablezsentry-z^sentry-c                 (    Xl         X l        X0l        g r'   r  )r+   r  r  r  s       r,   r-   Baggage.__init__.  s     )!2r/   N)r  c                   0 nSnSnU(       a  UR                  S5       H  nSU;  a  M  [        5          UR                  5       nUR                  S5      u  px[        R                  R                  U5      (       a-  [        UR                  S5      S   5      n	[        U5      X9'   SnOXD(       a  SOSU-   -  nSSS5        M     Ub  [        U5      US	'   Sn[        X4U5      $ ! , (       d  f       M  = f)
z6
freeze if incoming header already has sentry baggage
r   T,=r2   r;   FNr   )splitr   stripr   SENTRY_PREFIX_REGEXr   r   rG   )
r   r   r  r  r  r  itemr6   valbaggage_keys
             r,   r   Baggage.from_incoming_header8  s     S)d?02::<D#zz#HC2288==&-ciinQ.?&@4;CL1"')5FcBRV-VV) 32	 * #*-l*;L'G|@@ 32s   BC$$
C3	c                 &   0 nSnSn[         R                  " 5       nUR                  5       (       a  UR                  c  [	        U5      $ UR
                  nUR                  nUb  UR                  US'   UR                  S5      (       a  US   US'   UR                  S5      (       a  US   US'   UR                  S5      (       a  [        US   5      R                  US'   UR                  S5      (       a  [        US   5      US	'   [	        X#U5      $ )
Nr   Fr   environmentreleasedsn
public_keyrU   r  )rg   rh   r   _propagation_contextr   rY   r   rX   r   r)  rG   )r   r   r  r  r  r   rY   r   s           r,   from_optionsBaggage.from_options\  s    &&(!!U%?%?%G<((..#88*':'C'CL$;;}%%*1-*@L';;y!!&-i&8L#;;u),WU^)<)G)GL&;;+,,*-g6J.K*LL'|@@r/   c                 t   [         R                  " 5       n0 nUR                  5       (       d  [        U5      $ UR                  =(       d    0 nUR
                  US'   [        UR                  5      US'   UR                  S5      (       a  US   US'   UR                  S5      (       a  US   US'   UR                  S5      (       a  [        US   5      R                  US'   UR                  (       a#  UR                  [        ;  a  UR                  US'   UR                  b  [        UR                  5      US'   UR                  b  UR                  (       a  S	OS
US'   UR                   (       a@  UR                   R"                  (       a%  UR%                  UR                   R"                  5        [        USS9$ )zw
Populate fresh baggage entry with sentry_items and make it immutable
if this is the head SDK which originates traces.
r   r   r&  r'  r(  r)  transactionr  truefalsesampledFr  )rg   rh   r   r   rY   r   rG   r  rX   r   r)  re   sourceLOW_QUALITY_TRANSACTION_SOURCESr  r1  _baggager  r   )r   r.  r   r  rY   s        r,   populate_from_transaction!Baggage.populate_from_transaction}  ss    &&(!!<((..&B#.#7#7Z &)+*B*B&C]#;;}%%*1-*@L';;y!!&-i&8L#;;u),WU^)<)G)GL& ""*II*5*:*:L'"".*-k.E.E*FL'*0;0C0CfL#
 K$8$8$E$E 4 4 A AB|U33r/   c                     SU l         g )NFr2  rB   s    r,   freezeBaggage.freeze  s	    r/   c                 X    0 nU R                   R                  5        H	  u  p#X1U'   M     U$ r'   )r  rm   )r+   r   r6   r!  s       r,   r    Baggage.dynamic_sampling_context  s0    **002IC3K 3 r/   c                    / nU R                   R                  5        H]  u  p4[        5          [        R                  [        U5      -   S-   [        [        U5      5      -   nUR                  U5        S S S 5        M_     U(       a  UR                  U R                  5        SR                  U5      $ ! , (       d  f       M  = f)Nr  r  )
r  rm   r   r   SENTRY_PREFIXr
   rG   appendr  join)r+   include_third_partyrm   r6   r"  r!  s         r,   	serializeBaggage.serialize  s    ))//1HC,.,,uSz9C?%C/QT" /. 2
 LL//0xx /.s   AB11
C 	c                 P    SR                  S U R                  S5       5       5      $ )zRemove Sentry baggage from the given header.

Given a Baggage header, return a new Baggage header with all Sentry baggage items removed.
r  c              3      #    U  H;  n[         R                  R                  UR                  5       5      (       a  M7  Uv   M=     g 7fr'   )r   r   r   r  )r=   r!  s     r,   r>   /Baggage.strip_sentry_baggage.<locals>.<genexpr>  s3      -D2288F -s
   6A	A)r@  r  )r   s    r,   strip_sentry_baggageBaggage.strip_sentry_baggage  s+     xx"LL-
 	
r/   c                     [        [        U R                  R                  S5      5      nUb$  [        S5      Us=::  a  [        S5      :  a   U$   gg)zConvenience method to get the sample_rand value from the sentry_items.

We validate the value and parse it as a Decimal before returning it. The value is considered
valid if it is a Decimal in the range [0, 1).
r   Nr   r;   )r   r   r  rX   )r+   r   s     r,   r  Baggage._sample_rand  sL     "'4+<+<+@+@+OP"wqz['M71:'M (N r/   c                 B    SU R                  SS9 SU R                   S3$ )Nz
<Baggage "T)rA  z", mutable=>)rB  r  rB   s    r,   r   Baggage.__repr__  s,    DNNtNDE[QUQ]Q]P^^_``r/   )r  r  r  )r   T)F)rN   rO   rP   rQ   r  r  r>  recompiler   r-   r  r   r+  r6  r9  r   rB  staticmethodrG  r  r   rR   r<   r/   r,   r   r      s     AIM**Z0
 	 
 	!A !AF A A@ ,4 ,4\ 
 
ar/   r   c                 V    U R                   S   n[        X5      (       a  g[        XSS9$ )zq
Returns True if url matches trace_propagation_targets configured in the given client. Otherwise, returns False.
trace_propagation_targetsFT)substring_matching)rY   r   r   )r   urlrR  s      r,   should_propagate_tracerU    s/    
 !'/J KV!!CtTTr/   c                     0 nU R                  5        HD  u  p#UR                  S5      (       a  USS nUR                  SS5      R                  5       nX1U'   MF     U$ )z}
Normalizes incoming data so the keys are all lowercase with dashes instead of underscores and stripped from known prefixes.
rM      Nr3   r2   )rm   rH   r4   r
  )r   rc   r6   r   s       r,   r   r     s_    
 D#))+
>>'""ab'Ckk#s#))+S	 , Kr/   c                 0   ^ ^^^^ SSK Jm  UUU UU4S jnU$ )a  
Create a span decorator that can wrap both sync and async functions.

:param op: The operation type for the span.
:type op: str or :py:class:`sentry_sdk.consts.OP` or None
:param name: The name of the span.
:type name: str or None
:param attributes: Additional attributes to set on the span.
:type attributes: dict or None
:param template: The type of span to create. This determines what kind of
    span instrumentation and data collection will be applied. Use predefined
    constants from :py:class:`sentry_sdk.consts.SPANTEMPLATE`.
    The default is `SPANTEMPLATE.DEFAULT` which is the right choice for most
    use cases.
:type template: :py:class:`sentry_sdk.consts.SPANTEMPLATE`
r   )should_send_default_piic                   >^  [         R                  " T 5      UU UUUU4S j5       n [        R                  " T 5      Ul        [         R                  " T 5      UU UUUU4S j5       n [        R                  " T 5      Ul        [        R                  " T 5      (       a  U$ U$ ! [
         a     Nkf = f! [
         a     N;f = f)z4
Decorator to create a span for the given function.
c            
        >#    [        5       nUc0  [        R                  " S[        T
5      5        T
" U 0 UD6I S h  vN $ T=(       d    [	        T5      nT=(       d    [        T
5      =(       d    Sn[        TXA5      nT" 5       nUR                  UUS9 nUR                  T	=(       d    0 5        [        UTXdT
X5        T
" U 0 UD6I S h  vN n[        UTXh5        UsS S S 5        $  N N! , (       d  f       g = f7fNzbCannot create a child span for %s. Please start a Sentry transaction before calling this function.r   )rd   re   
get_current_spanr   r	  r   _get_span_op_get_span_namestart_childupdate_data_set_input_attributes_set_output_attributesargskwargscurrent_spanspan_opfunction_name	span_namesend_piiru   result
attributesfre   rd   rY  templates            r,   async_wrapperDcreate_span_decorator.<locals>.span_decorator.<locals>.async_wrapper"  s     ,-L#V*1-
 ////2L2G C$:1$=CM&xGI.0H)) *    !1r2%(HQ  !$1&11&tXxH  0  2 s:   9C<C'AC<5C+
C)C+C<)C++
C95C<c            
        > [        5       nUc(  [        R                  " S[        T
5      5        T
" U 0 UD6$ T=(       d    [	        T5      nT=(       d    [        T
5      =(       d    Sn[        TXA5      nT" 5       nUR                  UUS9 nUR                  T	=(       d    0 5        [        UTXdT
X5        T
" U 0 UD6n[        UTXh5        UsS S S 5        $ ! , (       d  f       g = fr\  r]  re  s            r,   sync_wrapperCcreate_span_decorator.<locals>.span_decorator.<locals>.sync_wrapperH  s     ,-L#V*1-
 $)&))2L2G C$:1$=CM&xGI.0H)) *    !1r2%(HQ D+F+&tXxH  s   A C
C#)	functoolswrapsinspect	signature__signature__r   iscoroutinefunction)ro  rq  rt  rn  re   rd   rY  rp  s   `  r,   span_decorator-create_span_decorator.<locals>.span_decorator  s     
		 	 
	@	*1*;*;A*>M' 
		 	 
	@	)0):):1)=L& &&q))  Y  		L  		s#   B" (B2 "
B/.B/2
B?>B?)sentry_sdk.scoperY  )rd   re   rn  rp  r|  rY  s   ```` @r,   create_span_decoratorr    s    ( 9U  U n r/   c                 Z    U =(       d    [         R                  " 5       n U R                  nU$ )zM
Returns the currently active span if there is one running, otherwise `None`
)rg   get_current_scoperu   )r   rh  s     r,   r^  r^  v  s&    
 3Z113E::Lr/                 ?r  c                ,   Uu  p#X#:  d  [        S5      e[        U 5      nUnXS:  a  UR                  X#5      nXS:  a  M  [        [        5       nSUl        [        U5      R                  [        S5      [        S9sSSS5        $ ! , (       d  f       g= f)a  Generate a sample_rand value from a trace ID.

The generated value will be pseudorandomly chosen from the provided
interval. Specifically, given (lower, upper) = interval, the generated
value will be in the range [lower, upper). The value has 6-digit precision,
so when printing with .6f, the value will never be rounded up.

The pseudorandom number generator is seeded with the trace ID.
z/Invalid interval: lower must be less than upper   z0.000001)roundingN)	r  r	   uniformr   r   precr   quantizer   )r   r  r
  r5   rngr   ctxs          r,   r  r    s     LE=JKK

CK

kk%/ 
 
n	%{#,,J - 
 
&	%	%s   -B
Bc                 *    U b  Uc  gU SL a  SU4$ US4$ )z
Compute the lower (inclusive) and upper (exclusive) bounds of the range of values
that a generated sample_rand value must fall into, given the parent_sampled and
sample_rate values.
r  Tr  r  r<   )r   r  s     r,   r  r    s0     !4	4	KCr/   c                     Sn[        U [        5      (       a  U R                  U5      nU$ [        X5      (       a   [	        X5      nU$ U$ ! [
         a    Sn U$ f = f)z
Gets a value from a source object. The source can be a dict or an object.
It is checked for dictionary keys and object attributes.
N)rF   dictrX   r   getattrr   )r3  r6   r   s      r,   
_get_valuer    sl     E&$

3 L 6, L5L  Ls   A AAc                 L   UnU [         R                  :X  aY  SnU(       a>  S H8  nUR                  U5      (       d  M  [        X%   [        5      (       d  M4  X%   n  O   U(       a  SU 3nU$ SnU$ U [         R
                  :X  a  SU 3nU$ U [         R                  :X  a  SU 3nU$ )z>
Get the name of the span based on the template and the name.
N)model
model_namezchat chatzinvoke_agent zexecute_tool )r   AI_CHATrX   rF   rG   AI_AGENTAI_TOOL)rp  re   rg  rk  r  r6   s         r,   r`  r`    s    
 I<'''.::c??z&+s'C'C"KE /
 (-eE7O	  39	  
\**	*#D6*	
  
\))	)#D6*	r/   c                    [         R                  [        R                  [         R                  [        R
                  [         R                  [        R                  0nUR                  U [        R                  5      n[        U5      $ )z6
Get the operation of the span based on the template.
)r   r  r   GEN_AI_CHATr  GEN_AI_INVOKE_AGENTr  GEN_AI_EXECUTE_TOOLrX   FUNCTIONrG   )rp  mappingrd   s      r,   r_  r_    s[     	bnnr55b44G
 
Xr{{	+Br7Nr/   c                   ^^ 0 mU [         R                  [         R                  [         R                  4;   Ga  [        R
                  [        4[        R
                  [        4[        R                  [        4[        R                  [        4[        R                  [        4[        R                  [        4[        R                  [        4[        R                  [        4[        R                  [        4[        R                  [        4S.
mUU4S jn[!        UR#                  5       5       H  u  pVUS:X  aI  [%        U[        5      (       a4  TR'                  [        R(                  / 5      R+                  SUS.5        MT  US:X  aI  [%        U[        5      (       a4  TR'                  [        R(                  / 5      R+                  SUS.5        M  U" XV5        M     U [         R                  :X  a%  U(       a  [-        X#S.5      T[        R.                  '   [        R(                  T;   a-  [-        T[        R(                     5      T[        R(                  '   T$ )	z3
Get input attributes for the given span template.
)
r  r  agent
agent_name
max_tokensfrequency_penaltypresence_penaltytemperaturetop_ptop_kc                 V   > U T;   a"  TU    u  p#Ub  [        X5      (       a  UTU'   g g g g r'   )rF   )r6   r   	attribute	data_typern  r  s       r,   _set_from_key,_get_input_attributes.<locals>._set_from_key  s>    g~)0&$E)E)E,1Jy) *F$ r/   promptuser)rolecontentsystem_promptsystem)rf  rg  )r   r  r  r  r   GEN_AI_REQUEST_MODELrG   GEN_AI_AGENT_NAMEGEN_AI_REQUEST_MAX_TOKENSr    GEN_AI_REQUEST_FREQUENCY_PENALTYr  GEN_AI_REQUEST_PRESENCE_PENALTYGEN_AI_REQUEST_TEMPERATUREGEN_AI_REQUEST_TOP_PGEN_AI_REQUEST_TOP_Klistrm   rF   
setdefaultGEN_AI_REQUEST_MESSAGESr?  r   GEN_AI_TOOL_INPUT)	rp  rl  rf  rg  r  r6   r   rn  r  s	          @@r,   _get_input_attributesr    s   
 JL))<+?+?AUAUVV33S9#88#>00#6#55s;#==sC"*"K"KU!S!)!I!I5 Q$??G33U;33S9
	2 v||~.JCh:eS#9#9%%h&F&FKRR#6 o%*UC*@*@%%h&F&FKRR%%8 #% / <'''H1:,2

8--.
 '':57@x7788

8334 r/   c                    ^ ^ 0 mUU 4S jnU" [         R                  S5        U" [         R                  S5        U" [         R                  S5        T$ )z
Get usage attributes.
c                 r   > U H0  n[        TU5      nUc  M  [        U[        5      (       d  M+  UTU '   M2     g r'   )r  rF   r   )r  keysr6   r   rn  usages       r,   _set_from_keys-_get_usage_attributes.<locals>._set_from_keys-  s5    Cuc*E Zs%;%;(-
9% r/   )prompt_tokensinput_tokens)completion_tokensoutput_tokens)total_tokens)r   GEN_AI_USAGE_INPUT_TOKENSGEN_AI_USAGE_OUTPUT_TOKENSGEN_AI_USAGE_TOTAL_TOKENS)r  r  rn  s   ` @r,   _get_usage_attributesr  &  sY    
 J. **) ++. **
 r/   c                    0 nU [         R                  [         R                  [         R                  4;   a  [	        5          U/n[        US5      nUR                  U5        [        US5      n[        US5      nUR                  U5        U H"  nUc  M  UR                  [        U5      5        M$     [        US5      nUb'  [        U[        5      (       a  X[        R                  '   [        US5      nUb'  [        U[        5      (       a  X[        R                  '   SSS5        U [         R                  :X  a#  U(       a  [        U5      U[        R                  '   U$ ! , (       d  f       NG= f)z4
Get output attributes for the given span template.
r  metadataNr  r  )r   r  r  r  r   r  r?  r   r  rF   rG   r   GEN_AI_RESPONSE_MODELr   GEN_AI_TOOL_OUTPUT)	rp  rl  rm  rn  usage_candidatesr  metausage_candidater  s	            r,   _get_output_attributesr  D  s1   
 JL))<+?+?AUAUVV(* &xvw/E##E*fj1DtW-E##E*#3".%%&;O&LM $4
 $FG4J%*Z*E*E=G899:#FL9J%*Z*E*E=G899:- +2 <'''H2;F2C
8../9 +*s    AEB
E
E-c                    0 nU[         R                  :X  a#  [        R                  S[        R                  U0nO~U[         R
                  :X  a  [        R                  S0nOWU[         R                  :X  aC  [        R                  S[        R                  U0nUR                  nUb  X[        R                  '   UR                  [        XXV5      5        U R                  U=(       d    0 5        g)ak  
Set span input attributes based on the given span template.

:param span: The span to set attributes on.
:param template: The template to use to set attributes on the span.
:param send_pii: Whether to send PII data.
:param f: The wrapped function.
:param args: The arguments to the wrapped function.
:param kwargs: The keyword arguments to the wrapped function.
invoke_agentr  execute_toolN)r   r  r   GEN_AI_OPERATION_NAMEr  r  r  GEN_AI_TOOL_NAMEr  GEN_AI_TOOL_DESCRIPTIONr   r  rb  )	ru   rp  rl  re   ro  rf  rg  rn  	docstrings	            r,   rc  rc  k  s     J<(((**N&&

 
\))	)**F

 
\))	)**N%%t


 II	 ;Dx778+HMNZ%2&r/   c                 L    U R                  [        XU5      =(       d    0 5        g)a  
Set span output attributes based on the given span template.

:param span: The span to set attributes on.
:param template: The template to use to set attributes on the span.
:param send_pii: Whether to send PII data.
:param result: The result of the wrapped function.
N)rb  r  )ru   rp  rl  rm  s       r,   rd  rd    s     	+HGM2Nr/   )r   r4  r   )Span)Fmanualr'   )T
contextlibrv  rx  r   rN  r   collections.abcr   datetimer   decimalr   r   r   r   randomr	   urllib.parser
   r   r   rg   sentry_sdk.constsr   r   r   sentry_sdk.utilsr   r   r   r   r   r   r   r   r   r   r   r   r   typingr   r   r   r   r    r!   typesr"   rO  r   base64_strippedr$   rZ   contextmanagerrw   r   r   r   r   r   ri   r   r   rU  r   DEFAULTr  r^  r  r  r  r`  r_  r  r  r  rc  rd  sentry_sdk.tracingr   r4  r   r  r<   r/   r,   <module>r     ss      	 	 
 #  E E  '   8 8     !  ZZ  (W (>$  , ,^
@(XHv@&(p@ p@fFa FaR
U" $4,2F2Fm`  
F $46r<$N"'J
O  ' r/   