o
    heM                     @   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 e;e9e:fZEG d#d$ d$eZFd%d& ZGG d'd( d(ZHd)d* ZId+d, ZJd-d. ZKdGd/d0ZLd1d2 ZMd3d4 ZNd5d6 ZOd7d8 ZPd9d: ZQd;d< ZRd=d> ZSd?d@ ZTdAdB ZUdCdD ZVdEdF ZWdS )H    )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 installedc                   @   s,   e Zd ZdZ			d	ddZedd ZdS )
CeleryIntegrationceleryTFNc                 C   s*   || _ || _|| _|rt  t  d S d S N)propagate_tracesmonitor_beat_tasksexclude_beat_tasks_patch_beat_apply_entry_setup_celery_beat_signals)selfr5   r6   r7    r;   a/var/www/html/aiguide_backend/venv/lib/python3.10/site-packages/sentry_sdk/integrations/celery.py__init__B   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typer@   runr?   _wrap_tracer)nametaskargskwargsold_build_tracerr;   r<   sentry_build_tracer[   s   
z9CeleryIntegration.setup_once.<locals>.sentry_build_tracer)r%   zcelery.worker.jobzcelery.app.tracezcelery.redirected)CELERY_VERSIONr   celery.app.traceapptracebuild_tracercelery.app.taskr%   _wrap_apply_asyncapply_async_patch_worker_exitr   )rP   rL   r%   r;   rJ   r<   
setup_onceQ   s   zCeleryIntegration.setup_once)TFN)__name__
__module____qualname__
identifierr=   staticmethodrV   r;   r;   r;   r<   r2   ?   s    
r2   c                   C   s   t   S r4   )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 r4   r;   )r:   r;   r;   r<   	__enter__      zNoOpMgr.__enter__c                 C   r_   r4   r;   )r:   exc_type	exc_value	tracebackr;   r;   r<   __exit__   ra   zNoOpMgr.__exit__N)rW   rX   rY   r`   re   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   BEATF)opdescription sentry-monitor-start-timestamp-sz%.9fz{},{}zsentry-)r
   currentget_integrationr2   getpopr5   
IndexError	TypeError
start_spanr   QUEUE_SUBMIT_CELERYrF   r^   r   dictiter_trace_propagation_headersr6   updater]   r   format
setdefaultitems
startswith)rH   rI   hubintegrationkwarg_headersr5   task_started_from_beatrG   span_mgrspanrf   existing_baggagesentry_baggagecombined_baggagekeyvaluefr;   r<   rT      sr   




.$z&_wrap_apply_async.<locals>.apply_asyncr   )r   rT   r;   r   r<   rS      s   PrS   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 )Nr3   r>   rf   zunknown celery task)ri   rF   sourceok
celery_jobrg      )rG   rH   rI   )custom_sampling_context)r
   rl   rm   r2   
push_scope_nameclear_breadcrumbsadd_event_processor_make_event_processorr   r   rn   r   QUEUE_TASK_CELERYr   rF   
set_statusstart_transactionlist)rH   rI   r{   scopetransactionr   rG   r;   r<   _inner   sF   


"z_wrap_tracer.<locals>._innerr   rG   r   r   r;   r   r<   rE      s   	*rE   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 r4   )	Exceptionsysexc_infor   _capture_exceptionr   )rH   rI   r   r   r;   r<   r   )  s   z_wrap_task_call.<locals>._innerr   r   r;   r   r<   rB      s   	
rB   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tagscelery_task_idextra)	task_namerH   rI   z
celery-jobr   r   r3   r,   rF   fingerprint)r   rx   rF   
issubclassr,   rA   )eventhintr   r   rH   rI   rG   uuidr;   r<   event_processor9  s.   




z._make_event_processor.<locals>.event_processorr;   )rG   r   rH   rI   requestr   r;   r   r<   r   7  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 )
Nrg   abortedinternal_errorthrowsr3   F)rC   handled)client_options	mechanism)r   )r
   rl   rm   r2   
isinstanceCELERY_CONTROL_FLOW_EXCEPTIONS_set_statushasattrr   clientr   optionscapture_event)rG   r   r{   r   r   r   r;   r;   r<   r   T  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 r4   )r   configure_scoper   r   )r{   statusr   r;   r;   r<   r   p  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 r4   )r   r
   rl   rm   r2   flush)rH   rI   r{   old_workloopr;   r<   sentry_workloop  s   
&
z+_patch_worker_exit.<locals>.sentry_workloop)billiard.poolr   workloop)r   r   r;   r   r<   rU   x  s   

rU   c                 C   sF   | j dpi }d|v r||d  |d= || j dpi  |S )Nrf   
properties)r   rn   rv   )rG   rf   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)seconds
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.   rC   r   r   tzUTCtimezone)r   r-   rw   r.   r   r   r   warningrC   r   r   strr   )celery_schedulerO   monitor_namemonitor_configschedule_typeschedule_valueschedule_unitr;   r;   r<   _get_monitor_config  sT   

	

r   c                     s   t j  fdd} | t _d S )Nc                     s   | \}}|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 )Nrf   )sentry-monitor-slugsentry-monitor-config)monitor_slugr   r   sentry-monitor-check-in-id)rO   r.   rF   r
   rl   rm   r2   r   r7   r   set_new_propagation_contextr   boolr   ro   rv   r   r	   IN_PROGRESS)rH   rI   	schedulerschedule_entryrO   r   r   r{   r|   r   r   is_supported_schedulerf   check_in_idoriginal_apply_entryr;   r<   sentry_apply_entry  s<   


$z3_patch_beat_apply_entry.<locals>.sentry_apply_entry)r(   apply_entry)r   r;   r   r<   r8     s   
-r8   c                   C   s"   t t tt tt d S r4   )r0   connectcrons_task_successr/   crons_task_failurer1   crons_task_retryr;   r;   r;   r<   r9     s   

r9   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   rk   r   r   r   r   durationr   )	r   debugr   rn   r   r   r]   r	   OKsenderrI   rf   r   start_timestamp_sr;   r;   r<   r   !     
r   c                 K   r   )Nzcelery_task_failure %sr   r   rk   r   r   	r   r   r   rn   r   r   r]   r	   ERRORr   r;   r;   r<   r   6  r   r   c                 K   r   )Nzcelery_task_retry %sr   r   rk   r   r   r   r   r;   r;   r<   r   K  r   r   r4   )X
__future__r   r   r\   sentry_sdk.apir   sentry_sdk.constsr   sentry_sdk._compatr   sentry_sdk._functoolsr   sentry_sdk.cronsr   r	   sentry_sdk.hubr
   sentry_sdk.integrationsr   r   sentry_sdk.integrations.loggingr   sentry_sdk.tracingr   r   sentry_sdk._typesr   sentry_sdk.utilsr   r   r   r   typingr   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r3   r$   rM   r%   r&   rN   r'   celery.beatr(   celery.exceptionsr)   r*   r+   r,   celery.schedulesr-   r.   celery.signalsr/   r0   r1   ImportErrorr   r2   r]   r^   rS   rE   rB   r   r   r   rU   r   r   r   r8   r9   r   r   r   r;   r;   r;   r<   <module>   sp    
@

V7
94