
    Dj                         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 d dl	m
Z
 d dlmZmZmZmZmZ 	 d dlZd dlZ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 Zd Zd Z G d de      Zy# e$ r	  ed      w xY w)    N)OP
SPANSTATUS)DidNotEnableIntegration)TRANSACTION_SOURCE_TASK)event_from_exceptionloggerpackage_versionqualname_from_functionreraisezRay not installed.)TYPE_CHECKING)Callable)AnyOptional)ExcInfoc                  t    t        j                         j                         ry t        j                  d       y )Nz[[Tracing] Sentry not initialized in ray cluster worker, performance data will be discarded.)
sentry_sdk
get_client	is_activer	   debug     _C:\Crop_Prediction\Backend\crop-ai-system\venv\Lib\site-packages\sentry_sdk/integrations/ray.py_check_sentry_initializedr      s)    ((*
LLer   c                  v    t         j                  t        j                        fd       } | t         _        y )Nc                      t        j                         r  g|| S d d fd
} |g|| }|j                   fd}||_        |S )N)_tracingc                    t                t        j                  | xs i t        j                  t              t        j                  t              }t        j                  |      5 }	  |i |}|j                  t        j                         cddd       S # t        $ rI |j                  t        j                         t        j                          }t#        |       t%        |  Y \w xY w# 1 sw Y   yxY w)z$
            Ray Worker
            )opnameoriginsourceN)r   r   continue_tracer   QUEUE_TASK_RAYr   RayIntegrationr!   r   start_transaction
set_statusr   OK	ExceptionINTERNAL_ERRORsysexc_info_capture_exceptionr   )r   f_argsf_kwargstransactionresultr,   fs         r   _fz1_patch_ray_remote.<locals>.new_remote.<locals>._f6   s    
 &'$33B$$+A.%,,.K --k: 
k'3(3F**:==9 
 
 ! '**:+D+DE"||~H&x0X&	'	
 
s1   (C1*'BC1AC.+C1-C..C11C:c                  &   t        j                  t        j                  t	              t
        j                        5 }t        j                         j                         D ci c]  \  }}||
 }}}	  	| i |d|i}|j                  t        j                         cddd       S c c}}w # t        $ rI |j                  t        j                         t        j                         }t!        |       t#        |  Y bw xY w# 1 sw Y   yxY w)z$
            Ray Client
            )r   r    r!   r   N)r   
start_spanr   QUEUE_SUBMIT_RAYr   r%   r!   get_current_scopeiter_trace_propagation_headersr'   r   r(   r)   r*   r+   r,   r-   r   )
argskwargsspankvtracingr1   r,   r2   old_remote_methods
           r   &_remote_method_with_header_propagationzU_patch_ray_remote.<locals>.new_remote.<locals>._remote_method_with_header_propagationT   s    
 &&&&+A.%,,   !+ < < > ] ] _1 qD '.QQQFOOJMM2 % 
 ! 'OOJ$=$=>"||~H&x0X&	' sB   &D%B,2D6+B2!D,D2ADDDDD)inspectisclassremote)r2   r9   r:   r3   rvr@   r?   
old_remotes   `     @r   
new_remotez%_patch_ray_remote.<locals>.new_remote,   sc     ??1
 a0$000!% 	6 +T+F+II	2 ;		r   )rayrC   	functoolswraps)rF   rE   s    @r   _patch_ray_remoterJ   (   s3    J__Z B !BH CJr   c                     t        j                         }t        | |j                  dt        j
                  d      \  }}t        j                  ||       y )NF)handledtype)client_options	mechanism)hint)r   r   r   optionsr%   
identifiercapture_event)r,   r:   clienteventrP   s        r   r-   r-   t   sK    ""$F&~~"--
KE4 U.r   c                   *    e Zd ZdZde Zed        Zy)r%   rG   zauto.queue.c                      t        d      } | t        dj                  |             | dk  rt        d      t                y )NrG   zUnparsable ray version: {})      r   zRay 2.7.0 or newer required)r
   r   formatrJ   )versions    r   
setup_oncezRayIntegration.setup_once   sD     "%(?;BB7KLLY<==r   N)__name__
__module____qualname__rR   r!   staticmethodr\   r   r   r   r%   r%      s%    J:,'F
 
r   r%   )rA   r+   r   sentry_sdk.constsr   r   sentry_sdk.integrationsr   r   sentry_sdk.tracingr   sentry_sdk.utilsr   r	   r
   r   r   rG   ImportErrorrH   typingr   collections.abcr   r   r   r   r   rJ   r-   r%   r   r   r   <module>rh      sx     
  , = 6 -   ($(IX/[ c  -
+
,,-s   A+ +A9