o
    Y۷i8B                     @   s  d dl mZmZ d dlmZ d dlmZ d dlmZ	 d dl
mZmZmZmZmZmZmZmZmZ d dlmZ d dlmZ d dlmZ d d	lmZ eeZe	d
Ze	dZeddG dd dZ eddG dd dZ!edddZ"eee!gee f eee e!df f Z#G dd deZ$G dd deee" Z%G dd de%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%e) e)Z+G d%d& d&e'Z,G d'd( d(e,Z-G d)d* d*e%e, e,Z.G d+d, d,e(Z/G d-d. d.e/Z0G d/d0 d0e&e/ e/Z1G d1d2 d2e(Z2G d3d4 d4e2Z3G d5d6 d6e&e2 e2Z4G d7d8 d8e'Z5G d9d: d:e5Z6G d;d< d<e%e5 e5Z7G d=d> d>e(Z8G d?d@ d@e8Z9G dAdB dBe&e8 e8Z:G dCdD dDe'Z;G dEdF dFe;Z<G dGdH dHe%e; e;Z=dS )I    )ABCabstractmethod)	dataclass)	getLogger)compile)	CallableDict	GeneratorGenericIterableOptionalSequenceTypeVarUnion)metrics)Context)Observation)
Attributesz[a-zA-Z][-_./a-zA-Z0-9]{0,254}z[\x00-\x7F]{0,63}T)frozenc                   @   s"   e Zd ZU dZeee  ed< dS )_MetricsHistogramAdvisoryNexplicit_bucket_boundaries)__name__
__module____qualname__r   r   r   float__annotations__ r   r   `/home/ubuntu/vllm_env/lib/python3.10/site-packages/opentelemetry/metrics/_internal/instrument.pyr   0   s   
 r   c                   @   s   e Zd ZU dZdZeed< dS )CallbackOptionszOptions for the callback

    Args:
        timeout_millis: Timeout for the callback's execution. If the callback does asynchronous
            work (e.g. HTTP requests), it should respect this timeout.
    i'  timeout_millisN)r   r   r   __doc__r   r   r   r   r   r   r   r   5   s   
 r   InstrumentT
Instrument)boundNc                   @   s^   e Zd ZdZe		ddedededdfdd	Zededededeee	e f fd
dZ
dS )r"   z7Abstract class that serves as base for all instruments. nameunitdescriptionreturnNc                 C      d S Nr   selfr%   r&   r'   r   r   r   __init__L   s   zInstrument.__init__c                 C   sp   i }t | dur| |d< nd|d< |du rd}t|dur$||d< nd|d< |du r2d|d< |S ||d< |S )as  
        Checks the following instrument name, unit and description for
        compliance with the spec.

        Returns a dict with keys "name", "unit" and "description", the
        corresponding values will be the checked strings or `None` if the value
        is invalid. If valid, the checked strings should be used instead of the
        original values.
        Nr%   r$   r&   r'   )_name_regex	fullmatch_unit_regex)r%   r&   r'   resultr   r   r   _check_name_unit_descriptionU   s   

z'Instrument._check_name_unit_descriptionr$   r$   )r   r   r   r    r   strr-   staticmethodr   r   r2   r   r   r   r   r"   I   s0    c                	   @   sL   e Zd Z		ddedededdfddZdddZed	d
defddZdS )_ProxyInstrumentr$   r%   r&   r'   r(   Nc                 C   s   || _ || _|| _d | _d S r*   )_name_unit_description_real_instrumentr+   r   r   r   r-   z   s   
z_ProxyInstrument.__init__metermetrics.Meterc                 C   s   |  || _dS )z;Called when a real meter is set on the creating _ProxyMeterN)_create_real_instrumentr:   r,   r;   r   r   r   on_meter_set   s   z_ProxyInstrument.on_meter_setc                 C      dS )z:Create an instance of the real instrument. Implement this.Nr   r>   r   r   r   r=          z(_ProxyInstrument._create_real_instrumentr3   )r;   r<   r(   N)	r   r   r   r4   r-   r?   r   r!   r=   r   r   r   r   r6   y   s    

r6   c                       sB   e Zd Z			d
dedeee  dededdf
 fdd	Z  ZS )_ProxyAsynchronousInstrumentNr$   r%   	callbacksr&   r'   r(   c                    s   t  ||| || _d S r*   )superr-   
_callbacksr,   r%   rC   r&   r'   	__class__r   r   r-      s   
z%_ProxyAsynchronousInstrument.__init__Nr$   r$   )	r   r   r   r4   r   r   	CallbackTr-   __classcell__r   r   rG   r   rB      s    
rB   c                   @      e Zd ZdZdS )Synchronousz*Base class for all synchronous instrumentsNr   r   r   r    r   r   r   r   rM          rM   c                       sJ   e Zd ZdZe			ddedeee  dededdf
 fd	d
Z	  Z
S )Asynchronousz+Base class for all asynchronous instrumentsNr$   r%   rC   r&   r'   r(   c                    s   t  j|||d d S N)r&   r'   rD   r-   rF   rG   r   r   r-      s   zAsynchronous.__init__rI   )r   r   r   r    r   r4   r   r   rJ   r-   rK   r   r   rG   r   rP      s"    
rP   c                
   @   D   e Zd ZdZe		d	deeef dee	 dee
 ddfddZdS )
CounterzOA Counter is a synchronous `Instrument` which supports non-negative increments.Namount
attributescontextr(   c                 C   r@   )ak  Records an increment to the counter.

        Args:
            amount: The amount to increment the counter by. Must be non-negative.
            attributes: Optional set of attributes to associate with the measurement.
            context: Optional context to associate with the measurement. If not
                provided, the current context is used.
        Nr   r,   rU   rV   rW   r   r   r   add   rA   zCounter.addNNr   r   r   r    r   r   intr   r   r   r   rY   r   r   r   r   rT          
rT   c                	       l   e Zd ZdZ		ddedededdf fdd	Z		dd
eeef de	e
 de	e ddf fddZ  ZS )NoOpCounterz"No-op implementation of `Counter`.r$   r%   r&   r'   r(   Nc                       t  j|||d d S rQ   rR   r+   rG   r   r   r-         zNoOpCounter.__init__rU   rV   rW   c                       t  j|||dS N)rV   rW   rD   rY   rX   rG   r   r   rY         zNoOpCounter.addr3   rZ   r   r   r   r    r4   r-   r   r\   r   r   r   r   rY   rK   r   r   rG   r   r_      0    
r_   c                	   @   N   e Zd Z		ddeeef dee dee ddfddZ	dd	de
fd
dZdS )_ProxyCounterNrU   rV   rW   r(   c                 C      | j r| j ||| d S d S r*   r:   rY   rX   r   r   r   rY         z_ProxyCounter.addr;   r<   c                 C      | | j| j| jS r*   )create_counterr7   r8   r9   r>   r   r   r   r=      
   z%_ProxyCounter._create_real_instrumentrZ   )r   r   r   r   r\   r   r   r   r   rY   rT   r=   r   r   r   r   ri          

	ri   c                
   @   rS   )
UpDownCounterzXAn UpDownCounter is a synchronous `Instrument` which supports increments and decrements.NrU   rV   rW   r(   c                 C   r@   )a/  Records an increment or decrement to the counter.

        Unlike `Counter`, the ``amount`` may be negative, allowing the
        instrument to track values that go up and down (e.g. number of
        active requests, queue depth).

        Args:
            amount: The amount to add to the counter. May be positive or negative.
            attributes: Optional set of attributes to associate with the measurement.
            context: Optional context to associate with the measurement. If not
                provided, the current context is used.
        Nr   rX   r   r   r   rY      rA   zUpDownCounter.addrZ   r[   r   r   r   r   rq      r]   rq   c                	       r^   )NoOpUpDownCounterz(No-op implementation of `UpDownCounter`.r$   r%   r&   r'   r(   Nc                    r`   rQ   rR   r+   rG   r   r   r-     ra   zNoOpUpDownCounter.__init__rU   rV   rW   c                    rb   rc   rd   rX   rG   r   r   rY     re   zNoOpUpDownCounter.addr3   rZ   rf   r   r   rG   r   rr     rg   rr   c                	   @   rh   )_ProxyUpDownCounterNrU   rV   rW   r(   c                 C   rj   r*   rk   rX   r   r   r   rY     rl   z_ProxyUpDownCounter.addr;   r<   c                 C   rm   r*   )create_up_down_counterr7   r8   r9   r>   r   r   r   r=      ro   z+_ProxyUpDownCounter._create_real_instrumentrZ   )r   r   r   r   r\   r   r   r   r   rY   rq   r=   r   r   r   r   rs     rp   rs   c                   @   rL   )ObservableCounterzAn ObservableCounter is an asynchronous `Instrument` which reports monotonically
    increasing value(s) when the instrument is being observed.
    NrN   r   r   r   r   ru   (  rO   ru   c                       F   e Zd ZdZ			ddedeee  dededdf
 fd	d
Z  Z	S )NoOpObservableCounterz,No-op implementation of `ObservableCounter`.Nr$   r%   rC   r&   r'   r(   c                       t  j||||d d S rQ   rR   rF   rG   r   r   r-   1     
zNoOpObservableCounter.__init__rI   
r   r   r   r    r4   r   r   rJ   r-   rK   r   r   rG   r   rw   .       
rw   c                   @      e Zd ZdddefddZdS )_ProxyObservableCounterr;   r<   r(   c                 C      | | j| j| j| jS r*   )create_observable_counterr7   rE   r8   r9   r>   r   r   r   r=   C     z/_ProxyObservableCounter._create_real_instrumentN)r   r   r   ru   r=   r   r   r   r   r}   @  s    r}   c                   @   rL   )ObservableUpDownCountera  An ObservableUpDownCounter is an asynchronous `Instrument` which reports additive value(s) (e.g.
    the process heap size - it makes sense to report the heap size from multiple processes and sum them
    up, so we get the total heap usage) when the instrument is being observed.
    NrN   r   r   r   r   r   N  rO   r   c                       rv   )NoOpObservableUpDownCounterz2No-op implementation of `ObservableUpDownCounter`.Nr$   r%   rC   r&   r'   r(   c                    rx   rQ   rR   rF   rG   r   r   r-   X  ry   z$NoOpObservableUpDownCounter.__init__rI   rz   r   r   rG   r   r   U  r{   r   c                   @   r|   )_ProxyObservableUpDownCounterr;   r<   r(   c                 C   r~   r*   )!create_observable_up_down_counterr7   rE   r8   r9   r>   r   r   r   r=   k  r   z5_ProxyObservableUpDownCounter._create_real_instrumentN)r   r   r   r   r=   r   r   r   r   r   g      r   c                   @   sv   e Zd ZdZe			ddedededeee  ddf
d	d
Z	e		dde
eef dee dee ddfddZdS )	HistogramzHistogram is a synchronous `Instrument` which can be used to report arbitrary values
    that are likely to be statistically meaningful. It is intended for statistics such as
    histograms, summaries, and percentile.
    r$   Nr%   r&   r'   #explicit_bucket_boundaries_advisoryr(   c                 C   r)   r*   r   r,   r%   r&   r'   r   r   r   r   r-   |  s   zHistogram.__init__rU   rV   rW   c                 C   r@   )a  Records a measurement.

        Used to report measurements that are likely to be statistically
        meaningful, such as request durations, payload sizes, or any value
        for which a distribution (e.g. percentiles) is useful.

        Args:
            amount: The measurement to record. Should be non-negative in most
                cases; negative values are only meaningful when the histogram
                is used to track signed deltas.
            attributes: Optional set of attributes to associate with the measurement.
            context: Optional context to associate with the measurement. If not
                provided, the current context is used.
        Nr   rX   r   r   r   record  rA   zHistogram.recordr$   r$   NrZ   )r   r   r   r    r   r4   r   r   r   r-   r   r\   r   r   r   r   r   r   r   r   v  s:    
	
r   c                       sz   e Zd ZdZ			ddedededeee  ddf
 fd	d
Z		dde	e
ef dee dee ddf fddZ  ZS )NoOpHistogramz$No-op implementation of `Histogram`.r$   Nr%   r&   r'   r   r(   c                    rx   )N)r&   r'   r   rR   r   rG   r   r   r-     ry   zNoOpHistogram.__init__rU   rV   rW   c                    rb   rc   )rD   r   rX   rG   r   r   r     re   zNoOpHistogram.recordr   rZ   )r   r   r   r    r4   r   r   r   r-   r   r\   r   r   r   rK   r   r   rG   r   r     s6    

r   c                       s   e Zd Z			ddedededeee  ddf
 fdd	Z		dd
ee	ef dee
 dee ddfddZdddefddZ  ZS )_ProxyHistogramr$   Nr%   r&   r'   r   r(   c                    s   t  j|||d || _d S rQ   )rD   r-   $_explicit_bucket_boundaries_advisoryr   rG   r   r   r-     s   z_ProxyHistogram.__init__rU   rV   rW   c                 C   rj   r*   )r:   r   rX   r   r   r   r     rl   z_ProxyHistogram.recordr;   r<   c                 C   s   |j | j| j| j| jdS )N)r   )create_histogramr7   r8   r9   r   r>   r   r   r   r=     s   z'_ProxyHistogram._create_real_instrumentr   rZ   )r   r   r   r4   r   r   r   r-   r   r\   r   r   r   r   r=   rK   r   r   rG   r   r     s6    


	r   c                   @   rL   )ObservableGaugezAsynchronous Gauge is an asynchronous `Instrument` which reports non-additive value(s) (e.g.
    the room temperature - it makes no sense to report the temperature value from multiple rooms
    and sum them up) when the instrument is being observed.
    NrN   r   r   r   r   r     rO   r   c                       rv   )NoOpObservableGaugez*No-op implementation of `ObservableGauge`.Nr$   r%   rC   r&   r'   r(   c                    rx   rQ   rR   rF   rG   r   r   r-     ry   zNoOpObservableGauge.__init__rI   rz   r   r   rG   r   r     r{   r   c                   @   r|   )_ProxyObservableGauger;   r<   r(   c                 C   r~   r*   )create_observable_gauger7   rE   r8   r9   r>   r   r   r   r=     r   z-_ProxyObservableGauge._create_real_instrumentN)r   r   r   r   r=   r   r   r   r   r     r   r   c                
   @   rS   )
GaugezdA Gauge is a synchronous `Instrument` which can be used to record non-additive values as they occur.NrU   rV   rW   r(   c                 C   r@   )a&  Records the current value of the gauge.

        The gauge reports the last recorded value when observed. It is
        intended for non-additive measurements where only the current
        value matters (e.g. CPU utilisation percentage, room temperature).

        Args:
            amount: The current value to record.
            attributes: Optional set of attributes to associate with the measurement.
            context: Optional context to associate with the measurement. If not
                provided, the current context is used.
        Nr   rX   r   r   r   set  rA   z	Gauge.setrZ   )r   r   r   r    r   r   r\   r   r   r   r   r   r   r   r   r   r     r]   r   c                	       r^   )	NoOpGaugez"No-op implementation of ``Gauge``.r$   r%   r&   r'   r(   Nc                    r`   rQ   rR   r+   rG   r   r   r-     ra   zNoOpGauge.__init__rU   rV   rW   c                    rb   rc   )rD   r   rX   rG   r   r   r   !  re   zNoOpGauge.setr3   rZ   )r   r   r   r    r4   r-   r   r\   r   r   r   r   r   rK   r   r   rG   r   r     rg   r   c                	   @   rh   )_ProxyGaugeNrU   rV   rW   r(   c                 C   rj   r*   )r:   r   rX   r   r   r   r   .  rl   z_ProxyGauge.setr;   r<   c                 C   rm   r*   )create_gauger7   r8   r9   r>   r   r   r   r=   7  ro   z#_ProxyGauge._create_real_instrumentrZ   )r   r   r   r   r\   r   r   r   r   r   r   r=   r   r   r   r   r   *  s    

	r   )>abcr   r   dataclassesr   loggingr   rer   
re_compiletypingr   r   r	   r
   r   r   r   r   r   opentelemetryr   opentelemetry.contextr   +opentelemetry.metrics._internal.observationr   opentelemetry.util.typesr   r   _loggerr.   r0   r   r   r!   rJ   r"   r6   rB   rM   rP   rT   r_   ri   rq   rr   rs   ru   rw   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sv   ,0

'

