o
    `j                     @   s:   d dl Z d dlZd dlmZ d dlmZ G dd dZdS )    N)QuotaViolationError)KafkaMetricc                   @   sj   e Zd ZdZdZdd Zdd Zedd Zed	d
 Z	dddZ
dd ZdddZdddZdd ZdS )Sensora5  
    A sensor applies a continuous sequence of numerical values
    to a set of associated metrics. For example a sensor on
    message size would record a sequence of message sizes using
    the `record(double)` api and would maintain a set
    of metrics about request sizes such as the average or max.
    )	_lock	_registry_name_parents_metrics_stats_config#_inactive_sensor_expiration_time_ms_last_record_timec                 C   sf   |st dt | _|| _|| _|pg | _g | _g | _|| _	|d | _
t d | _| t  d S )Nzname must be non-empty  )
ValueError	threadingRLockr   r   r   r   r	   r
   r   r   time	monotonicr   _check_forestset)selfregistrynameparentsconfig'inactive_sensor_expiration_time_seconds r   X/home/djax/ivt_ai_plugin/venv/lib/python3.10/site-packages/kafka/metrics/stats/sensor.py__init__   s   

zSensor.__init__c                 C   s<   | |v rt d| jf ||  | jD ]}|| qdS )z<Validate that this sensor doesn't end up referencing itself.z4Circular dependency in sensors: %s is its ownparent.N)r   r   addr   r   )r   sensorsparentr   r   r   r   $   s   

zSensor._check_forestc                 C   s   | j S )zy
        The name this sensor is registered with.
        This name will be unique among all registered sensors.
        )r   r   r   r   r   r   -   s   zSensor.namec                 C   s
   t | jS N)tupler	   r"   r   r   r   metrics5   s   
zSensor.metrics      ?Nc                 C   s   |du r
t  d }|| _| j | jD ]
}|| j|| q| | W d   n1 s.w   Y  | jD ]}||| q6dS )a  
        Record a value at a known time.
        Arguments:
            value (double): The value we are recording
            time_ms (int): A POSIX timestamp in milliseconds.
                Default: The time when record() is evaluated (now)

        Raises:
            QuotaViolationException: if recording this value moves a
                metric beyond its configured maximum or minimum bound
        Nr   )	r   r   r   r   r
   recordr   _check_quotasr   )r   valuetime_msstatr!   r   r   r   r'   9   s   

zSensor.recordc                 C   sP   | j D ]"}|jr%|jjr%||}|jj|s%td|j||jjjf qdS )zh
        Check if we have violated our quota for any metric that
        has a configured quota
        z.'%s' violated quota. Actual: %d, Threshold: %dN)r	   r   quotar)   is_acceptabler   metric_namebound)r   r*   metricr)   r   r   r   r(   P   s   

zSensor._check_quotasc                 C   sX   |st d| j| | D ]}t|j|j|p| j}| j	| | j
| qdS )a  
        Register a compound statistic with this sensor which
        yields multiple measurable quantities (like a histogram)

        Arguments:
            stat (AbstractCompoundStat): The stat to register
            config (MetricConfig): The configuration for this stat.
                If None then the stat will use the default configuration
                for this sensor.
        zcompound stat must be non-emptyN)r   r
   appendstatsr   r   r+   r   r   register_metricr	   )r   compound_statr   named_measurabler0   r   r   r   add_compound_   s   
zSensor.add_compoundc                 C   sb   | j $ t|||p| j}| j| | j| | j| W d   dS 1 s*w   Y  dS )aP  
        Register a metric with this sensor

        Arguments:
            metric_name (MetricName): The name of the metric
            stat (AbstractMeasurableStat): The statistic to keep
            config (MetricConfig): A special configuration for this metric.
                If None use the sensor default configuration.
        N)r   r   r   r   r3   r	   r1   r
   )r   r.   r+   r   r0   r   r   r   r   s   s   
"z
Sensor.addc                 C   s   t  d | j | jkS )zV
        Return True if the Sensor is eligible for removal due to inactivity.
        r   )r   r   r   r   r"   r   r   r   has_expired   s   zSensor.has_expired)r&   Nr#   )__name__
__module____qualname____doc__	__slots__r   r   propertyr   r%   r'   r(   r6   r   r7   r   r   r   r   r      s    	




r   )r   r   kafka.errorsr   kafka.metricsr   r   r   r   r   r   <module>   s
    