
    鍱i8B                        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       G d d             Z  ed       G d d             Z! edd      Z"eee!gee   f   eee   e!df   f   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/      Z0 G d/ d0e&e/   e/      Z1 G d1 d2e(      Z2 G d3 d4e2      Z3 G d5 d6e&e2   e2      Z4 G d7 d8e'      Z5 G d9 d:e5      Z6 G d; d<e%e5   e5      Z7 G d= d>e(      Z8 G d? d@e8      Z9 G dA dBe&e8   e8      Z: G dC dDe'      Z; G dE dFe;      Z< G dG dHe%e;   e;      Z=y)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                   (    e Zd ZU dZeee      ed<   y)_MetricsHistogramAdvisoryNexplicit_bucket_boundaries)__name__
__module____qualname__r   r   r   float__annotations__     r/home/jang/Projects/ai-researcher/.venv/lib/python3.12/site-packages/opentelemetry/metrics/_internal/instrument.pyr   r   0   s    <@% 9@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"   5   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	e   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__L   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_descriptionU   s     ,.  &2!F6N!F6N<D  &2!F6N!F6N$&F=!  %0F=!r   r3   r3   )r   r   r   r$   r   strr1   staticmethodr	   r   r8   r   r   r    r&   r&   I   s    A 	  	
 
     +. 	c8C= 	!   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    r1   z_ProxyInstrument.__init__z   s"     

'7;r   metermetrics.Meterc                 0    | j                  |      | _        y)z;Called when a real meter is set on the creating _ProxyMeterN)_create_real_instrumentrB   r0   rC   s     r    on_meter_setz_ProxyInstrument.on_meter_set   s     !% < <U Cr   c                      y)z:Create an instance of the real instrument. Implement this.Nr   rG   s     r    rF   z(_ProxyInstrument._create_real_instrument       r   r9   )rC   rD   r,   N)	r   r   r   r:   r1   rH   r   r%   rF   r   r   r    r=   r=   y   sa     		<	< 	< 		<
 
	<D I_ I I Ir   r=   c                   H     e Zd Z	 	 	 ddedeee      dededdf
 fdZ xZS )	_ProxyAsynchronousInstrumentNr)   	callbacksr*   r+   r,   c                 6    t         |   |||       || _        y r.   )superr1   
_callbacksr0   r)   rM   r*   r+   	__class__s        r    r1   z%_ProxyAsynchronousInstrument.__init__   s     	t[1#r   Nr3   r3   )	r   r   r   r:   r   r   	CallbackTr1   __classcell__rR   s   @r    rL   rL      sR     48$$ HY/0$ 	$
 $ 
$ $r   rL   c                       e Zd ZdZy)Synchronousz*Base class for all synchronous instrumentsNr   r   r   r$   r   r   r    rX   rX      s    4r   rX   c                   V     e Zd ZdZe	 	 	 d	dedeee      dededdf
 fd       Z	 xZ
S )
Asynchronousz+Base class for all asynchronous instrumentsNr)   rM   r*   r+   r,   c                 *    t         |   |||       y N)r*   r+   rO   r1   rQ   s        r    r1   zAsynchronous.__init__   s     	DkBr   rS   )r   r   r   r$   r   r:   r   r   rT   r1   rU   rV   s   @r    r[   r[      sd    5 48CC HY/0C 	C
 C 
C Cr   r[   c            
       N    e Zd ZdZe	 	 ddeeef   dee	   dee
   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   r0   ra   rb   rc   s       r    addzCounter.add   rJ   r   NNr   r   r   r$   r   r   intr   r   r   r   rf   r   r   r    r`   r`      sX    Y ,0%)	c5j! Z( '"	
 
 r   r`   c            	       r     e Zd ZdZ	 	 ddedededdf fdZ	 	 ddeeef   d	e	e
   d
e	e   ddf fdZ xZS )NoOpCounterz"No-op implementation of `Counter`.r)   r*   r+   r,   Nc                 *    t         |   |||       y r]   r^   r0   r)   r*   r+   rR   s       r    r1   zNoOpCounter.__init__        	DkBr   ra   rb   rc   c                 (    t         |   |||      S N)rb   rc   rO   rf   r0   ra   rb   rc   rR   s       r    rf   zNoOpCounter.add        w{6j'{JJr   r9   rg   r   r   r   r$   r:   r1   r   ri   r   r   r   r   rf   rU   rV   s   @r    rk   rk          ,
 	CC C 	C
 
C ,0%)	Kc5j!K Z(K '"	K
 
K Kr   rk   c            	       P    e Zd Z	 	 d
deeef   dee   dee   ddfdZ	ddde
fd	Zy)_ProxyCounterNra   rb   rc   r,   c                 X    | j                   r| j                   j                  |||       y y r.   rB   rf   re   s       r    rf   z_ProxyCounter.add   *       !!%%fj'B !r   rC   rD   c                 d    |j                  | j                  | j                  | j                        S r.   )create_counterr?   r@   rA   rG   s     r    rF   z%_ProxyCounter._create_real_instrument   s,    ##JJJJ
 	
r   rg   )r   r   r   r   ri   r   r   r   r   rf   r`   rF   r   r   r    rw   rw      s`     ,0%)	Cc5j!C Z(C '"	C
 
C
_ 
 
r   rw   c            
       N    e Zd ZdZe	 	 ddeeef   dee	   dee
   ddfd       Zy)	UpDownCounterzXAn UpDownCounter is a synchronous `Instrument` which supports increments and decrements.Nra   rb   rc   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   re   s       r    rf   zUpDownCounter.add   rJ   r   rg   rh   r   r   r    r~   r~      sX    b ,0%)	c5j! Z( '"	
 
 r   r~   c            	       r     e Zd ZdZ	 	 ddedededdf fdZ	 	 ddeeef   d	e	e
   d
e	e   ddf fdZ xZS )NoOpUpDownCounterz(No-op implementation of `UpDownCounter`.r)   r*   r+   r,   Nc                 *    t         |   |||       y r]   r^   rm   s       r    r1   zNoOpUpDownCounter.__init__  rn   r   ra   rb   rc   c                 (    t         |   |||      S rp   rq   rr   s       r    rf   zNoOpUpDownCounter.add  rs   r   r9   rg   rt   rV   s   @r    r   r     s    2
 	CC C 	C
 
C ,0%)	Kc5j!K Z(K '"	K
 
K Kr   r   c            	       P    e Zd Z	 	 d
deeef   dee   dee   ddfdZ	ddde
fd	Zy)_ProxyUpDownCounterNra   rb   rc   r,   c                 X    | j                   r| j                   j                  |||       y y r.   ry   re   s       r    rf   z_ProxyUpDownCounter.add  rz   r   rC   rD   c                 d    |j                  | j                  | j                  | j                        S r.   )create_up_down_counterr?   r@   rA   rG   s     r    rF   z+_ProxyUpDownCounter._create_real_instrument   s,    ++JJJJ
 	
r   rg   )r   r   r   r   ri   r   r   r   r   rf   r~   rF   r   r   r    r   r     s`     ,0%)	Cc5j!C Z(C '"	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.
    NrY   r   r   r    r   r   (  s    r   r   c                   L     e Zd ZdZ	 	 	 d	dedeee      dededdf
 fdZ xZ	S )
NoOpObservableCounterz,No-op implementation of `ObservableCounter`.Nr)   rM   r*   r+   r,   c                 ,    t         |   ||||       y r]   r^   rQ   s        r    r1   zNoOpObservableCounter.__init__1  #     	#	 	 	
r   rS   
r   r   r   r$   r:   r   r   rT   r1   rU   rV   s   @r    r   r   .  sU    6
 48

 HY/0
 	

 
 

 
r   r   c                       e Zd ZdddefdZy)_ProxyObservableCounterrC   rD   r,   c                 z    |j                  | j                  | j                  | j                  | j                        S r.   )create_observable_counterr?   rP   r@   rA   rG   s     r    rF   z/_ProxyObservableCounter._create_real_instrumentC  s5     ..JJOOJJ	
 	
r   N)r   r   r   r   rF   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.
    NrY   r   r   r    r   r   N      r   r   c                   L     e Zd ZdZ	 	 	 d	dedeee      dededdf
 fdZ xZ	S )
NoOpObservableUpDownCounterz2No-op implementation of `ObservableUpDownCounter`.Nr)   rM   r*   r+   r,   c                 ,    t         |   ||||       y r]   r^   rQ   s        r    r1   z$NoOpObservableUpDownCounter.__init__X  r   r   rS   r   rV   s   @r    r   r   U  sU    <
 48

 HY/0
 	

 
 

 
r   r   c                       e Zd ZdddefdZy)_ProxyObservableUpDownCounterrC   rD   r,   c                 z    |j                  | j                  | j                  | j                  | j                        S r.   )!create_observable_up_down_counterr?   rP   r@   rA   rG   s     r    rF   z5_ProxyObservableUpDownCounter._create_real_instrumentk  s5     66JJOOJJ	
 	
r   N)r   r   r   r   rF   r   r   r    r   r   g  s    
$
	 
r   r   c                       e Zd ZdZe	 	 	 ddedededeee      ddf
d       Z	e	 	 dd	e
eef   d
ee   dee   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   )r0   r)   r*   r+   r   s        r    r1   zHistogram.__init__|  s     	r   ra   rb   rc   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   re   s       r    recordzHistogram.record  rJ   r   r3   r3   Nrg   )r   r   r   r$   r   r:   r   r   r   r1   r   ri   r   r   r   r   r   r    r   r   v  s    
  IM  	
 .6huo-F 
   ,0%)	c5j! Z( '"	
 
 r   r   c                        e Zd ZdZ	 	 	 ddedededeee      ddf
 fdZ	 	 dd	e	e
ef   d
ee   dee   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^   r0   r)   r*   r+   r   rR   s        r    r1   zNoOpHistogram.__init__  s#     	#0S	 	 	
r   ra   rb   rc   c                 (    t         |   |||      S rp   )rO   r   rr   s       r    r   zNoOpHistogram.record  s     w~fW~MMr   r   rg   )r   r   r   r$   r:   r   r   r   r1   r   ri   r   r   r   rU   rV   s   @r    r   r     s    .
 IM

 
 	

 .6huo-F
 

" ,0%)	Nc5j!N Z(N '"	N
 
N Nr   r   c                        e Zd Z	 	 	 ddedededeee      ddf
 fdZ	 	 ddee	ef   d	ee
   d
ee   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]   )rO   r1   $_explicit_bucket_boundaries_advisoryr   s        r    r1   z_ProxyHistogram.__init__  s$     	DkB/ 	1r   ra   rb   rc   c                 X    | j                   r| j                   j                  |||       y y r.   )rB   r   re   s       r    r   z_ProxyHistogram.record  s*       !!((WE !r   rC   rD   c                 |    |j                  | j                  | j                  | j                  | j                        S )N)r   )create_histogramr?   r@   rA   r   rG   s     r    rF   z'_ProxyHistogram._create_real_instrument  s:    %%JJJJ040Y0Y	 & 
 	
r   r   rg   )r   r   r   r:   r   r   r   r1   r   ri   r   r   r   r   rF   rU   rV   s   @r    r   r     s     IM



 

 	


 .6huo-F

 


 ,0%)	Fc5j!F Z(F '"	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.
    NrY   r   r   r    r   r     r   r   r   c                   L     e Zd ZdZ	 	 	 d	dedeee      dededdf
 fdZ xZ	S )
NoOpObservableGaugez*No-op implementation of `ObservableGauge`.Nr)   rM   r*   r+   r,   c                 ,    t         |   ||||       y r]   r^   rQ   s        r    r1   zNoOpObservableGauge.__init__  r   r   rS   r   rV   s   @r    r   r     sU    4
 48

 HY/0
 	

 
 

 
r   r   c                       e Zd ZdddefdZy)_ProxyObservableGaugerC   rD   r,   c                 z    |j                  | j                  | j                  | j                  | j                        S r.   )create_observable_gauger?   rP   r@   rA   rG   s     r    rF   z-_ProxyObservableGauge._create_real_instrument  s5     ,,JJOOJJ	
 	
r   N)r   r   r   r   rF   r   r   r    r   r     s    
$
	
r   r   c            
       N    e Zd ZdZe	 	 ddeeef   dee	   dee
   ddfd       Zy)	GaugezdA Gauge is a synchronous `Instrument` which can be used to record non-additive values as they occur.Nra   rb   rc   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   re   s       r    setz	Gauge.set  rJ   r   rg   )r   r   r   r$   r   r   ri   r   r   r   r   r   r   r   r    r   r     sX    n ,0%)	c5j! Z( '"	
 
 r   r   c            	       r     e Zd ZdZ	 	 ddedededdf fdZ	 	 ddeeef   d	e	e
   d
e	e   ddf fdZ xZS )	NoOpGaugez"No-op implementation of ``Gauge``.r)   r*   r+   r,   Nc                 *    t         |   |||       y r]   r^   rm   s       r    r1   zNoOpGauge.__init__  rn   r   ra   rb   rc   c                 (    t         |   |||      S rp   )rO   r   rr   s       r    r   zNoOpGauge.set!  rs   r   r9   rg   )r   r   r   r$   r:   r1   r   ri   r   r   r   r   r   rU   rV   s   @r    r   r     ru   r   r   c            	       P    e Zd Z	 	 d
deeef   dee   dee   ddfdZ	ddde
fd	Zy)_ProxyGaugeNra   rb   rc   r,   c                 X    | j                   r| j                   j                  |||       y y r.   )rB   r   re   s       r    r   z_ProxyGauge.set.  rz   r   rC   rD   c                 d    |j                  | j                  | j                  | j                        S r.   )create_gauger?   r@   rA   rG   s     r    rF   z#_ProxyGauge._create_real_instrument7  s,    !!JJJJ
 	
r   rg   )r   r   r   r   ri   r   r   r   r   r   r   rF   r   r   r    r   r   *  s`     ,0%)	Cc5j!C Z(C '"	C
 
C
_ 
 
r   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   _loggerr4   r6   r   r"   r%   rT   r&   r=   rL   rX   r[   r`   rk   rw   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    <module>r      sl  $ $ !  $
 
 
 " ) C H
:;-. $A A A $# # # m<8o 556h{#_d:;=	- -`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   