o
    i                     @   s   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G d
d deZdedee fddZG dd deZdS )    )defaultdict)Any)Optional)cast)Probe)ProbeLocationMixin)ProbeStatusLogger)
get_logger)RLockc                   @   s^   e Zd ZdZdeddfddZdddZdd	d
ZdededdfddZ	deddfddZ
dS )ProbeRegistryEntryprobe	installedemitting
error_typemessager   returnNc                 C   s"   || _ d| _d| _d | _d | _d S )NFr   selfr    r   U/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/debugging/_probe/registry.py__init__   s
   
zProbeRegistryEntry.__init__c                 C   
   d| _ d S NT)r   r   r   r   r   set_installed       
z ProbeRegistryEntry.set_installedc                 C   r   r   )r   r   r   r   r   set_emitting#   r   zProbeRegistryEntry.set_emittingr   r   c                 C   s   || _ || _d S N)r   r   )r   r   r   r   r   r   	set_error&   s   
zProbeRegistryEntry.set_errorc                 C   s   | j | d S r   )r   updater   r   r   r   r    *   s   zProbeRegistryEntry.updater   N)__name__
__module____qualname__	__slots__r   r   r   r   strr   r    r   r   r   r   r      s    

r   r   r   c                 C   s(   t | tr|  d S tdt| )Nr   zUnsupported probe type: {})
isinstancer   location
ValueErrorformattype)r   r   r   r   _get_probe_location.   s   
r,   c                       s(  e Zd ZdZdedededdf fddZd	eddfd
dZdeddfddZ	deddfddZ
deddfddZdedededdfddZdeddfddZdeddfddZd)ddZdeddfddZd edefd!d"Zd	edee fd#d$Zd edee fd%d&Zdedef fd'd(Z  ZS )*ProbeRegistrya  Keep track of all the registered probes.

    New probes are also registered as pending, on a location basis, until they
    are processed (e.g. installed, generally by some import hook). Pending
    probes can be retrieved with the ``get_pending`` method.
    status_loggerargskwargsr   Nc                    s.   t  j|i | || _tt| _t | _dS )zInitialize the probe registry.N)superr   loggerr   list_pendingr
   _lock)r   r.   r/   r0   	__class__r   r   r   =   s   
zProbeRegistry.__init__probesc              	   G   s   | j < |D ]0}|| v rqt|| |j< t|}|du r(| |dd|j q| j| | | j	| qW d   dS 1 sBw   Y  dS )zRegister a probe.NUnresolvedLocationz3Unable to resolve location information for probe {})
r5   r   probe_idr,   r   r*   r4   appendr2   received)r   r8   r   r(   r   r   r   registerG   s"   
"zProbeRegistry.registerr   c                 C   sn   | j * || vrtd|j 	 W d    d S | |j | | | W d    d S 1 s0w   Y  d S )Nz)Attempted to update unregistered probe %s)r5   r2   errorr:   r    log_probe_statusr   r   r   r   r    ^   s   "zProbeRegistry.updatec                 C   sP   | j  | |j   | | | j| W d   dS 1 s!w   Y  dS )z#Set the installed flag for a probe.N)r5   r:   r   _remove_pendingr2   r   r   r   r   r   r   h   s
   
"zProbeRegistry.set_installedc              	   C   s   | j @ z
tt| |j }W n ty%   td|j Y W d   dS w |js;|  | j| W d   dS W d   dS 1 sFw   Y  dS )z"Set the emitting flag for a probe.z*Probe %s no longer registered emitted dataN)	r5   r   r   r:   KeyErrorr2   debugr   r   )r   r   entryr   r   r   r   r   s   "zProbeRegistry.set_emittingr   r   c                 C   sP   | j  | |j || | j|||f W d   dS 1 s!w   Y  dS )z"Set the error message for a probe.N)r5   r:   r   r2   r>   )r   r   r   r   r   r   r   r      s   "zProbeRegistry.set_errorrC   c                 C   sx   |j r| j |j d S |jr| j|j d S |jr3|jd us$J || jj|j|j|jfd d S | j|j d S )N)r>   )r   r2   r   r   r   r   r>   r<   r   rC   r   r   r   _log_probe_status_unlocked   s   z(ProbeRegistry._log_probe_status_unlockedc                 C   s<   | j  | | |j  W d   dS 1 sw   Y  dS )z2Log the status of a probe using the status logger.N)r5   rE   r:   r   r   r   r   r?      s   "zProbeRegistry.log_probe_statusc                 C   sD   | j  |  D ]}| | qW d   dS 1 sw   Y  dS )z9Log the status of all the probes using the status logger.N)r5   valuesrE   rD   r   r   r   log_probes_status   s
   "zProbeRegistry.log_probes_statusc                 C   sT   t | }d u r
d S | j| }z|| W n	 ty   Y nw |s(| j|= d S d S r   )r,   r4   remover)   )r   r   r(   pending_probesr   r   r   r@      s   
zProbeRegistry._remove_pendingr(   c                 C   s&   |   D ]}t|j|kr dS qdS )NTF)rF   r,   r   )r   r(   rC   r   r   r   
has_probes   s
   zProbeRegistry.has_probesc              
   G   s   g }| j 4 |D ](}z| |j}W n ty"   td|j Y qw |j}| | || qW d   |S 1 s<w   Y  |S )zUnregister a collection of probes.

        This also ensures that any pending probes are removed if they haven't
        been processed yet.
        z)Tried to unregister unregistered probe %sN)	r5   popr:   rA   r2   warningr   r@   r;   )r   r8   unregistered_probesr   rC   r   r   r   
unregister   s    

zProbeRegistry.unregisterc                 C   s   | j |  S )z-Get the currently pending probes by location.)r4   copy)r   r(   r   r   r   get_pending   s   zProbeRegistry.get_pendingc                    sJ   t |ts	J || j t |jW  d   S 1 sw   Y  dS )z$Check if a probe is in the registry.N)r'   r   r5   r1   __contains__r:   r   r6   r   r   rQ      s   $zProbeRegistry.__contains__r!   )r"   r#   r$   __doc__r   r   r   r   r=   r    r   r   r&   r   r   rE   r?   rG   r@   boolrJ   r3   rN   rP   objectrQ   __classcell__r   r   r6   r   r-   5   s     



r-   N)collectionsr   typingr   r   r   ddtrace.debugging._probe.modelr   r   ddtrace.debugging._probe.statusr   ddtrace.internal.loggerr	   ddtrace.internal.threadsr
   r"   r2   rT   r   r&   r,   dictr-   r   r   r   r   <module>   s    