o
    pf<                     @   s   d dl Z d dlmZmZmZmZmZmZ ddlm	Z	m
Z
mZ dZe dZe dZe dZG d	d
 d
ZG dd deZeZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZdS )    N)DictListOptionalSequenceTupleUnion   )ExemplarSample	Timestamp)countergaugesummary	histogramgaugehistogramunknowninfostatesetz^[a-zA-Z_:][a-zA-Z0-9_:]*$z^[a-zA-Z_][a-zA-Z0-9_]*$z^__.*$c                   @   s   e Zd ZdZddedededefddZdded
eeef dedee	e
ef  dee dd	fddZdedefddZdefddZdd Zd	S )MetriczA single metric family and its samples.

    This is intended only for internal use by the instrumentation client.

    Custom collectors should use GaugeMetricFamily, CounterMetricFamily
    and SummaryMetricFamily instead.
     namedocumentationtypunitc                 C   sv   |r| d| s|d| 7 }t|std| || _|| _|| _|dkr)d}|tvr3td| || _g | _	d S )N_zInvalid metric name: untypedr   zInvalid metric type: )
endswithMETRIC_NAME_REmatch
ValueErrorr   r   r   METRIC_TYPEStypesamples)selfr   r   r   r    r$   W/home/ertert/spirit/venv/lib/python3.10/site-packages/prometheus_client/metrics_core.py__init__   s   

zMetric.__init__Nlabelsvalue	timestampexemplarreturnc                 C   s   | j t||||| dS )z?Add a sample to the metric.

        Internal-only, do not use.N)r"   appendr
   )r#   r   r'   r(   r)   r*   r$   r$   r%   
add_sample'   s   zMetric.add_sampleotherc                 C   sF   t |to"| j|jko"| j|jko"| j|jko"| j|jko"| j|jkS N)
isinstancer   r   r   r!   r   r"   )r#   r.   r$   r$   r%   __eq__-   s   





zMetric.__eq__c                 C   s   d | j| j| j| j| jS )NzMetric({}, {}, {}, {}, {}))formatr   r   r!   r   r"   )r#   r$   r$   r%   __repr__5   s   zMetric.__repr__c                    s8    fdd| j D }|rt| j| j| j}||_ |S dS )zMBuild a snapshot of a metric with samples restricted to a given set of names.c                    s   g | ]
}|d   v r|qS )r   r$   ).0snamesr$   r%   
<listcomp>@   s    z-Metric._restricted_metric.<locals>.<listcomp>N)r"   r   r   r   r!   )r#   r7   r"   mr$   r6   r%   _restricted_metric>   s   zMetric._restricted_metric)r   NN)__name__
__module____qualname____doc__strr&   r   floatr   r   r   r	   r-   objectboolr1   r3   r:   r$   r$   r$   r%   r      s    <	r   c                   @   n   e Zd ZdZ			ddededee deee  def
d	d
Zddee dedee	e
ef  ddfddZdS )UnknownMetricFamilyzOA single unknown metric and its samples.
    For use by custom collectors.
    Nr   r   r   r(   r'   r   c                 C   \   t | ||d| |d ur|d urtd|d u rg }t|| _|d ur,| g | d S d S )Nr   1Can only specify at most one of value and labels.r   r&   r   tuple_labelnames
add_metricr#   r   r   r(   r'   r   r$   r$   r%   r&   M      
zUnknownMetricFamily.__init__r)   r+   c                 C   (   | j t| jtt| j||| dS )zAdd a metric to the metric family.
        Args:
        labels: A list of label values
        value: The value of the metric.
        Nr"   r,   r
   r   dictziprJ   r#   r'   r(   r)   r$   r$   r%   rK   ]   s   (zUnknownMetricFamily.add_metricNNr   r/   r<   r=   r>   r?   r@   r   rA   r   r&   r   r   rK   r$   r$   r$   r%   rE   H   s"    

0rE   c                   @   s   e Zd ZdZ				ddededee deee  dee d	efd
dZ		ddee dedee dee	e
ef  ddf
ddZdS )CounterMetricFamilyzIA single counter and its samples.

    For use by custom collectors.
    Nr   r   r   r(   r'   createdr   c                 C   st   | dr|d d }t| ||d| |d ur |d ur td|d u r&g }t|| _|d ur8| g || d S d S )N_totalir   rG   )r   r   r&   r   rI   rJ   rK   )r#   r   r   r(   r'   rV   r   r$   r$   r%   r&   p   s   
	
zCounterMetricFamily.__init__r)   r+   c                 C   s`   | j t| jd tt| j||| |dur.| j t| jd tt| j||| dS dS )zAdd a metric to the metric family.

        Args:
          labels: A list of label values
          value: The value of the metric
          created: Optional unix timestamp the child was created at.
        rW   N_createdrO   )r#   r'   r(   rV   r)   r$   r$   r%   rK      s   (,zCounterMetricFamily.add_metricNNNr   r;   rT   r$   r$   r$   r%   rU   j   s@    

rU   c                   @   rD   )GaugeMetricFamilyzGA single gauge and its samples.

    For use by custom collectors.
    Nr   r   r   r(   r'   r   c                 C   rF   )Nr   rG   rH   rL   r$   r$   r%   r&      rM   zGaugeMetricFamily.__init__r)   r+   c                 C   rN   )z|Add a metric to the metric family.

        Args:
          labels: A list of label values
          value: A float
        NrO   rR   r$   r$   r%   rK      s   (zGaugeMetricFamily.add_metricrS   r/   rT   r$   r$   r$   r%   rZ      s"    

0rZ   c                   @   s~   e Zd ZdZ				ddededee dee deee  d	efd
dZ		ddee dededee
eef  ddf
ddZdS )SummaryMetricFamilyzIA single summary and its samples.

    For use by custom collectors.
    Nr   r   r   count_value	sum_valuer'   r   c                 C   s   t | ||d| |d u |d u krtd|d ur!|d ur!td|d u r'g }t|| _|d ur=|d ur?| g || d S d S d S )Nr   z4count_value and sum_value must be provided together.rG   rH   )r#   r   r   r\   r]   r'   r   r$   r$   r%   r&      s   
zSummaryMetricFamily.__init__r)   r+   c                 C   sT   | j t| jd tt| j||| | j t| jd tt| j||| dS )zAdd a metric to the metric family.

        Args:
          labels: A list of label values
          count_value: The count value of the metric.
          sum_value: The sum value of the metric.
        _count_sumNrO   )r#   r'   r\   r]   r)   r$   r$   r%   rK      s   (,zSummaryMetricFamily.add_metricrY   r/   )r<   r=   r>   r?   r@   r   intrA   r   r&   r   r   rK   r$   r$   r$   r%   r[      s>    

r[   c                   @   s   e Zd ZdZ				ddededeeeeee	f eee	e
f f   dee	 deee  d	efd
dZ	ddee deeeee	f eee	e
f f  dee	 deeee	f  ddf
ddZdS )HistogramMetricFamilyzKA single histogram and its samples.

    For use by custom collectors.
    Nr   r   r   bucketsr]   r'   r   c                 C   sv   t | ||d| |d ur|d u rtd|d ur!|d ur!td|d u r'g }t|| _|d ur9| g || d S d S )Nr   z-sum value cannot be provided without buckets.3Can only specify at most one of buckets and labels.rH   )r#   r   r   rb   r]   r'   r   r$   r$   r%   r&      s   
zHistogramMetricFamily.__init__r)   r+   c           	   
   C   s   |D ]2}|dd \}}d}t |dkr|d }| jt| jd ttt| j|d|fg ||| qt	|d d dkrq|durs| jt| jd tt| j||d d	 | | jt| jd
 tt| j||| dS dS dS )az  Add a metric to the metric family.

        Args:
          labels: A list of label values
          buckets: A list of lists.
              Each inner list can be a pair of bucket name and value,
              or a triple of bucket name, value, and exemplar.
              The buckets must be sorted, and +Inf present.
          sum_value: The sum value of the metric.
        N      _bucketler   r^   r   r_   )
lenr"   r,   r
   r   rP   listrQ   rJ   rA   )	r#   r'   rb   r]   r)   bbucketr(   r*   r$   r$   r%   rK      s(   &z HistogramMetricFamily.add_metricrY   r/   )r<   r=   r>   r?   r@   r   r   r   r   rA   r	   r&   r   rK   r$   r$   r$   r%   ra      s>    $

 ra   c                   @   s   e Zd ZdZ				ddededeeeeef   dee deee  d	efd
dZ		ddee deeeef  dee dee
eef  ddf
ddZdS )GaugeHistogramMetricFamilyzQA single gauge histogram and its samples.

    For use by custom collectors.
    Nr   r   r   rb   
gsum_valuer'   r   c                 C   s^   t | ||d| |d ur|d urtd|d u rg }t|| _|d ur-| g || d S d S )Nr   rc   rH   )r#   r   r   rb   rn   r'   r   r$   r$   r%   r&   &  s   
z#GaugeHistogramMetricFamily.__init__r)   r+   c              
   C   s   |D ]\}}| j t| jd ttt| j|d|fg || q| j t| jd tt| j||d d |t| jd tt| j|||g dS )a  Add a metric to the metric family.

        Args:
          labels: A list of label values
          buckets: A list of pairs of bucket names and values.
              The buckets must be sorted, and +Inf present.
          gsum_value: The sum value of the metric.
        rf   rg   _gcountrh   r   _gsumN)	r"   r,   r
   r   rP   rj   rQ   rJ   extend)r#   r'   rb   rn   r)   rl   r(   r$   r$   r%   rK   7  s   &
z%GaugeHistogramMetricFamily.add_metricrY   r/   )r<   r=   r>   r?   r@   r   r   r   rA   r&   r   r   rK   r$   r$   r$   r%   rm      s>    

rm   c                   @   sz   e Zd ZdZ		ddededeeeef  deee  fddZ	ddee deeef d	ee	e
ef  d
dfddZdS )InfoMetricFamilyzFA single info and its samples.

    For use by custom collectors.
    Nr   r   r(   r'   c                 C   Z   t | ||d |d ur|d urtd|d u rg }t|| _|d ur+| g | d S d S )Nr   rG   rH   r#   r   r   r(   r'   r$   r$   r%   r&   X     
zInfoMetricFamily.__init__r)   r+   c              	   C   s8   | j t| jd ttt| j|fi |d| dS )zAdd a metric to the metric family.

        Args:
          labels: A list of label values
          value: A dict of labels
        _infor   NrO   rR   r$   r$   r%   rK   g  s   
zInfoMetricFamily.add_metricr;   r/   )r<   r=   r>   r?   r@   r   r   r   r&   r   r   rA   rK   r$   r$   r$   r%   rr   R  .    


rr   c                   @   sz   e Zd ZdZ		ddededeeeef  deee  fddZ		ddee deeef d	ee
eef  d
dfddZdS )StateSetMetricFamilyzJA single stateset and its samples.

    For use by custom collectors.
    Nr   r   r(   r'   c                 C   rs   )Nr   rG   rH   rt   r$   r$   r%   r&     ru   zStateSetMetricFamily.__init__r)   r+   c              
   C   s`   t |}t| D ]#\}}|rdnd}| jt| jtt| j	| jf ||f || q
dS )zAdd a metric to the metric family.

        Args:
          labels: A list of label values
          value: A dict of string state names to booleans
        r   r   N)
rI   sorteditemsr"   r,   r
   r   rP   rQ   rJ   )r#   r'   r(   r)   stateenabledvr$   r$   r%   rK     s   zStateSetMetricFamily.add_metricr;   r/   )r<   r=   r>   r?   r@   r   r   rC   r   r&   r   r   rA   rK   r$   r$   r$   r%   rx   z  rw   rx   )retypingr   r   r   r   r   r   r"   r	   r
   r   r    compiler   METRIC_LABEL_NAME_RERESERVED_METRIC_LABEL_NAME_REr   rE   UntypedMetricFamilyrU   rZ   r[   ra   rm   rr   rx   r$   r$   r$   r%   <module>   s"     


9, ,>2(