
    &`iJ                        d dl Z d dlZd dlZd dl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mZmZmZmZmZmZmZmZ d dlZd dlZd dlmZmZmZ d dlmZ  ej        e          Z  G d	 d
          Z!da"da#de$fdZ%d Z&dee         fdZ'dedefdZ( G d de)          Z*dee
e+f         de
fdZ, G d d          Z-eddded         fd            Z.de+fdZ/d ed!ef         de+fd"Z0d ed!ef         de+fd#Z1d$ee+ed!ef         f         d%ee+ed!ef         f         fd&Z2d$ee+ed!ef         f         d%ee+ed!ef         f         de+fd'Z3d$ee+ed!ef         f         d%ee+ed!ef         f         de+fd(Z4d) Z5d* Z6d+ Z7d, Z8d- Z9dS ).    N)contextmanager)wraps)	Parameter)
ModuleType)
AnyCallableDict	GeneratorListMutableMappingOptionalSequenceUnioncast)is_class_methodis_function_or_methodis_static_method)get_runtime_contextc                   D    e Zd ZdZh dZd Zd Zd Zd Zd Z	d Z
d	 Zd
S )_OpenTelemetryProxya  
    This proxy makes it possible for tracing to be disabled when opentelemetry
    is not installed on the cluster, but is installed locally.

    The check for `opentelemetry`'s existence must happen where the functions
    are executed because `opentelemetry` may be present where the functions
    are pickled. This can happen when `ray[full]` is installed locally by `ray`
    (no extra dependencies) is installed on the cluster.
    >   traceContextcontext	propagatec                 x    |t           j        v r t          | d|                       S t          d|           )N_zAttribute does not exist: )r   allowed_functionsgetattrAttributeError)selfnames     s/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/util/tracing/tracing_helper.py__getattr__z_OpenTelemetryProxy.__getattr__/   sF    &888,74T,,... !Dd!D!DEEE    c                 ,    |                      d          S )Nzopentelemetry.trace_try_importr    s    r"   _tracez_OpenTelemetryProxy._trace5   s     5666r$   c                 ,    |                      d          S )Nzopentelemetry.contextr&   r(   s    r"   _contextz_OpenTelemetryProxy._context8   s     7888r$   c                 ,    |                      d          S )Nzopentelemetry.propagater&   r(   s    r"   
_propagatez_OpenTelemetryProxy._propagate;   s     9:::r$   c                 J    |                                  }|r|j        j        S d S N)r+   r   r   )r    r   s     r"   _Contextz_OpenTelemetryProxy._Context>   s'    --// 	?**4r$   c                     |                                   |                                  |                                  |                                  d S r/   )r)   r+   r-   r0   r(   s    r"   try_allz_OpenTelemetryProxy.try_allE   s=    r$   c                     	 t          j        |          S # t          $ r! t                      rt          d          Y d S w xY w)Na  Install OpenTelemetry with 'pip install opentelemetry-api==1.34.1 opentelemetry-sdk==1.34.1 opentelemetry-exporter-otlp==1.34.1' to enable tracing. See the Ray documentation for details: https://docs.ray.io/en/latest/ray-observability/user-guides/ray-tracing.html#installation)	importlibimport_moduleImportError_is_tracing_enabled)r    modules     r"   r'   z_OpenTelemetryProxy._try_importK   sh    		*6222 	 	 	"$$ !p    	s    'A AN)__name__
__module____qualname____doc__r   r#   r)   r+   r-   r0   r2   r'    r$   r"   r   r   "   s          EDDF F F7 7 79 9 9; ; ;    
 
 
 
 
r$   r   Freturnc                      t           S )zgChecks environment variable feature flag to see if tracing is turned on.
    Tracing is off by default.)_global_is_tracing_enabledr=   r$   r"   r7   r7   \   s
     &%r$   c                  X    da t                      at                                           d S )NT)r@   r   _opentelemetryr2   r=   r$   r"   _enable_tracingrC   b   s+    !%(**Nr$   params_listc                     t          |           D ]D\  }}|j        t          j        k    r*|                     |                     |                      nE| S )z]Given a list of Parameters, if a kwargs Parameter exists,
    move it to the end of the list.)	enumeratekindr   VAR_KEYWORDappendpop)rD   iparams      r"   _sort_params_listrM   i   sa     k**  5:...{q11222E / r$   function	new_paramc                    t          j        |           }t          |j                                                  }t          fd|D                       r|S t          |gz             }|                    |          }|S )z/Add additional Parameter to function signature.c              3   8   K   | ]}|j         j         k    V  d S r/   )r!   ).0rL   rO   s     r"   	<genexpr>z*_add_param_to_signature.<locals>.<genexpr>x   s,      
G
GE5:'
G
G
G
G
G
Gr$   )
parameters)inspect	signaturelistrT   valuesanyrM   replace)rN   rO   old_sigold_sig_list_repr
new_paramsnew_sigs    `    r"   _add_param_to_signaturer_   s   s    ))GW/668899

G
G
G
G5F
G
G
GGG "#4	{#BCCJooo44GNr$   c                       e Zd ZdS )_ImportFromStringErrorN)r9   r:   r;   r=   r$   r"   ra   ra      s        Dr$   ra   
import_strc                 .   t          | t                    s| S |                     d          \  }}}|r|s%d}t          |                    |                     	 t          j        |          }nE# t          $ r8}|j        |k    r|dd}t          |                    |                    d}~ww xY w|}	 |	                    d          D ]}t          ||          }n4# t          $ r' d}t          |                    ||	                    w xY w|S )
zRGiven a string that is in format "<module>:<attribute>",
    import the attribute.:zEImport string "{import_str}" must be in format"<module>:<attribute>".)rb   Nz'Could not import module "{module_str}".)
module_str.z;Attribute "{attrs_str}" not found in module "{module_str}".)	attrs_strre   )
isinstancestr	partitionra   formatr4   r5   r6   r!   splitr   r   )	rb   re   r   rg   messager8   excinstanceattr_strs	            r"   _import_from_stringrq      sg    j#&& )33C88J9 LY LV 	 %W^^z^%J%JKKKL(44 L L L8z!!4;$W^^z^%J%JKKK	L H
!,, 	3 	3Hx22HH	3 
 
 
O$NNY:NFF
 
 	

 Os$   A0 0
B2:3B--B28(C! !1Dc                   J    e Zd Zdeeef         fdZdeeef         ddfdZdS )_DictPropagatorr>   c                  H    i } t           j                            |            | S )z*Inject trace context into otel propagator.)rB   r   injectcontext_dicts    r"   inject_current_contextz&_DictPropagator.inject_current_context   s$    ') ''555r$   rw   _opentelemetry.Contextc                 p    t          t          j        t          j                            |                     S )z,Given a trace context, extract as a Context.)r   rB   r   r   extractrv   s    r"   r{   z_DictPropagator.extract   s-    "N$<$D$D\$R$R
 
 	
r$   N)r9   r:   r;   r	   r   rx   r{   r=   r$   r"   rs   rs      sb        DcN    
d38n 
1I 
 
 
 
 
 
r$   rs   parent_contextry   )NNNc              #     K   | | }nt                                           }t           j                            |          }	 dV  t           j                            |           dS # t           j                            |           w xY w)z(Uses the Ray trace context for the span.N)rB   r   r   attachdetach)r|   new_contexttokens      r"   _use_contextr      s      
 !$$,,.."))+66E-%%e,,,,,%%e,,,,s   A& &!Bfunction_namec                    t                      }d| t          t          j                              |                                |                                d}t          j        j        j	        j
        t          j        j        j        k    r|                                }|r||d<   t          t          j        j        j	        dd          }|r|                                |d<   |S )zXGet the Attributes of the function that will be reported as attributes
    in the trace.rN   )
ray.remoteray.functionray.pid
ray.job_idray.node_idzray.task_id	worker_idNray.worker_id)r   ri   osgetpid
get_job_idget_node_idray_privateworkerglobal_workermodeWORKER_MODEget_task_idr   hex)r   runtime_context	span_argstask_idr   s        r"   _function_hydrate_span_argsr      s     *++O !%ry{{##%0022&2244 I |(-1D1PPP!--// 	/'.Im$+9;MMI 5%.]]__	/"r$   func.c                     |  dS )z>Returns the function span name that has span kind of producer. ray.remoter=   r   s    r"   _function_span_producer_namer      s    r$   c                     |  dS )z>Returns the function span name that has span kind of consumer. ray.remote_workerr=   r   s    r"   _function_span_consumer_namer      s    &&&&r$   class_methodc                 4   t          |           r| j        } t          |          r|j        }t                      }d| ||  d| t          t	          j                              |                                |                                d}t          j	        j
        j        j        t          j	        j
        j        k    r|                                }|r||d<   t          t          j	        j
        j        dd          }|r|                                |d<   |S )zUGet the Attributes of the actor that will be reported as attributes
    in the trace.actorrf   )r   zray.actor_classzray.actor_methodr   r   r   r   ray.actor_idr   Nr   )callabler9   r   ri   r   r   r   r   r   r   r   r   r   r   get_actor_idr   r   )r   r   r   r   actor_idr   s         r"   _actor_hydrate_span_argsr      s     ! !)++O!"!,,F,,ry{{##%0022&2244 I |(-1D1PPP"//11 	1(0In%+9;MMI 5%.]]__	/"r$   c                     t          | t                    s| j        } t          |t                    s|j        }|  d| dS )z;Returns the actor span name that has span kind of producer.rf   r   rh   ri   r9   r   r   s     r"   _actor_span_producer_namer     sM    
 fc"" !fc"" !**v****r$   c                     t          | t                    s| j        } t          |t                    s|j        }|  d| dS )z;Returns the actor span name that has span kind of consumer.rf   r   r   r   s     r"   _actor_span_consumer_namer     sM    
 fc"" !fc"" !11v1111r$   c                      t                     	 	 ddt          dt          t          t          f         dt          dt          dt          f
 fd            }|S )	zfTrace the execution of a remote task. Inject
    the current span context into kwargs for propagation.Nargskwargs_args_kwargsr>   c                    t                      r| j        r|d|vsJ  | ||g|R i |S d|vsJ t          j                            t
                    }|                    t          | j                  t          j        j	        j
        t          | j                            5  t                                          |d<    | ||g|R i |cd d d            S # 1 swxY w Y   d S )N_ray_trace_ctxrG   
attributes)r7   _is_cross_languagerB   r   
get_tracerr9   start_as_current_spanr   _function_nameSpanKindPRODUCERr   rs   rx   )r    r   r   r   r   tracerr   s         r"   _invocation_remote_spanz9_tracing_task_invocation.<locals>._invocation_remote_span)  sv    #$$ 	A(? 	A!'v55556$f@u@@@@@@v----%00::))()<==%.7243FGG * 
 
 	A 	A (7'M'M'O'OF#$6$f@u@@@@@	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	As   '*CC"%C"NN)r   r   r   )r   r   s   ` r"   _tracing_task_invocationr   %  s     6]] +/A AA sCx(A 	A
 A 
A A A A A ]A2 #"r$   c           
      H    t                      s S t           t          j        dt          j        j        d                     _        t                     dddt          dt          t          t          t          f                  dt          dt          f fd            }|S )	zWrap the function argument passed to RemoteFunction's __init__ so that
    future execution of that function will include tracing.
    Use the provided trace context from kwargs.
    r   Ndefaultr   r   r   r>   c                    |  |i |S t           j                            t                    }j        dz   j        z   }t          t                              |                     5  |                    t          |          t           j        j
        j        t          |                    5   |i |cd d d            cd d d            S # 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nrf   r   )rB   r   r   r9   r:   r   rs   r{   r   r   r   CONSUMERr   )r   r   r   r   r   rN   s        r"   _function_with_tracingz=_inject_tracing_into_function.<locals>._function_with_tracingU  s    !8T,V,,,%00:: +c1H4EE ##N33
 
 	- 	-''(77%.72=AA ( 
 
	- 	- 8T,V,,	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	-s7   )AC)0C8C)C	C)C	C))C-0C-)r7   r_   rU   r   KEYWORD_ONLY__signature__r   r   r   r	   ri   )rN   r   s   ` r"   _inject_tracing_into_functionr   F  s    
    4g/<d	
 	
 	
 H 8__ 48- - -- c3h0- - 
	- - - - - _-* "!r$   c           
           t                     t                      dfdt          dt          t          t          f         dt          dt          f fd            }|S )z`Trace the creation of an actor. Inject
    the current span context into kwargs for propagation.Nr   r   r   r   c                 6   |i }t                      sd|vsJ  
| ||g|R i |S | j        j        }d}d|vsJ t          j                            t                    }|                    t          ||          t          j        j	        j
        t          ||                    5 }t                                          |d<    
| ||g|R i |}	|                    d|	j                                                   |	cd d d            S # 1 swxY w Y   d S )Nr   __init__r!   rG   r   r   )r7   __ray_metadata__
class_namerB   r   r   r9   r   r   r   r   r   rs   rx   set_attribute_ray_actor_idr   )r    r   r   r   r   r   method_namer   spanresultr   s             r"   #_invocation_actor_class_remote_spanzD_tracing_actor_creation.<locals>._invocation_actor_class_remote_spanr  s    >F #$$ 	A#611116$f@u@@@@@@*5
 w....%00::))*:{CC%.7/
KHH * 
 
 	 '6'M'M'O'OF#$VD$BBBB'BBF~v/C/G/G/I/IJJJ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   (ADDD)r   tupler   r   )r   r   s   ` r"   _tracing_actor_creationr   n  s|     6]] GG+/  sCx( 	
      ]B /.r$   c                      t                     	 	 ddt          t                   dt          t          t          f         dt          dt          dt          f
 fd            }|S )	z(Trace the invocation of an actor method.Nr   r   r   r   r>   c                 d   t                      r| j        j        r|d|vsJ  	| ||g|R i |S | j        j        j        }| j        }d|vsJ t          j                            t                    }|
                    t          ||          t          j        j        j        t          ||                    5 }t                                          |d<   |                    d| j        j                                                    	| ||g|R i |cd d d            S # 1 swxY w Y   d S )Nr   r   r   )r7   _actor_ray_is_cross_language'_ray_actor_creation_function_descriptorr   _method_namerB   r   r   r9   r   r   r   r   r   rs   rx   r   r   r   )
r    r   r   r   r   r   r   r   r   r   s
            r"   _start_spanz5_tracing_actor_method_invocation.<locals>._start_span  s    #$$ 	A(J 	A!'v55556$f@u@@@@@@[HS
'w....%00::))*:{CC%.7/
KHH * 
 
 
	A '6'M'M'O'OF#$~t{/H/L/L/N/NOOO6$f@u@@@@@
	A 
	A 
	A 
	A 
	A 
	A 
	A 
	A 
	A 
	A 
	A 
	A 
	A 
	A 
	A 
	A 
	A 
	As   <AD%%D),D)r   )r   r   r   r   )r   r   s   ` r"    _tracing_actor_method_invocationr     s     6]] #+/A AsmA sCx(A 	A
 A 
A A A A A ]A< r$   c           	         dt           dt          f         dt          fd}dt           dt          f         dt          fd}t          j        | t                    }|D ]\  }}t          | |          st          |          r%t          j        |          st          j        |          rN|dk    rUt          |t          j
        dt          j
        j        d	                    |_        t          j        |          r" t          |           ||                    }n! t          |           ||                    }t          | ||           | S )
zZGiven a class that will be made into an actor,
    inject tracing into all of the methods.r   .r>   c                      d ddt           dt           dt          t          t          t           f                  dt           dt           f
 fd}|S )Nr   r    r   r   r   r>   c          	          t                      r| | g|R i |S t          j                            t                    }t          t                              |                    5  |                    t          | j
        j                  t          j        j        j        t          | j
        j                            5   | g|R i |cddd           cddd           S # 1 swxY w Y   ddd           dS # 1 swxY w Y   dS zr
            Wrap the user's function with a function that
            will extract the trace context
            Nr   r7   rB   r   r   r9   r   rs   r{   r   r   	__class__r   r   r   r    r   r   r   r   r   s        r"   _resume_spanzF_inject_tracing_into_class.<locals>.span_wrapper.<locals>._resume_span  s    '(( 7N,Bvd6U666g6662@2F2Q2Q3 3F ''77  7 7++)$.*A6JJ#)2;3DN4KVTT ,  7 7 vd6U666g667 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7s7   )ADC+D+C/	/D2C/	3DD
Dr   r   r	   ri   r   r   s   ` r"   span_wrapperz0_inject_tracing_into_class.<locals>.span_wrapper  ss     8<	7 	7 	7	7	7 %T#s(^4	7 		7
 	7 	7 	7 	7 	7 	7: r$   c                      d ddt           dt           dt          t          t          t           f                  dt           dt           f
 fd}|S )Nr   r    r   r   r   r>   c          	      P  K   t                      r| | g|R i | d{V S t          j                            t                    }t          t                              |                    5  |                    t          | j
        j        j                  t          j        j        j        t          | j
        j        j                            5   | g|R i | d{V cddd           cddd           S # 1 swxY w Y   ddd           dS # 1 swxY w Y   dS r   r   r   s        r"   r   zL_inject_tracing_into_class.<locals>.async_span_wrapper.<locals>._resume_span  s      '(( =N,B#VD<5<<<G<<<<<<<<<#)44X>>F ''77  	= 	=++)$.*A6?SS#)2;3N+V_  ,  	= 	= $VD<5<<<G<<<<<<<<	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	=s7   1A'DD*DD	D
D	DD"Dr   r   s   ` r"   async_span_wrapperz6_inject_tracing_into_class.<locals>.async_span_wrapper  ss     8<	= 	= 	=	=	= %T#s(^4	= 		=
 	= 	= 	= 	= 	= 	=: r$   __del__r   Nr   )r   r   rU   
getmembersr   r   r   isgeneratorfunctionisasyncgenfunctionr_   r   r   r   iscoroutinefunctionr   setattr)_clsr   r   methodsr!   r   wrapped_methods          r"   _inject_tracing_into_classr     s   Xc3h/ C    @8CH#5 #    @  '<==G &, &,f D$'' 	?6+B+B 	&v.. 	'2LV2T2T 	  9  7 '"3"@$   
  
 &v.. 	A*U6]]+=+=f+E+EFFNN*U6]]<<+?+?@@NdN++++Kr$   ):r4   rU   loggingr   
contextlibr   	functoolsr   r   typesr   typingr   r   r	   r
   r   r   r   r   r   r   r   ray._private.workerray._private.inspect_utilr   r   r   ray.runtime_contextr   	getLoggerr9   loggerr   r@   rB   boolr7   rC   rM   r_   	Exceptionra   ri   rq   rs   r   r   r   r   r   r   r   r   r   r   r   r   r=   r$   r"   <module>r
     sr         				 % % % % % %                                          


             
 4 3 3 3 3 3		8	$	$3 3 3 3 3 3 3 3l # &T & & & &  4	?    	h 	9 	 	 	 		 	 	 	 	Y 	 	 	E*c/$: z    D
 
 
 
 
 
 
 
 -,- - - - -s    4 xS'9  c        
'xS'9 'c ' ' ' '
!#xS))*!#xS))*! ! ! !H
+#xS))*
+#xS))*
+ 	
+ 
+ 
+ 
+
2#xS))*
2#xS))*
2 	
2 
2 
2 
2# # #B%" %" %"P&/ &/ &/R" " "Jm m m m mr$   