o
    iI!                     @   s  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	 d dl
Z
d dl
m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 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 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. eG dd dZ/G d d! d!e0eZ1G d"d# d#e0eZ2eG d$d% d%e j3Z4e	d&ed'ed(e
jd)ee d*e'j5d+e4fd,d-Z6dS ).    N)ChainMap)	dataclass)field)Enum)singledispatch)Thread)	FrameType)Any)ClassVar)Mapping)Optional)Union)cast)uuid4)DDExpressionEvaluationError)Probe)ProbeConditionMixin)ProbeEvalTiming)RateLimitMixin)TimingMixin)get_args)Session)ExcInfoType)Metrics)BudgetRateLimiterWithJitter)RateLimitExceeded)Context)Spanc                   @   s   e Zd ZU eed< eed< dS )EvaluationErrorexprmessageN)__name__
__module____qualname__str__annotations__ r&   r&   S/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/debugging/_signal/model.pyr   #   s   
 r   c                   @   s(   e Zd ZdZdZdZdZdZdZdZ	dS )	SignalStateNONE	SKIP_CONDSKIP_COND_ERROR	SKIP_RATESKIP_BUDGET
COND_ERRORDONEN)
r!   r"   r#   r)   r*   r+   r,   r-   r.   r/   r&   r&   r&   r'   r(   )   s    r(   c                   @   s   e Zd ZdZdZdZdS )SignalTrackdefaultlogssnapshotN)r!   r"   r#   DEFAULTLOGSSNAPSHOTr&   r&   r&   r'   r0   3   s    r0   c                   @   s  e Zd ZU dZejZee ed< e	j
Zee	 ed< eed< eed< eed< dZeeeef  ed< ejZeed	< eed
Zee ed< eejd
Zeed< edd ddZeed< d5ddZ de!ee"f de#fddZ$de#fddZ%de#fddZ&de#fddZ'e(dee) fddZ*e(de+ee"f fd d!Z,e-j.de!ee"f ddfd"d#Z/e-j.d$e"d%e0d&e1de!ee"f ddf
d'd(Z2e-j.de!ee"f ddfd)d*Z3d5d+d,Z4d$e"d%e0d&e1ddfd-d.Z5d6d/ee6 ddfd0d1Z7e8dedededee" d2e9j:dd fd3d4Z;dS )7Signala  Debugger signal base class.

    Used to model the data carried by the signal emitted by a probe when it is
    triggered.

    Probes with an evaluation timing will trigger the following logic:

    - If the timing is on ENTRY, the probe will trigger on entry and on exit,
      but the condition and rate limiter will only be evaluated on entry.

    - If the timing is on EXIT, the probe will trigger on exit only, and the
      condition and rate limiter will be evaluated on exit.

    - IF the timing is DEFAULT, the probe will trigger on the default timing for
      the signal.

    - If the probe has no timing, it defaults to the ENTRY timing.
    __default_timing__	__track__probeframethreadNtrace_contextstate)default_factoryerrors	timestampc                   C   s
   t t S N)r$   r   r&   r&   r&   r'   <lambda>X   s   
 zSignal.<lambda>F)r?   inituuidreturnc                 C   sB   | j }t|tr|j}|tju r| j| _d S || _d S tj| _d S rB   )	r:   
isinstancer   evaluate_atr   r4   r8   _timingENTRY)selfr:   eval_atr&   r&   r'   __post_init__Z   s
   
 zSignal.__post_init__scopec              
   C   s   | j }t|ts
dS |j}|du rdS zt||rW dS W n- tyL } z!| jt	|j
|jd |j tu r=tjntj| _W Y d}~dS d}~ww tj| _dS )z9Evaluate the probe condition against the collected frame.TN)r   r    F)r:   rG   r   	conditionboolevalr   r@   appendr   dslerrorcondition_error_limiterlimitr   r(   r+   r.   r>   r*   )rK   rN   r:   rO   er&   r&   r'   _eval_conditionb   s*   
zSignal._eval_conditionc                 C   s<   | j }t|ts
dS | jdu o|j tu }|rtj| _	|S )z Evaluate the probe rate limiter.FN)
r:   rG   r   sessionlimiterrV   r   r(   r,   r>   )rK   r:   exceededr&   r&   r'   _rate_limit_exceeded|   s   
zSignal._rate_limit_exceededc                 C   s6   | j jd}|d urt|}|d uo|jdkS dS )N
session_id   T)r:   tagsgetr   lookuplevel)rK   r]   rY   r&   r&   r'   _session_check   s
   
zSignal._session_checkc                 C   sL   | j }|d ur$| j}||j ||jt|dtdkr$tj| _	dS dS )N
__budget__infTF)
rY   r:   count_probeprobe_idget_probe_countgetattrfloatr(   r-   r>   )rK   rY   r:   r&   r&   r'   _budget_exceeded   s   zSignal._budget_exceededc                 C   s$   | j jd}|d urt|S d S )Nr]   )r:   r_   r`   r   ra   )rK   r]   r&   r&   r'   rY      s   zSignal.sessionc                 C   s   t t| jS rB   )dictr   r;   )rK   r&   r&   r'   args   s   zSignal.argsc                 C      d S rB   r&   rK   rN   r&   r&   r'   enter      zSignal.enterretvalexc_infodurationc                 C   rn   rB   r&   )rK   rr   rs   rt   rN   r&   r&   r'   exit   rq   zSignal.exitc                 C   rn   rB   r&   ro   r&   r&   r'   line   rq   zSignal.linec                 C   s`   |   sd S | jtjurd S t| j| jj}| |sd S | 	 r#d S | 
 r)d S | | d S rB   )rc   rI   r   rJ   r   rm   r;   	f_globalsrX   r\   rk   rp   ro   r&   r&   r'   do_enter   s   
zSignal.do_enterc                 C   s   |   sd S | jtjurd S | j}d|d i}|d  }d ur$||d< n||d< t||j|j}| jt	j
u rI| |s=d S |  rCd S |  rId S | |tt||pSd| tj| _d S )Nz	@durationg    .Ar^   z
@exceptionz@returnr   )rc   r>   r(   r)   r;   r   f_localsrw   rI   r   EXITrX   r\   rk   ru   r   r   r/   )rK   rr   rs   rt   r;   extraexcrN   r&   r&   r'   do_exit   s&   

zSignal.do_exitglobal_limiterc                 C   s|   |   sd S | j}t|j|j}| |sd S |d ur'| tu r'tj	| _
d S |  r-d S |  r3d S | | tj| _
d S rB   )rc   r;   r   ry   rw   rX   rV   r   r(   r,   r>   r\   rk   rv   r/   )rK   r~   r;   rN   r&   r&   r'   do_line   s   

zSignal.do_linemeterc                 C   s   t | ||||S rB   )probe_to_signalr:   r;   r<   r=   r   r&   r&   r'   
from_probe   s   zSignal.from_probe)rF   NrB   )<r!   r"   r#   __doc__r   rz   r8   r
   r%   r0   r4   r9   r   r   r   r=   r   r   r   r   r(   r)   r>   r$   r   listr@   r   timerA   rj   rE   rM   r   r	   rP   rX   r\   rc   rk   propertyr   rY   rl   rm   abcabstractmethodrp   r   intru   rv   rx   r}   RateLimiterr   staticmethodr   Meterr   r&   r&   r&   r'   r7   9   sX   
 


(
"r7   r:   r;   r<   r=   r   rF   c                 C   s   t dt|  )NzUnsupported probe type: )	TypeErrortyper   r&   r&   r'   r     s   r   )7r   collectionsr   dataclassesr   r   enumr   	functoolsr   	threadingr   r   typesr   typingr	   r
   r   r   r   r   rE   r   ddtrace.debugging._expressionsr   ddtrace.debugging._probe.modelr   r   r   r   r   ddtrace.debugging._safetyr   ddtrace.debugging._sessionr   ddtrace.internal.compatr   ddtrace.internal.metricsr   ddtrace.internal.rate_limiterr   r   r   ddtrace.tracer   r   r   r$   r(   r0   ABCr7   r   r   r&   r&   r&   r'   <module>   sh    
 M