
    9h)                         S SK r S SKrS SKrS SKrSSKJr  SSKJr  SSKJ	r	  SSK
Jr  SSKJr  \ R                  " \5      r " S S	5      rg)
    N   )KafkaMetric)AnonMeasurable)MetricConfig)
MetricName)Sensorc                       \ rS rSrSrSS jr\S 5       r\S 5       rSS jr	S r
S\R                  S4S	 jrS
 rSS jrS rS rS r " S S5      rS rSrg)Metrics   ar  
A registry of sensors and metrics.

A metric is a named, numerical measurement. A sensor is a handle to
record numerical measurements as they occur. Each Sensor has zero or
more associated metrics. For example a Sensor might represent message
sizes and we might associate with this sensor a metric for the average,
maximum, or other statistics computed off the sequence of message sizes
that are recorded by the sensor.

Usage looks something like this:
    # set up metrics:
    metrics = Metrics() # the global repository of metrics and sensors
    sensor = metrics.sensor('message-sizes')
    metric_name = MetricName('message-size-avg', 'producer-metrics')
    sensor.add(metric_name, Avg())
    metric_name = MetricName('message-size-max', 'producer-metrics')
    sensor.add(metric_name, Max())

    # as messages are sent we record the sizes
    sensor.record(message_size);
Nc                   ^  [         R                  " 5       T l        U=(       d
    [        5       T l        0 T l        0 T l        0 T l        U=(       d    / T l        T R                   H  nUR                  / 5        M     U(       a1  U 4S jn[         R                  " US9nSUl        UR                  5         T R                  T R                  SSS5      [        U 4S j5      5        g)	a  
Create a metrics repository with a default config, given metric
reporters and the ability to expire eligible sensors

Arguments:
    default_config (MetricConfig, optional): The default config
    reporters (list of AbstractMetricsReporter, optional):
        The metrics reporters
    enable_expiration (bool, optional): true if the metrics instance
        can garbage collect inactive sensors, false otherwise
c                  l   >  [         R                  " S5        T R                  R                  T 5        M3  )N   )timesleepExpireSensorTaskrunselfs   ]C:\Suresh\moveshuttle\MDcreated\moveengine\venv\Lib\site-packages\aiokafka/metrics/metrics.pyexpire_loop%Metrics.__init__.<locals>.expire_loop>   s*    JJrN))--d3     )targetTcountzkafka-metrics-countz"total number of registered metricsc                 .   > [        TR                  5      $ N)len_metrics)confignowr   s     r   <lambda>"Metrics.__init__.<locals>.<lambda>M   s    s4==/Ar   N)	threadingRLock_lockr   _config_sensorsr   _children_sensors
_reportersinitThreaddaemonstart
add_metricmetric_namer   )r   default_config	reportersenable_expirationreporterr   metrics_schedulers   `      r   __init__Metrics.__init__'   s     __&
%7!##/rHMM" ( 4 !* 0 0 D'+$##%.0T AB		
r   c                     U R                   $ r   )r&   r   s    r   r   Metrics.configP   s    ||r   c                     U R                   $ )zD
Get all the metrics currently maintained and indexed by metricName
)r   r   s    r   metricsMetrics.metricsT   s    
 }}r   c                     [        U R                  R                  5      nUR                  U=(       d    0 5        [	        XX55      $ )a  
Create a MetricName with the given name, group, description and tags,
plus default tags specified in the metric configuration.
Tag in tags takes precedence if the same tag key is specified in
the default metric configuration.

Arguments:
    name (str): The name of the metric
    group (str): logical group name of the metrics to which this
        metric belongs
    description (str, optional): A human-readable description to
        include in the metric
    tags (dict, optionals): additional key/value attributes of
        the metric
)dictr   tagsupdater   )r   namegroupdescriptionr>   combined_tagss         r   r/   Metrics.metric_name[   s7      T[[--.TZR(${BBr   c                 ^    U(       d  [        S5      eU R                  R                  US5      $ )z
Get the sensor with the given name if it exists

Arguments:
    name (str): The name of the sensor

Returns:
    Sensor: The sensor or None if no such sensor exists
zname must be non-emptyN)
ValueErrorr'   get)r   r@   s     r   
get_sensorMetrics.get_sensoro   s*     566}}  t,,r   c                    U R                  U5      nU(       a  U$ U R                     U R                  U5      nU(       d  [        U UUU=(       d    U R                  U5      nXPR                  U'   U(       aL  U HF  nU R
                  R                  U5      nU(       d  / nXpR
                  U'   UR                  U5        MH     [        R                  SU5        UsSSS5        $ ! , (       d  f       g= f)ab  
Get or create a sensor with the given unique name and zero or
more parent sensors. All parent sensors will receive every value
recorded with this sensor.

Arguments:
    name (str): The name of the sensor
    config (MetricConfig, optional): A default configuration to use
        for this sensor for metrics that don't have their own config
    inactive_sensor_expiration_time_seconds (int, optional):
        If no value if recorded on the Sensor for this duration of
        time, it is eligible for removal
    parents (list of Sensor): The parent sensors

Returns:
    Sensor: The sensor that is created
zAdded sensor with name %sN)
rH   r%   r   r   r'   r(   rG   appendloggerdebug)r   r@   r   'inactive_sensor_expiration_time_secondsparentssensorparentchildrens           r   rP   Metrics.sensor}   s    0 &MZZ__T*F)dkk; '-d#")#'#9#9#=#=f#E'')H=E226: / #* 8$?' ZZs   B3C$$
C2c                 N   U R                   R                  U5      nU(       a  SnUR                     U R                     U R                   R                  US5      nU(       ae  XB:X  a`  UR                   H  nU R                  UR                  5        M      [        R                  SU5        U R                  R                  US5      nSSS5        SSS5        U(       a%  U H  nU R                  UR                  5        M      ggg! , (       d  f       ND= f! , (       d  f       NM= f)z
Remove a sensor (if it exists), associated metrics and its children.

Arguments:
    name (str): The name of the sensor to be removed
NzRemoved sensor with name %s)r'   rG   r%   popr:   remove_metricr/   rL   rM   r(   remove_sensorr@   )r   r@   rP   child_sensorsvalmetricchild_sensors          r   rW   Metrics.remove_sensor   s     ""4( Mtzzmm''d33="(..**6+=+=> #1LL!>E$($:$:$>$>vt$LM  * $1L&&|'8'89 %2  )zs#   DB	DD
D	D
D$c                 d    [        XU=(       d    U R                  5      nU R                  U5        g)a  
Add a metric to monitor an object that implements measurable.
This metric won't be associated with any sensor.
This is a way to expose existing values as metrics.

Arguments:
    metricName (MetricName): The name of the metric
    measurable (AbstractMeasurable): The measurable that will be
        measured by this metric
    config (MetricConfig, optional): The configuration to use when
        measuring this measurable
N)r   r   register_metric)r   r/   
measurabler   rZ   s        r   r.   Metrics.add_metric   s'     [f6KLV$r   c                     U R                      U R                  R                  US5      nU(       a$  U R                   H  nUR	                  U5        M     UsSSS5        $ ! , (       d  f       g= f)a/  
Remove a metric if it exists and return it. Return None otherwise.
If a metric is removed, `metric_removal` will be invoked
for each reporter.

Arguments:
    metric_name (MetricName): The name of the metric

Returns:
    KafkaMetric: the removed `KafkaMetric` or None if no such
        metric exists
N)r%   r   rU   r)   metric_removal)r   r/   rZ   r3   s       r   rV   Metrics.remove_metric   sM     ZZ]]&&{D9F $H++F3 !0 ZZs   A	A  
A.c                     U R                      UR                  [        U R                  R	                  5       5      5        U R
                  R                  U5        SSS5        g! , (       d  f       g= f)zAdd a MetricReporterN)r%   r*   listr:   valuesr)   rK   r   r3   s     r   add_reporterMetrics.add_reporter   sC    ZZMM$t||22456OO""8, ZZs   AA$$
A2c                 .   U R                      UR                  U R                  ;   a  [        SUR                   S35      eXR                  UR                  '   U R                   H  nUR                  U5        M     S S S 5        g ! , (       d  f       g = f)NzA metric named "z." already exists, cannot register another one.)r%   r/   r:   rF   r)   metric_change)r   rZ   r3   s      r   r^   Metrics.register_metric   sz    ZZ!!T\\1 &v'9'9&: ;- -  06LL++, OO&&v. , ZZs   A0B
Bc                   (    \ rS rSrSr\S 5       rSrg)Metrics.ExpireSensorTask   zm
This iterates over every Sensor and triggers a remove_sensor
if it has expired. Package private for testing
c                 &   [        U R                  R                  5       5      nU HV  u  p#UR                     UR	                  5       (       a'  [
        R                  SU5        U R                  U5        S S S 5        MX     g ! , (       d  f       Mj  = f)NzRemoving expired sensor %s)re   r'   itemsr%   has_expiredrL   rM   rW   )r:   rq   r@   rP   s       r   r   Metrics.ExpireSensorTask.run   sk    ))//12E % \\))++%A4H--d3 "\ !& "\s   =B
B	 N)__name__
__module____qualname____firstlineno____doc__staticmethodr   __static_attributes__rt   r   r   r   rn      s    	
 
	4 
	4r   r   c                 ~    U R                    H  nUR                  5         M     U R                  R                  5         g)zClose this metrics repository.N)r)   closer   clearrg   s     r   r}   Metrics.close  s,    HNN ( 	r   )r(   r&   r%   r   r)   r'   )NNF) Nr   )ru   rv   rw   rx   ry   r5   propertyr   r:   r/   rH   sysmaxsizerP   rW   r.   rV   rh   r^   r   r}   r{   rt   r   r   r
   r
      s}    .'
R    C(-" 03/b:*%"(-	/4 40r   r
   )loggingr   r#   r   kafka_metricr   r_   r   metric_configr   r/   r   statsr   	getLoggerru   rL   r
   rt   r   r   <module>r      s<     
   % & ' # 			8	$G Gr   