Your IP : 216.73.216.196


Current Path : /opt/alt/python37/lib/python3.7/site-packages/prometheus_client/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib/python3.7/site-packages/prometheus_client/__pycache__/metrics.cpython-37.pyc

B

�Y�^�V�@sddlZddlmZddlZddlZddlmZddlmZm	Z	m
Z
ddlmZm
Z
mZmZddlmZddlmZmZejd	kr�eZejZnd
d�Zdd
�Zdd�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�Z Gdd�de�Z!Gdd�de�Z"Gdd�de�Z#dS)�N)�Lock�)�values)�ExceptionCounter�InprogressTracker�Timer)�Metric�METRIC_LABEL_NAME_RE�METRIC_NAME_RE�RESERVED_METRIC_LABEL_NAME_RE)�REGISTRY)�floatToGoString�INF)�cCst�|||j�S)N)�types�
MethodType�	__class__)�func�obj�r�J/opt/alt/python37/lib/python3.7/site-packages/prometheus_client/metrics.py�create_bound_methodsrcCs�d}|r||d7}|r$||d7}||7}|dkrJ|�d�rJ|dd�}|rh|�d|�sh|d|7}|r�|dkr�td|��|S)N��_�counter�_totali����)�info�statesetz2Metric name is of a type that cannot have a unit: )�endswith�
ValueError)�metric_type�name�	namespace�	subsystem�unit�	full_namerrr�_build_full_namesr&cCs\t|�}xN|D]F}t�|�s(td|��t�|�r>td|��||jkrtd|��qW|S)NzInvalid label metric name: zReserved label metric name: )�tupler	�matchrr�_reserved_labelnames)�cls�
labelnames�lrrr�_validate_labelnames's



r-c@s�eZdZdZdZdd�Zdd�Zdd�Zd	d
�Zdd�Z	d
d�Z
dd�Zdd�Zdddde
dfdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd d!�ZdS)"�MetricWrapperBaseNrcCs|jp|jo|jS)N)�_labelnames�_labelvalues)�selfrrr�_is_observable7sz MetricWrapperBase._is_observablecCs|��stdt|j���dS)Nz!%s metric is missing label values)r2r�str�_type)r1rrr�_raise_if_not_observable=sz*MetricWrapperBase._raise_if_not_observablecCs|jo|jS)N)r/r0)r1rrr�
_is_parentDszMetricWrapperBase._is_parentcCst|j|j|j|j�S)N)r�_name�_documentationr4�_unit)r1rrr�_get_metricGszMetricWrapperBase._get_metriccCs
|��gS)N)r:)r1rrr�describeJszMetricWrapperBase.describecCs:|��}x*|��D]\}}}|�|j|||�qW|gS)N)r:�_samplesZ
add_sampler7)r1�metric�suffix�labels�valuerrr�collectMszMetricWrapperBase.collectcCsd�|j|j�S)Nz{0}:{1})�formatr4r7)r1rrr�__str__SszMetricWrapperBase.__str__cCst|�}d�|j|j|j�S)Nz{0}.{1}({2}))�typerB�
__module__�__name__r7)r1r rrr�__repr__VszMetricWrapperBase.__repr__rc		Cs�t|j||||�|_t||�|_t|p(d�|_i|_||_||_	t
�|j�sZtd|j��|�
�rpt�|_i|_|��r�|��|js�|r�|�|�dS)NrzInvalid metric name: )r&r4r7r-r/r'r0�_kwargsr8r9r
r(rr6r�_lock�_metricsr2�_metric_init�register)	r1r!�
documentationr+r"r#r$�registry�labelvaluesrrr�__init__Zs 
zMetricWrapperBase.__init__c	s�|jstd|��|jr6td|tt|j|j��f��|rF�rFtd���r~t��t|j�krdtd��t�fdd�|jD��}n,t|�t|j�kr�td��tdd�|D��}|j�B||j	kr�|j
|jf|j|j|j
|d	�|j��|j	|<|j	|SQRXd
S)arReturn the child for the given labelset.

        All metrics can have labels, allowing grouping of related time series.
        Taking a counter as an example:

            from prometheus_client import Counter

            c = Counter('my_requests_total', 'HTTP Failures', ['method', 'endpoint'])
            c.labels('get', '/').inc()
            c.labels('post', '/submit').inc()

        Labels can also be provided as keyword arguments:

            from prometheus_client import Counter

            c = Counter('my_requests_total', 'HTTP Failures', ['method', 'endpoint'])
            c.labels(method='get', endpoint='/').inc()
            c.labels(method='post', endpoint='/submit').inc()

        See the best practices on [naming](http://prometheus.io/docs/practices/naming/)
        and [labels](http://prometheus.io/docs/practices/instrumentation/#use-labels).
        z,No label names were set when constructing %sz@%s already has labels set (%s); can not chain calls to .labels()z"Can't pass both *args and **kwargszIncorrect label namesc3s|]}t�|�VqdS)N)�unicode)�.0r,)�labelkwargsrr�	<genexpr>�sz+MetricWrapperBase.labels.<locals>.<genexpr>zIncorrect label countcss|]}t|�VqdS)N)rQ)rRr,rrrrT�s)rMr+r$rON)r/rr0�dict�zip�sortedr'�lenrIrJrr7r8r9rH)r1rOrSr)rSrr?{s2
zMetricWrapperBase.labelsc	Gsj|jstd|��t|�t|j�kr:tdt|j�|f��tdd�|D��}|j�|j|=WdQRXdS)Nz,No label names were set when constructing %sz+Incorrect label count (expected %d, got %s)css|]}t|�VqdS)N)rQ)rRr,rrrrT�sz+MetricWrapperBase.remove.<locals>.<genexpr>)r/rrXr'rIrJ)r1rOrrr�remove�szMetricWrapperBase.removecCs|��r|��S|��SdS)N)r6�_multi_samples�_child_samples)r1rrrr<�szMetricWrapperBase._samplesc	csz|j�|j��}WdQRXxX|��D]L\}}tt|j|��}x2|��D]&\}}}|t|t|����|fVqHWq&WdS)N)	rIrJ�copy�items�listrVr/r<rU)r1Zmetricsr?r=Z
series_labelsr>Z
sample_labelsr@rrrrZ�sz MetricWrapperBase._multi_samplescCstd|��dS)Nz*_child_samples() must be implemented by %r)�NotImplementedError)r1rrrr[�sz MetricWrapperBase._child_samplescCstd|��dS)z�
        Initialize the metric object as a child, i.e. when it has labels (if any) set.

        This is factored as a separate function to allow for deferred initialization.
        z(_metric_init() must be implemented by %rN)r_)r1rrrrK�szMetricWrapperBase._metric_init)rFrE�__qualname__r4r)r2r5r6r:r;rArCrGrrPr?rYr<rZr[rKrrrrr.3s,7r.c@s:eZdZdZdZdd�Zd
dd�Zefdd	�Zd
d�Z	dS)�CounterarA Counter tracks counts of events or running totals.

    Example use cases for Counters:
    - Number of requests processed
    - Number of items that were inserted into a queue
    - Total amount of data that a system has processed

    Counters can only go up (and be reset when the process restarts). If your use case can go down,
    you should use a Gauge instead.

    An example for a Counter:

        from prometheus_client import Counter

        c = Counter('my_failures_total', 'Description of counter')
        c.inc()     # Increment by 1
        c.inc(1.6)  # Increment by given value

    There are utilities to count exceptions raised:

        @c.count_exceptions()
        def f():
            pass

        with c.count_exceptions():
            pass

        # Count only one type of exception
        with c.count_exceptions(ValueError):
            pass
    rcCs0t�|j|j|jd|j|j�|_t��|_dS)Nr)	r�
ValueClassr4r7r/r0�_value�time�_created)r1rrrrK�s
zCounter._metric_initrcCs |dkrtd��|j�|�dS)z&Increment counter by the given amount.rz9Counters can only be incremented by non-negative amounts.N)rrc�inc)r1�amountrrrrf�szCounter.inccCs|��t||�S)z�Count exceptions in a block of code or function.

        Can be used as a function decorator or context manager.
        Increments the counter when an exception of the given
        type is raised up out of the code.
        )r5r)r1�	exceptionrrr�count_exceptionsszCounter.count_exceptionscCsdi|j��fdi|jffS)Nrre)rc�getre)r1rrrr[szCounter._child_samplesN)r)
rFrEr`�__doc__r4rKrf�	Exceptionrir[rrrrra�s

racs�eZdZdZdZed�Zddddeddf�fdd	�	Zd
d�Z	dd
d�Z
ddd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Z�ZS)�Gaugea�Gauge metric, to report instantaneous values.

     Examples of Gauges include:
        - Inprogress requests
        - Number of items in a queue
        - Free memory
        - Total memory
        - Temperature

     Gauges can go both up and down.

        from prometheus_client import Gauge

        g = Gauge('my_inprogress_requests', 'Description of gauge')
        g.inc()      # Increment by 1
        g.dec(10)    # Decrement by given value
        g.set(4.2)   # Set to a given value

     There are utilities for common use cases:

        g.set_to_current_time()   # Set to current unixtime

        # Increment when entered, decrement when exited.
        @g.track_inprogress()
        def f():
            pass

        with g.track_inprogress():
            pass

     A Gauge can also take its value from a callback:

        d = Gauge('data_objects', 'Number of objects')
        my_dict = {}
        d.set_function(lambda: len(my_dict))
    Zgauge)�min�maxZlivesumZliveall�allrrNrpc


sL|	|_|	|jkrtd|	��tt|�j||||||||d�|j|jd<dS)NzInvalid multiprocess mode: )r!rMr+r"r#r$rNrO�multiprocess_mode)�_multiprocess_mode�_MULTIPROC_MODESr�superrmrPrH)
r1r!rMr+r"r#r$rNrOrq)rrrrP=s

zGauge.__init__cCs(tj|j|j|j|j|j|jd�|_dS)N)rq)rrbr4r7r/r0rrrc)r1rrrrKWszGauge._metric_initrcCs|j�|�dS)z$Increment gauge by the given amount.N)rcrf)r1rgrrrrf]sz	Gauge.inccCs|j�|�dS)z$Decrement gauge by the given amount.N)rcrf)r1rgrrr�decasz	Gauge.deccCs|j�t|��dS)zSet gauge to the given value.N)rc�set�float)r1r@rrrrvesz	Gauge.setcCs|�t���dS)z"Set gauge to the current unixtime.N)rvrd)r1rrr�set_to_current_timeiszGauge.set_to_current_timecCs|��t|�S)z�Track inprogress blocks of code or functions.

        Can be used as a function decorator or context manager.
        Increments the gauge when the code is entered,
        and decrements when it is exited.
        )r5r)r1rrr�track_inprogressmszGauge.track_inprogresscCs|��t|j�S)z�Time a block of code or function, and set the duration in seconds.

        Can be used as a function decorator or context manager.
        )r5rrv)r1rrrrdwsz
Gauge.timecs�fdd�}t||�|_dS)z�Call the provided function to return the Gauge value.

        The function must return a float, and may be called from
        multiple threads. All other methods of the Gauge become NOOPs.
        csdit���ffS)Nr)rw)r1)�frr�samples�sz#Gauge.set_function.<locals>.samplesN)rr[)r1rzr{r)rzr�set_functionszGauge.set_functioncCsdi|j��ffS)Nr)rcrj)r1rrrr[�szGauge._child_samples)r)r)rFrEr`rkr4�	frozensetrsrrPrKrfrurvrxryrdr|r[�
__classcell__rr)rrrms&$


rmc@s:eZdZdZdZdgZdd�Zdd�Zdd	�Zd
d�Z	dS)
�Summarya�A Summary tracks the size and number of events.

    Example use cases for Summaries:
    - Response latency
    - Request size

    Example for a Summary:

        from prometheus_client import Summary

        s = Summary('request_size_bytes', 'Request size (bytes)')
        s.observe(512)  # Observe 512 (bytes)

    Example for a Summary using time:

        from prometheus_client import Summary

        REQUEST_TIME = Summary('response_latency_seconds', 'Response latency (seconds)')

        @REQUEST_TIME.time()
        def create_response(request):
          '''A dummy function'''
          time.sleep(1)

    Example for using the same Summary object as a context manager:

        with REQUEST_TIME.time():
            pass  # Logic to be timed
    �summaryZquantilecCsRt�|j|j|jd|j|j�|_t�|j|j|jd|j|j�|_t��|_	dS)N�_count�_sum)
rrbr4r7r/r0r�r�rdre)r1rrrrK�s
"zSummary._metric_initcCs|j�d�|j�|�dS)zObserve the given amount.rN)r�rfr�)r1rgrrr�observe�szSummary.observecCs|��t|j�S)z�Time a block of code or function, and observe the duration in seconds.

        Can be used as a function decorator or context manager.
        )r5rr�)r1rrrrd�szSummary.timecCs*di|j��fdi|j��fdi|jffS)Nr�r�re)r�rjr�re)r1rrrr[�szSummary._child_samplesN)
rFrEr`rkr4r)rKr�rdr[rrrrr�srcs�eZdZdZdZdgZdddddd	d
ddd
ddddefZddddedef�fdd�	Z	dd�Z
dd�Zdd�Zdd�Z
dd �Z�ZS)!�	HistogramaA Histogram tracks the size and number of events in buckets.

    You can use Histograms for aggregatable calculation of quantiles.

    Example use cases:
    - Response latency
    - Request size

    Example for a Histogram:

        from prometheus_client import Histogram

        h = Histogram('request_size_bytes', 'Request size (bytes)')
        h.observe(512)  # Observe 512 (bytes)

    Example for a Histogram using time:

        from prometheus_client import Histogram

        REQUEST_TIME = Histogram('response_latency_seconds', 'Response latency (seconds)')

        @REQUEST_TIME.time()
        def create_response(request):
          '''A dummy function'''
          time.sleep(1)

    Example of using the same Histogram object as a context manager:

        with REQUEST_TIME.time():
            pass  # Logic to be timed

    The default buckets are intended to cover a typical web/rpc request from milliseconds to seconds.
    They can be overridden by passing `buckets` keyword argument to `Histogram`.
    Z	histogram�leg{�G�zt?g{�G�z�?g�������?g�������?g333333�?g�������?g�?g�?g�?g�?g@g@g@g$@rrNc


s8|�|	�tt|�j||||||||d�|	|jd<dS)N)r!rMr+r"r#r$rNrO�buckets)�_prepare_bucketsrtr�rPrH)
r1r!rMr+r"r#r$rNrOr�)rrrrP�s

zHistogram.__init__cCsZdd�|D�}|t|�kr"td��|r<|dtkr<|�t�t|�dkrPtd��||_dS)NcSsg|]}t|��qSr)rw)rR�brrr�
<listcomp>
sz.Histogram._prepare_buckets.<locals>.<listcomp>zBuckets not in sorted order����zMust have at least two buckets)rWrr�appendrX�
_upper_bounds)r1r�rrrr�	s
zHistogram._prepare_bucketscCs�g|_t��|_|jd}t�|j|j|jd|j|j�|_	x>|j
D]4}|j�t�|j|j|jd||jt|�f��qDWdS)N)r�r��_bucket)
�_bucketsrdrer/rrbr4r7r0r�r�r�r
)r1Zbucket_labelnamesr�rrrrKs

"
zHistogram._metric_initcCsB|j�|�x0t|j�D]"\}}||kr|j|�d�PqWdS)zObserve the given amount.rN)r�rf�	enumerater�r�)r1rg�i�boundrrrr�#s
zHistogram.observecCs
t|j�S)z�Time a block of code or function, and observe the duration in seconds.

        Can be used as a function decorator or context manager.
        )rr�)r1rrrrd+szHistogram.timecCs�g}d}x@t|j�D]2\}}||j|��7}|�ddt|�i|f�qW|�di|f�|jddkr~|�di|j��f�|�di|jf�t|�S)Nrr�r�r�r�re)	r�r�r�rjr�r
r�rer')r1r{�accr�r�rrrr[2szHistogram._child_samples)rFrEr`rkr4r)rZDEFAULT_BUCKETSrrPr�rKr�rdr[r~rr)rrr��s ""r�c@s,eZdZdZdZdd�Zdd�Zdd�Zd	S)
�Infoa�Info metric, key-value pairs.

     Examples of Info include:
        - Build information
        - Version information
        - Potential target metadata

     Example usage:
        from prometheus_client import Info

        i = Info('my_build', 'Description of info')
        i.info({'version': '1.2.3', 'buildhost': 'foo@bar'})

     Info metrics do not work in multiprocess mode.
    rcCst|j�|_t�|_i|_dS)N)rvr/�_labelname_setrrIrc)r1rrrrKQszInfo._metric_initc	CsB|j�|���r"td|j|f��|j�t|�|_WdQRXdS)zSet info metric.z8Overlapping labels for Info metric, metric: %s child: %sN)r��intersection�keysrr/rIrUrc)r1�valrrrrVs
z	Info.infoc	Cs |j�d|jdffSQRXdS)N�_infog�?)rIrc)r1rrrr[^szInfo._child_samplesN)rFrEr`rkr4rKrr[rrrrr�?s
r�csLeZdZdZdZddddeddf�fdd�	Zdd	�Zd
d�Zdd
�Z	�Z
S)�Enuma`Enum metric, which of a set of states is true.

     Example usage:
        from prometheus_client import Enum

        e = Enum('task_state', 'Description of enum',
          states=['starting', 'running', 'stopped'])
        e.state('running')

     The first listed state will be the default.
     Enum metrics do not work in multiprocess mode.
    rrrNc


s\tt|�j||||||||d�||kr6td|f��|	sHtd|f��|	|jd<|_dS)N)r!rMr+r"r#r$rNrOz&Overlapping labels for Enum metric: %sz&No states provided for Enum metric: %s�states)rtr�rPrrH�_states)
r1r!rMr+r"r#r$rNrOr�)rrrrPrs
z
Enum.__init__cCsd|_t�|_dS)Nr)rcrrI)r1rrrrK�szEnum._metric_initc	Cs,|��|j�|j�|�|_WdQRXdS)zSet enum metric state.N)r5rIr��indexrc)r1�staterrrr��sz
Enum.statec	s*�j��fdd�t�j�D�SQRXdS)Ncs.g|]&\}}d�j|i|�jkr$dndf�qS)rrr)r7rc)rRr��s)r1rrr��sz'Enum._child_samples.<locals>.<listcomp>)rIr�r�)r1r)r1rr[�s
zEnum._child_samples)rFrEr`rkr4rrPrKr�r[r~rr)rrr�csr�)$�sys�	threadingrrdrrrZcontext_managersrrrZmetrics_corerr	r
rrNr�utilsr
r�version_infor3rQrrr&r-�objectr.rarmrr�r�r�rrrr�<module>s,
%>z;u$