o
    0 i                     @   s   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 zd dlZW n eyG   e	dw d dlmZ erdd 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G d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)ExcInfoc                   C   s   t   rd S td d S )Nz[[Tracing] Sentry not initialized in ray cluster worker, performance data will be discarded.)
sentry_sdk
get_client	is_activer	   debug r   r   g/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/sentry_sdk/integrations/ray.py_check_sentry_initialized   s
   r   c                     s(   t j t d fdd	} | t _d S )Nc                    sF   t rg R i S  fdd}d ur!|S |S )Nc                    s   t d dfdd
}t|}t|j }|tjdtjj	d d |j
|d|_r5|}n	i |}|j  fdd}||_|S )	N)_sentry_tracingc              	      s   t   tj| pi tjt tjtj	d}t
|5}z |i |}|tj W n tyD   |tj t }t| t|  Y nw |W  d    S 1 sQw   Y  d S )N)opnameoriginsource)r   r   Zcontinue_tracer   ZQUEUE_TASK_RAYr   RayIntegrationr   r   ZTASKZstart_transaction
set_statusr   OK	ExceptionINTERNAL_ERRORsysexc_info_capture_exceptionr   )r   Zf_argsZf_kwargsZtransactionresultr$   )user_fr   r   new_func9   s(   $zH_patch_ray_remote.<locals>.new_remote.<locals>.wrapper.<locals>.new_funcr   )kinddefault)
parametersc               	      s   t jtjttjdD}dd t   D }z | i |d|i}|	t
j W n tyF   |	t
j t }t| t|  Y nw |W  d   S 1 sSw   Y  dS )z,
                Ray Client
                )r   r   r   c                 S   s   i | ]\}}||qS r   r   ).0kvr   r   r   
<dictcomp>o   s    zz_patch_ray_remote.<locals>.new_remote.<locals>.wrapper.<locals>._remote_method_with_header_propagation.<locals>.<dictcomp>r   N)r   Z
start_spanr   ZQUEUE_SUBMIT_RAYr   r   r   Zget_current_scopeZiter_trace_propagation_headersr   r   r    r!   r"   r#   r$   r%   r   )argskwargsspantracingr&   r$   Zold_remote_methodr'   r   r   &_remote_method_with_header_propagatione   s4   
$zf_patch_ray_remote.<locals>.new_remote.<locals>.wrapper.<locals>._remote_method_with_header_propagation)	functoolswrapsinspect	signaturelistr+   valuesappend	ParameterKEYWORD_ONLYreplace__signature__remote)r'   r(   r9   paramsrvr5   )r0   fr1   
old_remoter4   r   wrapper7   s&   

z6_patch_ray_remote.<locals>.new_remote.<locals>.wrapper)r8   isclass)rD   r0   r1   rF   rE   )r0   rD   r1   r   
new_remote,   s   
Mz%_patch_ray_remote.<locals>.new_remote)N)rayrA   r6   r7   )rI   r   rH   r   _patch_ray_remote(   s   
\rK   c                 K   s6   t  }t| |jdtjdd\}}t j||d d S )NF)Zhandledtype)Zclient_optionsZ	mechanism)hint)r   r   r   optionsr   
identifierZcapture_event)r$   r1   clienteventrM   r   r   r   r%      s   
r%   c                   @   s&   e Zd ZdZde Zedd ZdS )r   rJ   zauto.queue.c                  C   s   t d} tt|  t  d S )NrJ   )r
   r   r   rK   )versionr   r   r   
setup_once   s   

zRayIntegration.setup_onceN)__name__
__module____qualname__rO   r   staticmethodrS   r   r   r   r   r      s
    
r   ) r8   r6   r#   r   Zsentry_sdk.constsr   r   Zsentry_sdk.integrationsr   r   r   Zsentry_sdk.tracingr   Zsentry_sdk.utilsr   r	   r
   r   r   rJ   ImportErrortypingr   collections.abcr   r   r   r   r   rK   r%   r   r   r   r   r   <module>   s,    
d