
    Cj5                       S SK Jr  S SKrS SKrS SKrS SKrS SKJrJr  S SK	J	r	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Jr  SSKJrJrJrJr  SSKJrJrJr  SS jr " S S\5      r " S S\5      r " S S\5      r g)    )annotationsN)ABCabstractmethod)datetimetimezone)Any   )logger   )TracingConfig)TracingProcessor)Scope)NoOpSpanSpanSpanImpl	TSpanData)	NoOpTraceTrace	TraceImplc                    SS jn U" [         5      (       a  g[         R                  " U 5        g! [         a     gf = f)zHBest-effort debug logging that tolerates closed streams during shutdown.c                    U nUb]  UR                    H)  n[        USS 5      nUc  M  [        USS5      (       d  M)    g   UR                  (       d   gUR                  nUb  M]  g)NstreamclosedFT)handlersgetattr	propagateparent)logcurrenthandlerr   s       i/home/rurouni/.local/share/pipx/venvs/strix-agent/lib/python3.13/site-packages/agents/tracing/provider.py_has_closed_stream_handler/_safe_debug.<locals>._has_closed_stream_handler   sk    ),!"++ (D9%'&(E*J*J , $$ nnG !     N)r   zlogging.Loggerreturnbool)r
   debug	Exception)messager"   s     r!   _safe_debugr*      s8    
%f--W s   0 0 
==c                  j    \ rS rSrSrS rSS jrSS jrSS jrSS jr	SS jr
SS	 jrSS
 jrS rSrg) SynchronousMultiTracingProcessor,   zN
Forwards all calls to a list of TracingProcessors, in order of registration.
c                F    SU l         [        R                  " 5       U l        g )N )_processors	threadingLock_lockselfs    r!   __init__)SynchronousMultiTracingProcessor.__init__1   s    9;^^%
r$   c                |    U R                      U =R                  U4-  sl        SSS5        g! , (       d  f       g= fzZ
Add a processor to the list of processors. Each processor will receive all traces/spans.
Nr3   r0   )r5   tracing_processors     r!   add_tracing_processor6SynchronousMultiTracingProcessor.add_tracing_processor6   s*     ZZ!2 44 ZZs   -
;c                p    U R                      [        U5      U l        SSS5        g! , (       d  f       g= fzO
Set the list of processors. This will replace the current list of processors.
N)r3   tupler0   r5   
processorss     r!   set_processors/SynchronousMultiTracingProcessor.set_processors=   s"     ZZ$Z0D ZZs   '
5c                    U R                    H  n UR                  U5        M     g! [         a'  n[        R                  " SU SU 35         SnAMF  SnAff = f)z!
Called when a trace is started.
Error in trace processor z during on_trace_start: N)r0   on_trace_startr(   r
   errorr5   trace	processores       r!   rG   /SynchronousMultiTracingProcessor.on_trace_startD   s\     ))Ia((/ *  a8C[\][^_``a   '
AAAc                    U R                    H  n UR                  U5        M     g! [         a'  n[        R                  " SU SU 35         SnAMF  SnAff = f)z"
Called when a trace is finished.
rF   z during on_trace_end: N)r0   on_trace_endr(   r
   rH   rI   s       r!   rP   -SynchronousMultiTracingProcessor.on_trace_endN   s\     ))I_&&u- *  _8CYZ[Y\]^^_rN   c                    U R                    H  n UR                  U5        M     g! [         a'  n[        R                  " SU SU 35         SnAMF  SnAff = f)z 
Called when a span is started.
rF   z during on_span_start: N)r0   on_span_startr(   r
   rH   r5   spanrK   rL   s       r!   rS   .SynchronousMultiTracingProcessor.on_span_startX   s\     ))I`''- *  `8CZ[\Z]^__`rN   c                    U R                    H  n UR                  U5        M     g! [         a'  n[        R                  " SU SU 35         SnAMF  SnAff = f)z!
Called when a span is finished.
rF   z during on_span_end: N)r0   on_span_endr(   r
   rH   rT   s       r!   rX   ,SynchronousMultiTracingProcessor.on_span_endb   s\     ))I^%%d+ *  ^8CXYZX[\]]^rN   c                    U R                    H"  n[        SU 35         UR                  5         M$     g! [         a'  n[        R
                  " SU SU 35         SnAMS  SnAff = f)z$
Called when the application stops.
zShutting down trace processor z$Error shutting down trace processor : N)r0   r*   shutdownr(   r
   rH   r5   rK   rL   s      r!   r\   )SynchronousMultiTracingProcessor.shutdownl   si     ))I8DEV""$ *  VCI;bQRPSTUUVs   4
A%A  A%c                    U R                    H  n UR                  5         M     g! [         a'  n[        R                  " SU SU 35         SnAME  SnAff = f)z.
Force the processors to flush their buffers.
zError flushing trace processor r[   N)r0   force_flushr(   r
   rH   r]   s      r!   r`   ,SynchronousMultiTracingProcessor.force_flushw   sW     ))IQ%%' *  Q>ykA3OPPQs   &
AAAr:   N)r;   r   rB   list[TracingProcessor])rJ   r   r%   None)rU   z	Span[Any]r%   rd   r%   rd   )__name__
__module____qualname____firstlineno____doc__r6   r<   rC   rG   rP   rS   rX   r\   r`   __static_attributes__r/   r$   r!   r,   r,   ,   s:    &
51a_`^	VQr$   r,   c                  L   \ rS rSrSr\SS j5       r\SS j5       r\SS j5       r\SS j5       r	\SS j5       r
\SS j5       r\SS	 j5       r\SS
 j5       r\SS j5       r\     S             SS jj5       r\   S         SS jj5       rSS jrSS jrSrg)TraceProvider   z(Interface for creating traces and spans.c                    g)z7Add a processor that will receive all traces and spans.Nr/   r5   rK   s     r!   register_processor TraceProvider.register_processor       r$   c                    g)z3Replace the list of processors with ``processors``.Nr/   rA   s     r!   rC   TraceProvider.set_processors   rs   r$   c                    g)z*Return the currently active trace, if any.Nr/   r4   s    r!   get_current_traceTraceProvider.get_current_trace   rs   r$   c                    g)z)Return the currently active span, if any.Nr/   r4   s    r!   get_current_spanTraceProvider.get_current_span   rs   r$   c                    g)z#Enable or disable tracing globally.Nr/   r5   disableds     r!   set_disabledTraceProvider.set_disabled   rs   r$   c                    g)+Return the current time in ISO 8601 format.Nr/   r4   s    r!   time_isoTraceProvider.time_iso   rs   r$   c                    g)z Generate a new trace identifier.Nr/   r4   s    r!   gen_trace_idTraceProvider.gen_trace_id   rs   r$   c                    g)zGenerate a new span identifier.Nr/   r4   s    r!   gen_span_idTraceProvider.gen_span_id   rs   r$   c                    g)z Generate a new group identifier.Nr/   r4   s    r!   gen_group_idTraceProvider.gen_group_id   rs   r$   Nc                    g)zCreate a new trace.Nr/   r5   nametrace_idgroup_idmetadatar~   tracings          r!   create_traceTraceProvider.create_trace   rs   r$   c                    g)zCreate a new span.Nr/   )r5   	span_dataspan_idr   r~   s        r!   create_spanTraceProvider.create_span   rs   r$   c                    g)zForce all registered processors to flush buffered traces/spans immediately.

The default implementation is a no-op so existing custom ``TraceProvider``
implementations continue to work without adding this method.
Nr/   r4   s    r!   r`   TraceProvider.force_flush        r$   c                    g)zClean up any resources used by the provider.

The default implementation is a no-op so existing custom ``TraceProvider``
implementations continue to work without adding this method.
Nr/   r4   s    r!   r\   TraceProvider.shutdown   r   r$   r/   )rK   r   r%   rd   )rB   rc   r%   rd   r%   zTrace | Noner%   zSpan[Any] | Noner~   r&   r%   rd   r%   strNNNFNr   r   r   
str | Noner   r   r   zdict[str, Any] | Noner~   r&   r   zTracingConfig | Noner%   r   NNF
r   r   r   r   r   zTrace | Span[Any] | Noner~   r&   r%   zSpan[TSpanData]re   )rf   rg   rh   ri   rj   r   rq   rC   rw   rz   r   r   r   r   r   r   r   r`   r\   rk   r/   r$   r!   rm   rm      s}   2F F B B 9 9 8 8 2 2 : : / / . . / /   $#*.(,	"	" 	" 		"
 (	" 	" &	" 
	" 	"  #+/!! ! )	!
 ! 
! !r$   rm   c                      \ rS rSrSS jrSS jrSS jrSS jrSS jrSS jr	SS jr
SS	 jrSS
 jrSS jrSS jr     S             SS jjr   S         SS jjrSS jrSS jrSrg)DefaultTraceProvider   c                L    [        5       U l        S U l        S U l        SU l        g )NF)r,   _multi_processor_env_disabled_manual_disabled	_disabledr4   s    r!   r6   DefaultTraceProvider.__init__   s$     @ B*.-1r$   c                :    U R                   R                  U5        gr9   )r   r<   rp   s     r!   rq   'DefaultTraceProvider.register_processor   s     	33I>r$   c                :    U R                   R                  U5        gr?   )r   rC   rA   s     r!   rC   #DefaultTraceProvider.set_processors   s     	,,Z8r$   c                ,    [         R                  " 5       $ )z-
Returns the currently active trace, if any.
)r   rw   r4   s    r!   rw   &DefaultTraceProvider.get_current_trace   s     &&((r$   c                ,    [         R                  " 5       $ )z,
Returns the currently active span, if any.
)r   rz   r4   s    r!   rz   %DefaultTraceProvider.get_current_span   s     %%''r$   c                0    Xl         U R                  5         g)z"
Set whether tracing is disabled.
N)r   _refresh_disabled_flagr}   s     r!   r   !DefaultTraceProvider.set_disabled   s     !)##%r$   c                    U R                   c6  [        R                  R                  SS5      R	                  5       S;   U l         U R
                  c  [        U R                   5      U l        gU R
                  U l        g)a  Refresh disabled flag from cached env value and manual override.

The env flag is read once on first use to avoid surprises mid-run; further env
changes are ignored after the manual flag is set via set_disabled, which always
takes precedence over the env value.
NOPENAI_AGENTS_DISABLE_TRACINGfalse)true1)r   osenvirongetlowerr   r&   r   r4   s    r!   r   +DefaultTraceProvider._refresh_disabled_flag   sh     %!#/"eg "D   (!$"4"45DN!22DNr$   c                f    [         R                  " [        R                  5      R	                  5       $ )r   )r   nowr   utc	isoformatr4   s    r!   r   DefaultTraceProvider.time_iso
  s    ||HLL)3355r$   c                F    S[         R                  " 5       R                   3$ )zGenerate a new trace ID.trace_uuiduuid4hexr4   s    r!   r   !DefaultTraceProvider.gen_trace_id  s    

(()**r$   c                L    S[         R                  " 5       R                  SS  3$ )zGenerate a new span ID.span_N   r   r4   s    r!   r    DefaultTraceProvider.gen_span_id  s#    tzz|'',-..r$   c                L    S[         R                  " 5       R                  SS  3$ )zGenerate a new group ID.group_Nr   r   r4   s    r!   r   !DefaultTraceProvider.gen_group_id  s#    

(("-.//r$   Nc           
     f   U R                  5         U R                  (       d  U(       a#  [        R                  " SU 35        [	        5       $ U=(       d    U R                  5       n[        R                  " SU SU 35        [        UUUUU R                  U(       a  UR                  S5      S9$ SS9$ )z
Create a new trace.
z(Tracing is disabled. Not creating trace zCreating trace 	 with id api_keyN)r   r   r   r   rK   tracing_api_key)	r   r   r
   r'   r   r   r   r   r   r   s          r!   r   !DefaultTraceProvider.create_trace  s     	##%>>XLLCD6JK;2t002tfIhZ@A++6=GKK	2
 	
 DH
 	
r$   c           
        U R                  5         SnSnU R                  (       d  U(       a$  [        R                  " SU 35        [	        U5      $ U(       d  [
        R                  " 5       n[
        R                  " 5       nUc!  [        R                  " S5        [	        U5      $ [        U[        5      (       d  [        U[        5      (       a(  [        R                  " SU SU S35        [	        U5      $ U(       a  UR                  OSn	UR                  n
UR                  n[        USS5      nO[        U[        5      (       ab  [        U[        5      (       a%  [        R                  " SU S35        [	        U5      $ UR                  n
Sn	UR                  n[        USS5      nO[        U[         5      (       aj  [        U[        5      (       a%  [        R                  " SU S35        [	        U5      $ UR                  n	UR                  n
UR                  nUR"                  n[        R                  " SU S	U 35        [%        W
U=(       d    U R'                  5       W	U R(                  UUUS
9$ )z
Create a new span.
Nz'Tracing is disabled. Not creating span zTNo active trace. Make sure to start a trace with `trace()` first Returning NoOpSpan.zParent z or z is no-op, returning NoOpSpanr   zCreating span r   )r   r   	parent_idrK   r   r   trace_metadata)r   r   r
   r'   r   r   rz   rw   rH   
isinstancer   r   r   r   r   r   r   r   r   r   r   )r5   r   r   r   r~   r   r   current_spancurrent_tracer   r   s              r!   r    DefaultTraceProvider.create_span8  s    	##%&*04>>XLLB9+NOI&& 113L!335M$*  	**M955LRZ9[9[l^4>[\  	**0<,,$I$--H+;;O$]JEN&&&),,wvh.KLM	**HI$44O$VZ>N%%&(++wvh.KLM	**IH$44O#22N~i[	'CD1t//1+++)
 	
r$   c                    U R                  5         U R                  (       a  g U R                  R                  5         g! [         a#  n[
        R                  " SU 35         SnAgSnAff = f)z8Force all processors to flush their buffers immediately.NzError flushing trace provider: )r   r   r   r`   r(   r
   rH   r5   rL   s     r!   r`    DefaultTraceProvider.force_flush|  sY    ##%>>	@!!--/ 	@LL:1#>??	@s   ? 
A,	A''A,c                    U R                  5         U R                  (       a  g  [        S5        U R                  R	                  5         g ! [
         a#  n[        R                  " SU 35         S nAg S nAff = f)NzShutting down trace providerz$Error shutting down trace provider: )r   r   r*   r   r\   r(   r
   rH   r   s     r!   r\   DefaultTraceProvider.shutdown  sb    ##%>>	E67!!**, 	ELL?sCDD	Es   %A
 

A7A22A7)r   r   r   r   re   )rK   r   rb   r   r   r   r   r   r   r   r   )rf   rg   rh   ri   r6   rq   rC   rw   rz   r   r   r   r   r   r   r   r   r`   r\   rk   r/   r$   r!   r   r      s    ?9)(&3&6+/0  $#*.(,

 
 	

 (
 
 &
 

B #+/B
B
 B
 )	B

 B
 
B
H	@	Er$   r   )r)   r   r%   rd   )!
__future__r   loggingr   r1   r   abcr   r   r   r   typingr   r
   configr   processor_interfacer   scoper   spansr   r   r   r   tracesr   r   r   r*   r,   rm   r   r/   r$   r!   <module>r      sk    "  	   # '   ! 1  6 6 / /2SQ'7 SQlKC K\@E= @Er$   