
    tni                        d dl Z d dlZd dlZd dlZd dlmZmZ d dlmZm	Z	m
Z
 d dlmZ d dlmZmZmZmZmZ 	 d dlZn# e$ r  e	d          w xY wd dlmZ erd dlmZ d d	lmZmZ d d
lmZ ddZddZddZddZ G d de
          Z dS )    N)OP
SPANSTATUS)_check_minimum_versionDidNotEnableIntegration)TransactionSource)event_from_exceptionloggerpackage_versionqualname_from_functionreraisezRay not installed.)TYPE_CHECKING)Callable)AnyOptional)ExcInforeturnc                  |    t          j                                                    rd S t          j        d           d S )Nz[[Tracing] Sentry not initialized in ray cluster worker, performance data will be discarded.)
sentry_sdk
get_client	is_activer
   debug     X/var/www/html/gpu-tools/venv/lib/python3.11/site-packages/sentry_sdk/integrations/ray.py_check_sentry_initializedr      sE    ((** 
Le    r   funcCallable[..., Any]c                    t          j        |           }t          |j                                                  }t          j        dt           j        j        d           }|r5|d         j        t           j        j        u r|	                    d|           n|
                    |           |                    |          | _        d S )N_sentry_tracing)kinddefault)
parameters)inspect	signaturelistr$   values	ParameterKEYWORD_ONLYr!   VAR_KEYWORDinsertappendreplace__signature__)r   r&   paramssentry_tracing_params       r   #_insert_sentry_tracing_in_signaturer2   '   s     !$''I)&--//00F",+    ,&*/W%6%BBBb.////*+++"**f*==Dr   c                      t           j        t          j                  	 d		 	 	 	 	 	 	 d
fd            } | t           _        d S )NfOptional[Callable[..., Any]]argsr   kwargsr   r   c                 z     t          j                   r  gR i S d fd}  |           S |S )Nuser_fr   r   r   c                 <    t          j                   r  i            S t          j                   d d	 	 	 	 	 	 	 d fd            }t	          |           r |          }n  i |          }|j        	 	 	 	 	 	 d fd}||_        |S )N)r    f_argsr   r    Optional[dict[str, Any]]f_kwargsr   c                    t                       t          j        | pi t          j        t                    t          j        t          j	                  }t          j
        |          5 }	  |i |}|                    t          j                   nZ# t          $ rM |                    t          j                   t!          j                    }t%          |           t'          |  Y nw xY w|cd d d            S # 1 swxY w Y   d S )N)opnameoriginsource)r   r   continue_tracer   QUEUE_TASK_RAYr   RayIntegrationrA   r   TASKstart_transaction
set_statusr   OK	ExceptionINTERNAL_ERRORsysexc_info_capture_exceptionr   )r    r;   r=   transactionresultrM   r9   s         r   new_funczH_patch_ray_remote.<locals>.new_remote.<locals>.wrapper.<locals>.new_funcQ   sT    *+++(7#)r(/77)0,1    1+>> 
"++!'!<8!<!<#..z}====$ + + +#..z/HIII#&<>>*8444****	+ "
" 
" 
" 
" 
" 
" 
" 
" 
" 
" 
" 
" 
" 
" 
" 
" 
" 
"s7   *C<,'BC<AC+(C<*C++C<<D D r6   r7   c                     t          j        t          j        t	                    t
          j                  5 }d t          j                                                    D             }	  | i |d|i}|	                    t          j                   nZ# t          $ rM |	                    t          j                   t          j                    }t!          |           t#          |  Y nw xY w|cddd           S # 1 swxY w Y   dS )z,
                Ray Client
                )r?   r@   rA   c                     i | ]\  }}||	S r   r   ).0kvs      r   
<dictcomp>zz_patch_ray_remote.<locals>.new_remote.<locals>.wrapper.<locals>._remote_method_with_header_propagation.<locals>.<dictcomp>   s.        Aq 1  r   r    N)r   
start_spanr   QUEUE_SUBMIT_RAYr   rE   rA   get_current_scopeiter_trace_propagation_headersrH   r   rI   rJ   rK   rL   rM   rN   r   )r6   r7   spantracingrP   rM   old_remote_methodr9   s         r   &_remote_method_with_header_propagationzf_patch_ray_remote.<locals>.new_remote.<locals>.wrapper.<locals>._remote_method_with_header_propagationu   sw     **/77)0   "  $.$@$B$B$a$a$c$c  G	+!2!2!"%+" "=D" " " 
6666$ + + +
(ABBB#&<>>*8444****	+ ")" " " " " " " " " " " " " " " " " "s6   0C?++BC?AC.+C?-C..C??DD)r;   r   r    r<   r=   r   r   r   )r6   r   r7   r   r   r   )r%   isclass	functoolswrapsr2   remote)	r9   rQ   rvr_   r^   r6   r4   r7   
old_remotes	   `   @r   wrapperz6_patch_ray_remote.<locals>.new_remote.<locals>.wrapperI   s2   v&& ;
 3zz426226:::_V$$ ?C" ""!;" "" 	" " " " " %$"6 0999 ;Z))0ZZ000:: "	""(-"" " " " " " "8 ?BIIr   )r9   r   r   r   )r%   r`   )r4   r6   r7   rf   re   s   ``` r   
new_remotez%_patch_ray_remote.<locals>.new_remote>   s     ?1 	2
 :a1$111&111J	 J	 J	 J	 J	 J	 J	 J	 J	X =71::Nr   )N)r4   r5   r6   r   r7   r   r   r   )rayrc   ra   rb   )rg   re   s    @r   _patch_ray_remoteri   ;   sx    J_Z  ,0Y)Y9>YJOY	Y Y Y Y Y ! Yv CJJJr   rM   r   r7   r   c                     t          j                    }t          | |j        dt          j        d          \  }}t          j        ||           d S )NF)handledtype)client_options	mechanism)hint)r   r   r	   optionsrE   
identifiercapture_event)rM   r7   clienteventro   s        r   rN   rN      sa    "$$F&~"-
 
  KE4 U......r   c                   4    e Zd ZdZde Zedd            ZdS )rE   rh   zauto.queue.r   Nc                  j    t          d          } t          t          |            t                       d S )Nrh   )r   r   rE   ri   )versions    r   
setup_oncezRayIntegration.setup_once   s1    !%((~w777r   r   N)__name__
__module____qualname__rq   rA   staticmethodrx   r   r   r   rE   rE      sD        J':''F   \  r   rE   ry   )r   r   r   N)rM   r   r7   r   r   N)!r%   ra   rL   r   sentry_sdk.constsr   r   sentry_sdk.integrationsr   r   r   sentry_sdk.tracingr   sentry_sdk.utilsr	   r
   r   r   r   rh   ImportErrortypingr   collections.abcr   r   r   r   r   r2   ri   rN   rE   r   r   r   <module>r      s        



     , , , , , , , , U U U U U U U U U U 0 0 0 0 0 0             -JJJJ - - -
,+
,
,,- !           )(((((($$$$$$$$((((((   > > > >(_ _ _ _D/ / / /	 	 	 	 	[ 	 	 	 	 	s	   = A