B
    .l\                 @   s>   d dl Z d dlmZ ddlmZ G dd deZeddZdS )	    N)Lock   )Metricc               @   sL   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	d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.
    Fc             C   s   i | _ i | _|| _t | _d S )N)_collector_to_names_names_to_collectors_auto_describer   _lock)selfauto_describe r   9lib/python3.7/site-packages/prometheus_client/registry.py__init__   s    zCollectorRegistry.__init__c          	   C   sd   | j T | |}t| j|}|r4td|x|D ]}|| j|< q:W || j|< W dQ R X dS )z Add a collector to the registry.z/Duplicated timeseries in CollectorRegistry: {0}N)r	   
_get_namessetr   intersection
ValueErrorformatr   )r
   	collectornames
duplicatesnamer   r   r   register   s    

zCollectorRegistry.registerc          	   C   s:   | j * x| j| D ]}| j|= qW | j|= W dQ R X dS )z%Remove a collector from the registry.N)r	   r   r   )r
   r   r   r   r   r   
unregister"   s    zCollectorRegistry.unregisterc             C   s   d}y
|j }W n tk
r"   Y nX |s4| jr4|j}|s<g S g }ddgddddgddddgddd	gd
gd}x8| D ].}x(||jdgD ]}||j|  qW qtW |S )z/Get names of timeseries the collector produces.NZ_totalZ_created Z_sum_countZ_bucketZ_gsumZ_gcountZ_info)ZcounterZsummaryZ	histogramZgaugehistograminfo)ZdescribeAttributeErrorr   collectgettypeappendr   )r
   r   Z	desc_funcresultZtype_suffixesmetricsuffixr   r   r   r   )   s&    




zCollectorRegistry._get_namesc          	   c   sL   d}| j  t| j}W dQ R X x$|D ]}x| D ]
}|V  q6W q(W dS )z3Yields metrics from the collectors in the registry.N)r	   copyr   r   )r
   
collectorsr   r#   r   r   r   r   E   s    
zCollectorRegistry.collectc       	   	      s   t t  }| j. x&D ]}|| jkr|| j|  qW W dQ R X g  xX|D ]P}xJ| D ]>}fdd|jD }|r`t|j|j|j	}||_ 
| q`W qRW G  fddd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.Nc                s   g | ]}|d   kr|qS )r   r   ).0s)r   r   r   
<listcomp>a   s    z9CollectorRegistry.restricted_registry.<locals>.<listcomp>c                   s   e Zd Z fddZdS )zACollectorRegistry.restricted_registry.<locals>.RestrictedRegistryc                s    S )Nr   )r
   )metricsr   r   r   h   s    zICollectorRegistry.restricted_registry.<locals>.RestrictedRegistry.collectN)__name__
__module____qualname__r   r   )r*   r   r   RestrictedRegistryg   s   r.   )r   r	   r   addr   samplesr   r   Zdocumentationr    r!   object)	r
   r   r&   r   r   r#   r0   mr.   r   )r*   r   r   restricted_registryN   s     



z%CollectorRegistry.restricted_registryNc             C   sL   |dkri }x:|   D ].}x(|jD ]}|j|kr"|j|kr"|jS q"W qW dS )z~Returns the sample value, or None if not found.

        This is inefficient, and intended only for use in unittests.
        N)r   r0   r   labelsvalue)r
   r   r4   r#   r(   r   r   r   get_sample_valuem   s    z"CollectorRegistry.get_sample_value)F)N)r+   r,   r-   __doc__r   r   r   r   r   r3   r6   r   r   r   r   r      s   
	r   T)r   )r%   Z	threadingr   Zmetrics_corer   r1   r   ZREGISTRYr   r   r   r   <module>   s   t