a
    ö£Be4  ã                   @   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ee dœdd„ƒZdS )Ú	Collector©Úreturnc                 C   s   d S ©N© ©Úselfr   r   ú]/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/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ee dœ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eeeeef  dœdd„Ze	ddœdd	„Z
e	ddœd
d„Zdd„ Zee dœdd„Zee ddœdd„Zeeeef  ddœdd„Zeeeef  dœdd„Zdd„ Zd eeeeef  ee dœdd„ZdS )!ÚCollectorRegistryzÙMetric 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   st   | j �Z |  |¡}t| jƒ |¡}|r4td |¡ƒ‚|D ]}|| j|< q8|| j|< W d  ƒ n1 sf0    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   sJ   | j �0 | j| D ]}| j|= q| j|= W d  ƒ n1 s<0    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0 |s2| jr2|j}|s:g S g }ddgg d¢g d¢g d¢dgdœ}|ƒ D ]4}| |j¡ | |jg ¡D ]}| |j| ¡ q„qf|S )	z@Get names of timeseries the collector produces and clashes with.NZ_totalÚ_created)Ú_sumÚ_countr-   )Ú_bucketr.   r/   r-   )r0   Z_gsumZ_gcountÚ_info)ÚcounterÚsummaryZ	histogramZ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_namesr   c                 c   sn   d}d}| j �* t | j¡}| jr*|  ¡ }W d  ƒ n1 s>0    Y  |rR|V  |D ]}| ¡ E dH  qVdS )z3Yields metrics from the collectors in the registry.N)r   Úcopyr   r   Ú_target_info_metricr   )r   Ú
collectorsÚtir"   r   r   r   r   V   s    &zCollectorRegistry.collectÚRestrictedRegistry)r(   r   c                 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$   r@   )r   r(   r   r   r   Úrestricted_registryc   s    
z%CollectorRegistry.restricted_registry)Úlabelsr   c                 C   sn   | j �T |r2| js$d| jv r$tdƒ‚tƒ | jd< n| jrF| j dd ¡ || _W d   ƒ n1 s`0    Y  d S )Nr   z7CollectorRegistry already contains a target_info metric)r   r   r   r&   r   Úpop)r   rB   r   r   r   r    p   s    z!CollectorRegistry.set_target_infoc                 C   s0   | j � | jW  d   ƒ S 1 s"0    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 metadatar4   r   r	   )r
   Z
add_sampler   )r   Úmr   r   r   r=   ~   s    z%CollectorRegistry._target_info_metric)r*   rB   r   c                 C   sL   |du ri }|   ¡ D ]2}|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   Zsamplesr*   rB   Úvalue)r   r*   rB   r:   Úsr   r   r   Úget_sample_valueƒ   s    
z"CollectorRegistry.get_sample_value)FN)N)r   r   r   Ú__doc__Úboolr   r   Ústrr!   r   r+   r,   r#   r   r
   r   rA   r    rD   r=   ÚfloatrI   r   r   r   r   r      s   
r   c                   @   s2   e Zd Zee edœdd„Zee dœdd„ZdS )r@   )r(   Úregistryc                 C   s   t |ƒ| _|| _d S r   )r$   Ú	_name_setÚ	_registry)r   r(   rN   r   r   r   r!   ’   s    
zRestrictedRegistry.__init__r   c                 c   s¼   t ƒ }d }| jj�^ d| jv r0| jjr0| j ¡ }| jD ]*}|dkr6|| jjv r6| | jj| ¡ q6W d   ƒ n1 sv0    Y  |rŠ|V  |D ](}| ¡ D ]}| 	| j¡}|rš|V  qšqŽd S )Nr   )
r$   rP   r   rO   r   r=   r   Úaddr   Z_restricted_metric)r   r>   Ztarget_info_metricr*   r"   r:   rF   r   r   r   r   –   s    


2zRestrictedRegistry.collectN)	r   r   r   r   rL   r   r!   r
   r   r   r   r   r   r@   ‘   s   r@   T)r   )Úabcr   r   r<   Ú	threadingr   Útypingr   r   r   r   Zmetrics_corer
   r   r   r   r@   ZREGISTRYr   r   r   r   Ú<module>   s   |