o
    );c4                     @   s   d dl mZmZ d dlZd dlmZ d dlmZmZm	Z	m
Z
 ddlmZ G dd deZG d	d
 d
eZG dd deZG dd dZeddZdS )    )ABCabstractmethodN)Lock)DictIterableListOptional   )Metricc                   @   s"   e Zd Zedee fddZdS )	Collectorreturnc                 C   s   d S N selfr   r   e/Users/cmarchbanks/go/src/github.com/prometheus/client_python/build/lib/prometheus_client/registry.pycollect   s   zCollector.collectN)__name__
__module____qualname__r   r   r
   r   r   r   r   r   r   
   s    r   c                   @   s   e Zd Zdee fddZdS )_EmptyCollectorr   c                 C   s   g S r   r   r   r   r   r   r      s   z_EmptyCollector.collectN)r   r   r   r   r
   r   r   r   r   r   r      s    r   c                   @   s   e Zd ZdZd dedeeeef  fddZde	d	dfd
dZ
de	d	dfddZdd Zd	ee fddZdee d	dfddZdeeeef  d	dfddZd	eeeef  fddZdd Zd!dedeeeef  d	ee fddZdS )"CollectorRegistryzMetric collector registry.

    Collectors must have a no-argument method 'collect' that returns a list of
    Metric objects. The returned metrics should be consistent with the Prometheus
    exposition formats.
    FNauto_describetarget_infoc                 C   s.   i | _ i | _|| _t | _i | _| | d S r   )_collector_to_names_names_to_collectors_auto_describer   _lock_target_infoset_target_info)r   r   r   r   r   r   __init__   s   zCollectorRegistry.__init__	collectorr   c                 C   sv   | j . | |}t| j|}|rtd||D ]}|| j|< q|| j|< W d   dS 1 s4w   Y  dS )z Add a collector to the registry.z.Duplicated timeseries in CollectorRegistry: {}N)r   
_get_namessetr   intersection
ValueErrorformatr   )r   r!   names
duplicatesnamer   r   r   register%   s   
"zCollectorRegistry.registerc                 C   sL   | j  | j| D ]}| j|= q	| j|= W d   dS 1 sw   Y  dS )z%Remove a collector from the registry.N)r   r   r   )r   r!   r)   r   r   r   
unregister2   s
   

"zCollectorRegistry.unregisterc                 C   s   d}z|j }W n	 ty   Y nw |s| jr|j}|sg S g }ddgg dg dg ddgd}| D ]}||j ||jg D ]
}||j|  qBq3|S )	z@Get names of timeseries the collector produces and clashes with.N_total_created)_sum_countr-   )_bucketr.   r/   r-   )r0   _gsum_gcount_info)counterZsummary	histogramgaugehistograminfo)ZdescribeAttributeErrorr   r   appendr)   gettype)r   r!   Z	desc_funcresultZtype_suffixesmetricsuffixr   r   r   r"   9   s.   


zCollectorRegistry._get_namesc                 c   sp    d}d}| j  t| j}| jr|  }W d   n1 s w   Y  |r*|V  |D ]	}| E dH  q,dS )z3Yields metrics from the collectors in the registry.N)r   copyr   r   _target_info_metricr   )r   
collectorsZtir!   r   r   r   r   V   s   zCollectorRegistry.collectr'   RestrictedRegistryc                 C   s   t |}t|| S )a  Returns object that only collects some metrics.

        Returns an object which upon collect() will return
        only samples with the given names.

        Intended usage is:
            generate_latest(REGISTRY.restricted_registry(['a_timeseries']))

        Experimental.)r#   rB   )r   r'   r   r   r   restricted_registryc   s   

z%CollectorRegistry.restricted_registrylabelsc                 C   sp   | j + |r| jsd| jv rtdt | jd< n
| jr#| jdd  || _W d    d S 1 s1w   Y  d S )Nr   z7CollectorRegistry already contains a target_info metric)r   r   r   r%   r   pop)r   rD   r   r   r   r   p   s   "z!CollectorRegistry.set_target_infoc                 C   s0   | j  | jW  d    S 1 sw   Y  d S r   )r   r   r   r   r   r   get_target_infoz   s   $z!CollectorRegistry.get_target_infoc                 C   s    t ddd}|d| jd |S )NtargetzTarget metadatar7   r   r	   )r
   Z
add_sampler   )r   mr   r   r   r@   ~   s   z%CollectorRegistry._target_info_metricr)   c                 C   sL   |du ri }|   D ]}|jD ]}|j|kr"|j|kr"|j    S qq
dS )z~Returns the sample value, or None if not found.

        This is inefficient, and intended only for use in unittests.
        N)r   samplesr)   rD   value)r   r)   rD   r=   sr   r   r   get_sample_value   s   
z"CollectorRegistry.get_sample_value)FNr   )r   r   r   __doc__boolr   r   strr    r   r*   r+   r"   r   r
   r   rC   r   rF   r@   floatrL   r   r   r   r   r      s     
,r   c                   @   s4   e Zd Zdee defddZdee fddZdS )	rB   r'   registryc                 C   s   t || _|| _d S r   )r#   	_name_set	_registry)r   r'   rQ   r   r   r   r       s   

zRestrictedRegistry.__init__r   c                 c   s    t  }d }| jj/ d| jv r| jjr| j }| jD ]}|dkr1|| jjv r1|| jj|  qW d    n1 s<w   Y  |rF|V  |D ]}| D ]}|	| j}|r[|V  qNqHd S )Nr   )
r#   rS   r   rR   r   r@   r   addr   Z_restricted_metric)r   rA   Ztarget_info_metricr)   r!   r=   rH   r   r   r   r      s,   


zRestrictedRegistry.collectN)	r   r   r   r   rO   r   r    r
   r   r   r   r   r   rB      s    rB   T)r   )abcr   r   r?   	threadingr   typingr   r   r   r   metrics_corer
   r   r   r   rB   REGISTRYr   r   r   r   <module>   s    |