
    osjk?                        d dl mZmZ d dl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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       G d d             Z e	d       G d d             Z edd      Z eegee   f   eee   edf   z  Z! G d de      Z" G d deee          Z# G d de#e          Z$ G d de"      Z% G d de"      Z& G d  d!e%      Z' G d" d#e'      Z( G d$ d%e#e'   e'      Z) G d& d'e%      Z* G d( d)e*      Z+ G d* d+e#e*   e*      Z, G d, d-e&      Z- G d. d/e-      Z. G d0 d1e$e-   e-      Z/ G d2 d3e&      Z0 G d4 d5e0      Z1 G d6 d7e$e0   e0      Z2 G d8 d9e%      Z3 G d: d;e3      Z4 G d< d=e#e3   e3      Z5 G d> d?e&      Z6 G d@ dAe6      Z7 G dB dCe$e6   e6      Z8 G dD dEe%      Z9 G dF dGe9      Z: G dH dIe#e9   e9      Z;y)J    )ABCabstractmethod)Callable	GeneratorIterableSequence)	dataclass)	getLogger)compile)GenericTypeVar)metrics)Context)Observation)
Attributesz[a-zA-Z][-_./a-zA-Z0-9]{0,254}z[\x00-\x7F]{0,63}T)frozenc                   (    e Zd ZU dZee   dz  ed<   y)_MetricsHistogramAdvisoryNexplicit_bucket_boundaries)__name__
__module____qualname__r   r   float__annotations__     ]/root/.hermes/venv/lib/python3.12/site-packages/opentelemetry/metrics/_internal/instrument.pyr   r      s    9=$ 6=r   r   c                        e Zd ZU dZdZeed<   y)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   $   s     #NE"r   r   InstrumentT
Instrument)boundNc                   j    e Zd ZdZe	 	 d	dedededdfd       Zededededeeedz  f   fd       Z	y)
r#   z7Abstract class that serves as base for all instruments.nameunitdescriptionreturnNc                      y Nr   selfr&   r'   r(   s       r   __init__zInstrument.__init__;   s     	r   c                     i }t         j                  |       | |d<   nd|d<   |d}t        j                  |      ||d<   nd|d<   |d|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(   )_name_regex	fullmatch_unit_regex)r&   r'   r(   results       r   _check_name_unit_descriptionz'Instrument._check_name_unit_descriptionD   s     )+  &2!F6N!F6N<D  &2!F6N!F6N$&F=!  %0F=!r   r0   r0   )
r   r   r   r!   r   strr.   staticmethoddictr5   r   r   r   r#   r#   8   s    A 	  	
 
     +. 	c3:o	   r   c            	       L    e Zd Z	 	 ddedededdfdZdd	Zedddefd
       Zy)_ProxyInstrumentr&   r'   r(   r)   Nc                 <    || _         || _        || _        d | _        y r+   )_name_unit_description_real_instrumentr,   s       r   r.   z_ProxyInstrument.__init__i   s"     

'48r   metermetrics.Meterc                 0    | j                  |      | _        y)z;Called when a real meter is set on the creating _ProxyMeterN)_create_real_instrumentr@   r-   rA   s     r   on_meter_setz_ProxyInstrument.on_meter_sett   s     !% < <U Cr   c                      y)z:Create an instance of the real instrument. Implement this.Nr   rE   s     r   rD   z(_ProxyInstrument._create_real_instrument|       r   r6   )rA   rB   r)   N)	r   r   r   r7   r.   rF   r   r"   rD   r   r   r   r;   r;   h   sa     		9	9 	9 		9
 
	9D I_ I I Ir   r;   c                   H     e Zd Z	 	 	 ddedee   dz  dededdf
 fdZ xZS )	_ProxyAsynchronousInstrumentNr&   	callbacksr'   r(   r)   c                 6    t         |   |||       || _        y r+   )superr.   
_callbacksr-   r&   rK   r'   r(   	__class__s        r   r.   z%_ProxyAsynchronousInstrument.__init__   s     	t[1#r   Nr0   r0   )r   r   r   r7   r   	CallbackTr.   __classcell__rP   s   @r   rJ   rJ      sR     15$$ I&-$ 	$
 $ 
$ $r   rJ   c                       e Zd ZdZy)Synchronousz*Base class for all synchronous instrumentsNr   r   r   r!   r   r   r   rV   rV      s    4r   rV   c                   V     e Zd ZdZe	 	 	 d	dedee   dz  dededdf
 fd       Z xZ	S )
Asynchronousz+Base class for all asynchronous instrumentsNr&   rK   r'   r(   r)   c                 *    t         |   |||       y N)r'   r(   rM   r.   rO   s        r   r.   zAsynchronous.__init__   s     	DkBr   rQ   )
r   r   r   r!   r   r7   r   rR   r.   rS   rT   s   @r   rY   rY      sd    5 15CC I&-C 	C
 C 
C Cr   rY   c            
       J    e Zd ZdZe	 	 ddeez  dedz  dedz  ddfd       Z	y)	CounterzOA Counter is a synchronous `Instrument` which supports non-negative increments.Namount
attributescontextr)   c                      y)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-   r_   r`   ra   s       r   addzCounter.add   rH   r   NN
r   r   r   r!   r   intr   r   r   rd   r   r   r   r^   r^      sR    Y )-"&	e % 4	
 
 r   r^   c            	       n     e Zd ZdZ	 	 ddedededdf fdZ	 	 ddeez  d	edz  d
e	dz  ddf fdZ
 xZS )NoOpCounterz"No-op implementation of `Counter`.r&   r'   r(   r)   Nc                 *    t         |   |||       y r[   r\   r-   r&   r'   r(   rP   s       r   r.   zNoOpCounter.__init__        	DkBr   r_   r`   ra   c                 (    t         |   |||      S N)r`   ra   rM   rd   r-   r_   r`   ra   rP   s       r   rd   zNoOpCounter.add        w{6j'{JJr   r6   re   r   r   r   r!   r7   r.   rg   r   r   r   rd   rS   rT   s   @r   ri   ri          ,
 	CC C 	C
 
C )-"&	KeK %K 4	K
 
K Kr   ri   c            	       L    e Zd Z	 	 d
deez  dedz  dedz  ddfdZdddefd	Z	y)_ProxyCounterNr_   r`   ra   r)   c                 X    | j                   r| j                   j                  |||       y y r+   r@   rd   rc   s       r   rd   z_ProxyCounter.add   *       !!%%fj'B !r   rA   rB   c                 d    |j                  | j                  | j                  | j                        S r+   )create_counterr=   r>   r?   rE   s     r   rD   z%_ProxyCounter._create_real_instrument   s,    ##JJJJ
 	
r   re   )
r   r   r   rg   r   r   r   rd   r^   rD   r   r   r   ru   ru      sZ     )-"&	CeC %C 4	C
 
C
_ 
 
r   ru   c            
       J    e Zd ZdZe	 	 ddeez  dedz  dedz  ddfd       Z	y)	UpDownCounterzXAn UpDownCounter is a synchronous `Instrument` which supports increments and decrements.Nr_   r`   ra   r)   c                      y)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   rc   s       r   rd   zUpDownCounter.add   rH   r   re   rf   r   r   r   r|   r|      sR    b )-"&	e % 4	
 
 r   r|   c            	       n     e Zd ZdZ	 	 ddedededdf fdZ	 	 ddeez  d	edz  d
e	dz  ddf fdZ
 xZS )NoOpUpDownCounterz(No-op implementation of `UpDownCounter`.r&   r'   r(   r)   Nc                 *    t         |   |||       y r[   r\   rk   s       r   r.   zNoOpUpDownCounter.__init__   rl   r   r_   r`   ra   c                 (    t         |   |||      S rn   ro   rp   s       r   rd   zNoOpUpDownCounter.add   rq   r   r6   re   rr   rT   s   @r   r   r      s    2
 	CC C 	C
 
C )-"&	KeK %K 4	K
 
K Kr   r   c            	       L    e Zd Z	 	 d
deez  dedz  dedz  ddfdZdddefd	Z	y)_ProxyUpDownCounterNr_   r`   ra   r)   c                 X    | j                   r| j                   j                  |||       y y r+   rw   rc   s       r   rd   z_ProxyUpDownCounter.add  rx   r   rA   rB   c                 d    |j                  | j                  | j                  | j                        S r+   )create_up_down_counterr=   r>   r?   rE   s     r   rD   z+_ProxyUpDownCounter._create_real_instrument  s,    ++JJJJ
 	
r   re   )
r   r   r   rg   r   r   r   rd   r|   rD   r   r   r   r   r     sZ     )-"&	CeC %C 4	C
 
C
_ 
 
r   r   c                       e Zd ZdZy)ObservableCounterzAn ObservableCounter is an asynchronous `Instrument` which reports monotonically
    increasing value(s) when the instrument is being observed.
    NrW   r   r   r   r   r     s    r   r   c                   L     e Zd ZdZ	 	 	 d	dedee   dz  dededdf
 fdZ xZS )
NoOpObservableCounterz,No-op implementation of `ObservableCounter`.Nr&   rK   r'   r(   r)   c                 ,    t         |   ||||       y r[   r\   rO   s        r   r.   zNoOpObservableCounter.__init__   #     	#	 	 	
r   rQ   	r   r   r   r!   r7   r   rR   r.   rS   rT   s   @r   r   r     sU    6
 15

 I&-
 	

 
 

 
r   r   c                       e Zd ZdddefdZy)_ProxyObservableCounterrA   rB   r)   c                 z    |j                  | j                  | j                  | j                  | j                        S r+   )create_observable_counterr=   rN   r>   r?   rE   s     r   rD   z/_ProxyObservableCounter._create_real_instrument2  s5     ..JJOOJJ	
 	
r   N)r   r   r   r   rD   r   r   r   r   r   /  s    
$
	
r   r   c                       e Zd ZdZy)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.
    NrW   r   r   r   r   r   =      r   r   c                   L     e Zd ZdZ	 	 	 d	dedee   dz  dededdf
 fdZ xZS )
NoOpObservableUpDownCounterz2No-op implementation of `ObservableUpDownCounter`.Nr&   rK   r'   r(   r)   c                 ,    t         |   ||||       y r[   r\   rO   s        r   r.   z$NoOpObservableUpDownCounter.__init__G  r   r   rQ   r   rT   s   @r   r   r   D  sU    <
 15

 I&-
 	

 
 

 
r   r   c                       e Zd ZdddefdZy)_ProxyObservableUpDownCounterrA   rB   r)   c                 z    |j                  | j                  | j                  | j                  | j                        S r+   )!create_observable_up_down_counterr=   rN   r>   r?   rE   s     r   rD   z5_ProxyObservableUpDownCounter._create_real_instrumentZ  s5     66JJOOJJ	
 	
r   N)r   r   r   r   rD   r   r   r   r   r   V  s    
$
	 
r   r   c                       e Zd ZdZe	 	 	 ddedededee   dz  ddf
d       Ze	 	 dd	e	ez  d
e
dz  dedz  ddfd       Zy)	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.
    Nr&   r'   r(   #explicit_bucket_boundaries_advisoryr)   c                      y r+   r   )r-   r&   r'   r(   r   s        r   r.   zHistogram.__init__k  s     	r   r_   r`   ra   c                      y)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   rc   s       r   recordzHistogram.recordu  rH   r   r0   r0   Nre   )r   r   r   r!   r   r7   r   r   r.   rg   r   r   r   r   r   r   r   r   e  s    
  FJ  	
 .6e_t-C 
   )-"&	e % 4	
 
 r   r   c                        e Zd ZdZ	 	 	 ddedededee   dz  ddf
 fdZ	 	 dd	eez  d
e	dz  de
dz  ddf fdZ xZS )NoOpHistogramz$No-op implementation of `Histogram`.Nr&   r'   r(   r   r)   c                 ,    t         |   ||||       y )N)r'   r(   r   r\   r-   r&   r'   r(   r   rP   s        r   r.   zNoOpHistogram.__init__  s#     	#0S	 	 	
r   r_   r`   ra   c                 (    t         |   |||      S rn   )rM   r   rp   s       r   r   zNoOpHistogram.record  s     w~fW~MMr   r   re   )r   r   r   r!   r7   r   r   r.   rg   r   r   r   rS   rT   s   @r   r   r     s    .
 FJ

 
 	

 .6e_t-C
 

" )-"&	NeN %N 4	N
 
N Nr   r   c                        e Zd Z	 	 	 ddedededee   dz  ddf
 fdZ	 	 ddeez  d	edz  d
e	dz  ddfdZ
dddefdZ xZS )_ProxyHistogramNr&   r'   r(   r   r)   c                 8    t         |   |||       || _        y r[   )rM   r.   $_explicit_bucket_boundaries_advisoryr   s        r   r.   z_ProxyHistogram.__init__  s$     	DkB/ 	1r   r_   r`   ra   c                 X    | j                   r| j                   j                  |||       y y r+   )r@   r   rc   s       r   r   z_ProxyHistogram.record  s*       !!((WE !r   rA   rB   c                 |    |j                  | j                  | j                  | j                  | j                        S )N)r   )create_histogramr=   r>   r?   r   rE   s     r   rD   z'_ProxyHistogram._create_real_instrument  s:    %%JJJJ040Y0Y	 & 
 	
r   r   re   )r   r   r   r7   r   r   r.   rg   r   r   r   r   rD   rS   rT   s   @r   r   r     s     FJ



 

 	


 .6e_t-C

 


 )-"&	FeF %F 4	F
 
F
_ 
 
r   r   c                       e Zd ZdZy)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.
    NrW   r   r   r   r   r     r   r   r   c                   L     e Zd ZdZ	 	 	 d	dedee   dz  dededdf
 fdZ xZS )
NoOpObservableGaugez*No-op implementation of `ObservableGauge`.Nr&   rK   r'   r(   r)   c                 ,    t         |   ||||       y r[   r\   rO   s        r   r.   zNoOpObservableGauge.__init__  r   r   rQ   r   rT   s   @r   r   r     sU    4
 15

 I&-
 	

 
 

 
r   r   c                       e Zd ZdddefdZy)_ProxyObservableGaugerA   rB   r)   c                 z    |j                  | j                  | j                  | j                  | j                        S r+   )create_observable_gauger=   rN   r>   r?   rE   s     r   rD   z-_ProxyObservableGauge._create_real_instrument  s5     ,,JJOOJJ	
 	
r   N)r   r   r   r   rD   r   r   r   r   r     s    
$
	
r   r   c            
       J    e Zd ZdZe	 	 ddeez  dedz  dedz  ddfd       Z	y)	GaugezdA Gauge is a synchronous `Instrument` which can be used to record non-additive values as they occur.Nr_   r`   ra   r)   c                      y)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   rc   s       r   setz	Gauge.set  rH   r   re   )
r   r   r   r!   r   rg   r   r   r   r   r   r   r   r   r     sR    n )-"&	e % 4	
 
 r   r   c            	       n     e Zd ZdZ	 	 ddedededdf fdZ	 	 ddeez  d	edz  d
e	dz  ddf fdZ
 xZS )	NoOpGaugez"No-op implementation of ``Gauge``.r&   r'   r(   r)   Nc                 *    t         |   |||       y r[   r\   rk   s       r   r.   zNoOpGauge.__init__  rl   r   r_   r`   ra   c                 (    t         |   |||      S rn   )rM   r   rp   s       r   r   zNoOpGauge.set  rq   r   r6   re   )r   r   r   r!   r7   r.   rg   r   r   r   r   rS   rT   s   @r   r   r     rs   r   r   c            	       L    e Zd Z	 	 d
deez  dedz  dedz  ddfdZdddefd	Z	y)_ProxyGaugeNr_   r`   ra   r)   c                 X    | j                   r| j                   j                  |||       y y r+   )r@   r   rc   s       r   r   z_ProxyGauge.set  rx   r   rA   rB   c                 d    |j                  | j                  | j                  | j                        S r+   )create_gauger=   r>   r?   rE   s     r   rD   z#_ProxyGauge._create_real_instrument&  s,    !!JJJJ
 	
r   re   )
r   r   r   rg   r   r   r   r   r   rD   r   r   r   r   r     sZ     )-"&	CeC %C 4	C
 
C
_ 
 
r   r   )<abcr   r   collections.abcr   r   r   r   dataclassesr	   loggingr
   rer   
re_compiletypingr   r   opentelemetryr   opentelemetry.contextr   +opentelemetry.metrics._internal.observationr   opentelemetry.util.typesr   r   _loggerr1   r3   r   r   r"   rR   r#   r;   rJ   rV   rY   r^   ri   ru   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>r      sb   $ C C !  $ " ) C H
:;-. $> > > $# # # m<8 o 556%<=> 
- -`IsGK0 I2	$#3K#@ 	$5* 5C: Ck (K' K(
$W-w 
$K 0K K(
*=9= 
$ 
- 
$
 !235F
l 
"9 
$
 !89
$ $NNI N4
&y19 
>l 
/ 
$
 1
K 0K K(
U	
r   