o
    ZhU                     @   s  d dl m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 d dlmZ d d	lmZmZ d d
l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 erd dl m!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& 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- e'de"de!f dZ.z:d dl/m0Z1 d dl/m2Z2m3Z3 d dl4m5Z5 d dl6m7Z7 d dl8m9Z9m:Z:m;Z;m<Z< d d l=m>Z>m?Z? d d!l@mAZAmBZBmCZC W n eDy   ed"w zd d#lEmFZF W n eDy   dZFY nw e;e9e:fZGG d$d% d%eZHd&d' ZIG d(d) d)ZJd*d+ ZKd,d- ZLd.d/ ZMdJd0d1ZNd2d3 ZOd4d5 ZPd6d7 ZQd8d9 ZRd:d; ZSd<d= ZTd>d? ZUd@dA ZVdBdC ZWdDdE ZXdFdG ZYdHdI ZZdS )K    )absolute_importN)continue_trace)OP)reraisewraps)capture_checkinMonitorStatus)Hub)IntegrationDidNotEnable)ignore_logger)BAGGAGE_HEADER_NAMETRANSACTION_SOURCE_TASK)TYPE_CHECKING)capture_internal_exceptionsevent_from_exceptionloggermatch_regex_list)Any)Callable)Dict)List)Optional)Tuple)TypeVar)Union)Span)EventProcessorEventHintExcInfoF.)bound)VERSION)TaskCelery)task_has_custom)	Scheduler)IgnoreRejectRetrySoftTimeLimitExceeded)crontabschedule)task_failuretask_success
task_retryzCelery not installed)RedBeatSchedulerc                   @   s,   e Zd ZdZ			d	ddZedd ZdS )
CeleryIntegrationceleryTFNc                 C   s0   || _ || _|| _|rt  t  t  d S d S N)propagate_tracesmonitor_beat_tasksexclude_beat_tasks_patch_beat_apply_entry_patch_redbeat_maybe_due_setup_celery_beat_signals)selfr6   r7   r8    r=   U/var/www/html/lang_env/lib/python3.10/site-packages/sentry_sdk/integrations/celery.py__init__G   s   
zCeleryIntegration.__init__c                     st   t dk rtddd lm  m}  | j  fdd}|| _ddlm} t|j	|_	t
  td td td	 d S )
N)   zCelery 3 or newer required.r   c                    s`   t |dds!t|drt|t|jt|_nt||j|_d|_t| | |g|R i |S )N_sentry_is_patchedF__call__T)getattrr'   _wrap_task_calltyperB   runrA   _wrap_tracer)nametaskargskwargsZold_build_tracerr=   r>   sentry_build_tracera   s   
z9CeleryIntegration.setup_once.<locals>.sentry_build_tracer)r%   zcelery.worker.jobzcelery.app.tracezcelery.redirected)CELERY_VERSIONr   celery.app.traceapptraceZbuild_tracerZcelery.app.taskr%   _wrap_apply_asyncapply_async_patch_worker_exitr   )rQ   rM   r%   r=   rL   r>   
setup_onceW   s   zCeleryIntegration.setup_once)TFN)__name__
__module____qualname__
identifierr?   staticmethodrU   r=   r=   r=   r>   r3   D   s    
r3   c                   C   s   t   S r5   )timer=   r=   r=   r>   _now_seconds_since_epoch   s   r\   c                   @   s   e Zd Zdd Zdd ZdS )NoOpMgrc                 C      d S r5   r=   )r<   r=   r=   r>   	__enter__      zNoOpMgr.__enter__c                 C   r^   r5   r=   )r<   exc_type	exc_value	tracebackr=   r=   r>   __exit__   r`   zNoOpMgr.__exit__N)rV   rW   rX   r_   rd   r=   r=   r=   r>   r]      s    r]   c                    s   t   fdd}|S )Nc               	      s  t j}|t}|d u r | i |S |dpi }|d|j}|s* | i |S z
| d d dk}W n ttfyA   d}Y nw | d }|sQ|j	t
j|jdnt }|}	t p |	d urft||	ni }
|jru|
dd	t  i |
r|t}|
t}|p|}|r|rd
||}||
 |r||t< |di |
 |r||d t< | D ]\}}|dr||d |< q||d< W d    n1 sw   Y   | i |W  d    S 1 sw   Y  d S )Nheaderszsentry-propagate-traces   r   ZBEATF)opdescription sentry-monitor-start-timestamp-sz%.9fz{},{}zsentry-)r
   currentget_integrationr3   getpopr6   
IndexError	TypeErrorZ
start_spanr   ZQUEUE_SUBMIT_CELERYrH   r]   r   dictZiter_trace_propagation_headersr7   updater\   r   format
setdefaultitems
startswith)rJ   rK   hubintegrationZkwarg_headersr6   Ztask_started_from_beatrI   Zspan_mgrspanre   Zexisting_baggageZsentry_baggageZcombined_baggagekeyvaluefr=   r>   rS      sr   




.$z&_wrap_apply_async.<locals>.apply_asyncr   )r|   rS   r=   r{   r>   rR      s   PrR   c                       t   fdd}|S )Nc               	      s^  t j}|td u r | i |S | }d|_|  |tg| R i | d }t	 ! t
| d dp:i tjdtd}j|_|d W d    n1 sTw   Y  |d u rk | i |W  d    S |j|djt| d | d	 d
id  | i |W  d    W  d    S 1 sw   Y  W d    d S 1 sw   Y  d S )Nr4   r@   re   zunknown celery task)rg   rH   sourceokZ
celery_jobrf      )rI   rJ   rK   )Zcustom_sampling_context)r
   rj   rk   r3   Z
push_scope_nameZclear_breadcrumbsZadd_event_processor_make_event_processorr   r   rl   r   ZQUEUE_TASK_CELERYr   rH   
set_statusZstart_transactionlist)rJ   rK   rv   scopeZtransactionr|   rI   r=   r>   _inner   sF   


"z_wrap_tracer.<locals>._innerr   rI   r|   r   r=   r   r>   rG      s   	*rG   c                    r}   )Nc                     sf   z | i |W S  t y2   t }t  t| W d    n1 s&w   Y  t|  Y d S w r5   )	Exceptionsysexc_infor   _capture_exceptionr   )rJ   rK   r   r   r=   r>   r   /  s   z_wrap_task_call.<locals>._innerr   r   r=   r   r>   rD   &  s   	
rD   c                    s    fdd}|S )Nc                    s   t  ! | di }|d< | di }j d|d< W d    n1 s'w   Y  d|v r`t  % t|d d trPdd	td
g| d< W d    | S W d    | S 1 s[w   Y  | S )NtagsZcelery_task_idextra)Z	task_namerJ   rK   z
celery-jobr   r   r4   r,   rH   fingerprint)r   rs   rH   
issubclassr,   rC   )eventhintr   r   rJ   rK   rI   uuidr=   r>   event_processor?  s.   




z._make_event_processor.<locals>.event_processorr=   )rI   r   rJ   rK   requestr   r=   r   r>   r   =  s   r   c                 C   s   t j}|td u rd S t|d trt|d d S t|d t| dr.t|d | jr.d S |j	}t
||jdddd\}}|j||d	 d S )
Nrf   ZabortedZinternal_errorthrowsr4   F)rE   Zhandled)Zclient_optionsZ	mechanism)r   )r
   rj   rk   r3   
isinstanceCELERY_CONTROL_FLOW_EXCEPTIONS_set_statushasattrr   clientr   optionsZcapture_event)rI   r   rv   r   r   r   r=   r=   r>   r   Z  s    


r   c              	   C   sz   t  0 |  }|jd ur|j| W d    n1 sw   Y  W d    d S W d    d S 1 s6w   Y  d S r5   )r   configure_scoperx   r   )rv   statusr   r=   r=   r>   r   v  s   

"r   c                     s(   ddl m}  | j  fdd}|| _d S )Nr   )Workerc                     s   z0 | i |W t   tj}|td ur!|  W d    S W d    S 1 s+w   Y  S t   tj}|td urJ|  W d    w W d    w 1 sTw   Y  w r5   )r   r
   rj   rk   r3   flush)rJ   rK   rv   Zold_workloopr=   r>   sentry_workloop  s   
&
z+_patch_worker_exit.<locals>.sentry_workloop)Zbilliard.poolr   Zworkloop)r   r   r=   r   r>   rT   ~  s   

rT   c                 C   sF   | j dpi }d|v r||d  |d= || j dpi  |S )Nre   
properties)r   rl   rq   )rI   re   r=   r=   r>   _get_headers  s   r   c                 C   sF   d}t | } |D ]\}}| |krt| | }||f  S qt| dfS )N))dayg     @)hourg      @)minuteg      N@second)floatint)secondsZ
TIME_UNITSunitdividerintervalr=   r=   r>   _get_humanized_interval  s   r   c                 C   s   i }d }d }d }t | trd}d| }n&t | tr1d}t| j\}}|dkr0td|| i S n
tdt|  i S i |d< ||d d< ||d d	< |d urU||d d
< t	| doc| j
d uoct| j
ph|jphd|d< |S )Nr-   zd{0._orig_minute} {0._orig_hour} {0._orig_day_of_month} {0._orig_month_of_year} {0._orig_day_of_week}r   r   zIntervals shorter than one minute are not supported by Sentry Crons. Monitor '%s' has an interval of %s seconds. Use the `exclude_beat_tasks` option in the celery integration to exclude it.z8Celery schedule type '%s' not supported by Sentry Crons.r.   rE   rz   r   tzUTCtimezone)r   r-   rr   r.   r   r   r   warningrE   r   r   strr   )celery_schedulerP   monitor_namemonitor_configZschedule_typeZschedule_valueZschedule_unitr=   r=   r>   _get_monitor_config  sT   

	

r   c                     s   t j  fdd} | t _d S )Nc                        | \}}|j }|j}|j}tj}|t}|d u r  | i |S t||jr- | i |S |	 B}	|	
  t|||}
t|
}|re|jdi }|||
d t||
tjd}|d|i ||jd<  | i |W  d    S 1 svw   Y  d S Nre   )sentry-monitor-slugsentry-monitor-config)monitor_slugr   r   sentry-monitor-check-in-idrP   r.   rH   r
   rj   rk   r3   r   r8   r   Zset_new_propagation_contextr   boolr   rm   rq   r   r	   ZIN_PROGRESSrJ   rK   Z	schedulerZschedule_entryrP   r   r   rv   rw   r   r   Zis_supported_schedulere   check_in_idZoriginal_apply_entryr=   r>   sentry_apply_entry  <   


$z3_patch_beat_apply_entry.<locals>.sentry_apply_entry)r(   Zapply_entry)r   r=   r   r>   r9     s   
-r9   c                     s(   t d u rd S t j  fdd} | t _d S )Nc                     r   r   r   r   Zoriginal_maybe_duer=   r>   sentry_maybe_due(  r   z2_patch_redbeat_maybe_due.<locals>.sentry_maybe_due)r2   Z	maybe_due)r   r=   r   r>   r:      s
   
-r:   c                   C   s"   t t tt tt d S r5   )r0   connectcrons_task_successr/   crons_task_failurer1   crons_task_retryr=   r=   r=   r>   r;   X  s   

r;   c                 K   ^   t d|  t| }d|vrd S |di }t|d }t|d ||d t | tjd d S )Nzcelery_task_success %sr   r   ri   r   r   r   r   durationr   )	r   debugr   rl   r   r   r\   r	   OKZsenderrK   re   r   Zstart_timestamp_sr=   r=   r>   r   _     
r   c                 K   r   )Nzcelery_task_failure %sr   r   ri   r   r   	r   r   r   rl   r   r   r\   r	   ERRORr   r=   r=   r>   r   t  r   r   c                 K   r   )Nzcelery_task_retry %sr   r   ri   r   r   r   r   r=   r=   r>   r     r   r   r5   )[
__future__r   r   r[   Zsentry_sdk.apir   Zsentry_sdk.constsr   Zsentry_sdk._compatr   Zsentry_sdk._functoolsr   Zsentry_sdk.cronsr   r	   Zsentry_sdk.hubr
   Zsentry_sdk.integrationsr   r   Zsentry_sdk.integrations.loggingr   Zsentry_sdk.tracingr   r   Zsentry_sdk._typesr   Zsentry_sdk.utilsr   r   r   r   typingr   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r4   r$   rN   r%   r&   rO   r'   Zcelery.beatr(   Zcelery.exceptionsr)   r*   r+   r,   Zcelery.schedulesr-   r.   Zcelery.signalsr/   r0   r1   ImportErrorZredbeat.schedulersr2   r   r3   r\   r]   rR   rG   rD   r   r   r   rT   r   r   r   r9   r:   r;   r   r   r   r=   r=   r=   r>   <module>   s|    
A

V7
948