o
    i#                  
   @   sL  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 eeZdZ dZ!dZ"dZ#e de$dee fddZ%dZ&dZ'dZ(dZ)eG dd  d Z*e* Z+eG d!d" d"e j,Z-G d#d$ d$e j,Z.eG d%d& d&e.Z/eG d'd( d(e.Z0eG d)d* d*e.Z1eG d+d, d,e1Z2G d-d. d.e$eZ3eG d/d0 d0e1Z4eG d1d2 d2e.Z5G d3d4 d4e$eZ6eG d5d6 d6e.Z7ed7d8G d9d: d:e-e2e7e0Z8ed7d8G d;d< d<e-e4e5e7e0Z9eG d=d> d>e j,Z:eG d?d@ d@e:Z;eG dAdB dBe:Z<eG dCdD dDZ=eG dEdF dFZ>eG dGdH dHe.Z?ed7d8G dIdJ dJe-e2e?e0e/Z@ed7d8G dKdL dLe-e4e5e?e0e/ZAeG dMdN dNZBed7d8G dOdP dPe-e4eBe0ZCG dQdR dRe$eZDeG dSdT dTZEeG dUdV dVZFeG dWdX dXZGed7d8G dYdZ dZe-e2eGZHed7d8G d[d\ d\e-e4e5eGZIeG d]d^ d^ZJed7d8G d_d` d`e-e2eJe0e/ZKed7d8G dadb dbe-e4eJe0e/ZLee@e8eHeKf ZMeeAe9eCeIeLf ZNG dcdd dde$eZOdS )e    N)	dataclass)field)fields)Enum)Path)Any)Callable)Mapping)Optional)Union)DDExpression)maybe_stringify)
get_logger)_resolve)BudgetRateLimiterWithJitter)_isinstance)cachedg     @      ?g?gN贁Nk?_pathreturnc                    sR   t |    r  S  fddt jD D ]}t| }dur&|  S qdS )zResolve the source path for the given path.

    This recursively strips parent directories until it finds a file that
    exists according to sys.path.
    c                 3   s    | ]}  |V  qd S N)relative_to).0_path R/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/debugging/_probe/model.py	<genexpr>)   s    z'_resolve_source_file.<locals>.<genexpr>N)r   is_fileresolvereversedparentsr   )r   relpathresolved_pathr   r   r   _resolve_source_file   s   r%      d         c                   @   s>   e Zd ZU eZeed< eZeed< e	Z
eed< eZeed< dS )CaptureLimits	max_levelmax_sizemax_len
max_fieldsN)__name__
__module____qualname__MAXLEVELr+   int__annotations__MAXSIZEr,   MAXLENr-   	MAXFIELDSr.   r   r   r   r   r*   6   s
   
 r*   c                   @   sb   e Zd ZU dZeed< eed< eddZe	ee
f ed< dd	d
ZdefddZdefddZdS )ProbeFprobe_idversioncomparetagsotherr   Nc                 C   sX   | j |j krtd dS | j|jkrdS dd t| D D ]}t| |t|| qdS )z-Update the mutable fields from another probe.z.Probe ID mismatch when updating mutable fieldsNc                 s   s    | ]	}|j r|jV  qd S r   )r<   name)r   fr   r   r   r   X   s    zProbe.update.<locals>.<genexpr>)r9   logerrorr:   r   setattrgetattr)selfr>   attribr   r   r   updateO   s   
zProbe.updatec                 C      dS )NFr   rE   r   r   r   is_global_rate_limited[   s   zProbe.is_global_rate_limitedc                 C   s
   t | jS r   )hashr9   rI   r   r   r   __hash__^   s   
zProbe.__hash__)r>   r8   r   N)r/   r0   r1   __context_creator__strr4   r3   r   r=   dictr   rG   boolrJ   rL   r   r   r   r   r8   G   s   
 
r8   c                   @   s   e Zd ZdddZdS )AbstractProbeMixInr   Nc                 C      d S r   r   rI   r   r   r   __post_init__c   s    z AbstractProbeMixIn.__post_init__r   N)r/   r0   r1   rS   r   r   r   r   rQ   b   s    rQ   c                       s>   e Zd ZU eed< eddddZeed< d	 fddZ  Z	S )
RateLimitMixinrateFinitreprr<   limiterr   Nc                    <   t    t j jrd j nd fddddd _d S )Nr   c                         t d S )NzRate limit exceeded for %r)rA   warningr   rI   r   r   <lambda>p       z.RateLimitMixin.__post_init__.<locals>.<lambda>TF
limit_ratetau	on_exceed	call_onceraise_on_exceed)superrS   RateLimiterrV   rZ   rI   	__class__rI   r   rS   k      

zRateLimitMixin.__post_init__rT   )
r/   r0   r1   floatr4   r   rZ   rg   rS   __classcell__r   r   rh   r   rU   f   s   
 rU   c                       sX   e Zd ZU dZee ed< eddZe	ed< eddddZ
eed< d fd
dZ  ZS )ProbeConditionMixinzrConditional probe.

    If the condition is ``None``, then this is equivalent to a non-conditional
    probe.
    	conditionFr;   condition_error_raterW   condition_error_limiterr   Nc                    r[   )Nr   c                      r\   )Nz*Condition error rate limit exceeded for %r)rA   debugr   rI   r   r   r^      r_   z3ProbeConditionMixin.__post_init__.<locals>.<lambda>TFr`   )rf   rS   rg   ro   rp   rI   rh   rI   r   rS      rj   z!ProbeConditionMixin.__post_init__rT   )r/   r0   r1   __doc__r
   r   r4   r   ro   rk   rp   rg   rS   rl   r   r   rh   r   rm   v   s   
 rm   c                   @   s2   e Zd Zdeee eeeef  f fddZdS )ProbeLocationMixinr   c                 C   rH   )zReturn a tuple of (location, sublocation) for the probe.
        For example, line probe returns the (file, line) and method probe return (module, method)
        )NNr   rI   r   r   r   location   s   zProbeLocationMixin.locationN)	r/   r0   r1   tupler
   rN   r   r3   rt   r   r   r   r   rs      s    *rs   c                       sv   e Zd ZU eddZeed< eddZeed< edddZ	e
e ed< d fd	d
Zdee
e ef fddZ  ZS )LineLocationMixinFr;   source_fileline)rX   r<   resolved_source_filer   Nc                    s   t    t| j| _d S r   )rf   rS   r%   rw   ry   rI   rh   r   r   rS      s   
zLineLocationMixin.__post_init__c                 C   s   t | j| jfS r   )r   ry   rx   rI   r   r   r   rt         zLineLocationMixin.locationrT   )r/   r0   r1   r   rw   rN   r4   rx   r3   ry   r
   r   rS   ru   rt   rl   r   r   rh   r   rv      s   
 "rv   c                   @   s   e Zd ZdZdZdZdS )ProbeEvalTimingDEFAULTENTRYEXITN)r/   r0   r1   r|   r}   r~   r   r   r   r   r{      s    r{   c                   @   sH   e Zd ZU eddZeed< eddZeed< deeef fddZ	dS )	FunctionLocationMixinFr;   module
func_qnamer   c                 C   s   | j | jfS r   )r   r   rI   r   r   r   rt         zFunctionLocationMixin.locationN)
r/   r0   r1   r   r   rN   r4   r   ru   rt   r   r   r   r   r      s   
 r   c                   @   s   e Zd ZU eed< dS )TimingMixinevaluate_atN)r/   r0   r1   r{   r4   r   r   r   r   r      s   
 r   c                   @   s   e Zd ZdZdZdZdZdS )MetricProbeKindCOUNTGAUGE	HISTOGRAMDISTRIBUTIONN)r/   r0   r1   COUNTERr   r   r   r   r   r   r   r      s
    r   c                   @   s*   e Zd ZU eed< eed< ee ed< dS )MetricProbeMixinkindr?   valueN)r/   r0   r1   rN   r4   r
   r   r   r   r   r   r      s   
 r   F)eqc                   @      e Zd ZdS )MetricLineProbeNr/   r0   r1   r   r   r   r   r          r   c                   @   r   )MetricFunctionProbeNr   r   r   r   r   r      r   r   c                   @   s,   e Zd Zejdeeef defddZdS )TemplateSegmentscoper   c                 C   rR   r   r   rE   r   r   r   r   eval   s   zTemplateSegment.evalN)	r/   r0   r1   abcabstractmethodr	   rN   r   r   r   r   r   r   r      s     r   c                   @   s0   e Zd ZU eed< deeef defddZdS )LiteralTemplateSegment	str_valuer   r   c                 C   s   | j S r   )r   r   r   r   r   r      s   zLiteralTemplateSegment.evalN)r/   r0   r1   rN   r4   r	   r   r   r   r   r   r   r         
 r   c                   @   s0   e Zd ZU eed< deeef defddZdS )ExpressionTemplateSegmentexprr   r   c                 C   s   | j |S r   )r   r   r   r   r   r   r      r   zExpressionTemplateSegment.evalN)	r/   r0   r1   r   r4   r	   rN   r   r   r   r   r   r   r      r   r   c                   @   sJ   e Zd ZU eed< ee ed< deeef de	egef defddZ
dS )	StringTemplatetemplatesegmentsr   
serializerr   c                    s2   dt dtffdd d fdd| jD S )Nr   r   c                    s   t | tr| S  | S r   )r   rN   )r   )r   r   r   _to_str   s   z&StringTemplate.render.<locals>._to_str c                    s   g | ]	} | qS r   )r   )r   s)r   r   r   r   
<listcomp>   s    z)StringTemplate.render.<locals>.<listcomp>)r   rN   joinr   )rE   r   r   r   )r   r   r   r   render   s   zStringTemplate.renderN)r/   r0   r1   rN   r4   listr   r	   r   r   r   r   r   r   r   r      s   
 ,r   c                   @   s0   e Zd ZU eed< eed< eddZeed< dS )CaptureExpressionr?   r   Fr;   limitsN)	r/   r0   r1   rN   r4   r   r   r   r*   r   r   r   r   r      s   
 r   c                   @   sZ   e Zd ZU eed< ee ed< eed< ee ed< e	ddZ
eed< edefd	d
ZdS )LogProbeMixinr   r   take_snapshotcapture_expressionsFr;   r   r   c                 C   s   | j s| jrdS dS )N
   r'   )r   r   rI   r   r   r   
__budget__  s   zLogProbeMixin.__budget__N)r/   r0   r1   rN   r4   r   r   rP   r   r   r   r*   propertyr3   r   r   r   r   r   r      s   
 r   c                   @      e Zd ZdefddZdS )LogLineProber   c                 C      | j pt| jS r   r   rP   r   rI   r   r   r   rJ   	  rz   z#LogLineProbe.is_global_rate_limitedNr/   r0   r1   rP   rJ   r   r   r   r   r         r   c                   @   r   )LogFunctionProber   c                 C   r   r   r   rI   r   r   r   rJ     rz   z'LogFunctionProbe.is_global_rate_limitedNr   r   r   r   r   r     r   r   c                   @   r   )SpanProbeMixinNr   r   r   r   r   r     r   r   c                   @   s&   e Zd ZU edddddZeed< dS )SpanFunctionProbeTF)defaultrX   rY   r<   rM   N)r/   r0   r1   r   rM   rP   r4   r   r   r   r   r     s   
 r   c                   @   s   e Zd ZdZdZdS )SpanDecorationTargetSpanROOTACTIVEN)r/   r0   r1   r   r   r   r   r   r   r     s    r   c                   @      e Zd ZU eed< eed< dS )SpanDecorationTagr?   r   N)r/   r0   r1   rN   r4   r   r   r   r   r   r   "     
 r   c                   @   s&   e Zd ZU ee ed< ee ed< dS )SpanDecorationwhenr=   N)r/   r0   r1   r
   r   r4   r   r   r   r   r   r   r   (  s   
 r   c                   @   s"   e Zd ZU eed< ee ed< dS )SpanDecorationMixintarget_spandecorationsN)r/   r0   r1   r   r4   r   r   r   r   r   r   r   .  s   
 r   c                   @   r   )SpanDecorationLineProbeNr   r   r   r   r   r   4  r   r   c                   @   r   )SpanDecorationFunctionProbeNr   r   r   r   r   r   9  r   r   c                   @   r   )SessionMixin
session_idlevelN)r/   r0   r1   rN   r4   r3   r   r   r   r   r   >  r   r   c                   @   r   )TriggerLineProbeNr   r   r   r   r   r   D  r   r   c                   @   r   )TriggerFunctionProbeNr   r   r   r   r   r   I  r   r   c                   @   s    e Zd ZdZdZdZdZdZdS )	ProbeType	LOG_PROBEMETRIC_PROBE
SPAN_PROBESPAN_DECORATION_PROBETRIGGER_PROBEN)r/   r0   r1   r   r   r   r   r   r   r   r   r   r   T  s    r   )Pr   dataclassesr   r   r   enumr   pathlibr   typingr   r   r	   r
   r   ddtrace.debugging._expressionsr   ddtrace.internal.compatr   ddtrace.internal.loggerr   ddtrace.internal.moduler   ddtrace.internal.rate_limiterr   rg   ddtrace.internal.safetyr   ddtrace.internal.utils.cacher   r/   rA   DEFAULT_PROBE_RATEDEFAULT_SNAPSHOT_PROBE_RATEDEFAULT_TRIGGER_PROBE_RATE"DEFAULT_PROBE_CONDITION_ERROR_RATErN   r%   r2   r5   r6   r7   r*   DEFAULT_CAPTURE_LIMITSABCr8   rQ   rU   rm   rs   rv   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   	LineProbeFunctionProber   r   r   r   r   <module>   s    	