
    go                     6   U d Z ddl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mZmZmZmZmZ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mZm Z m!Z!m"Z"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l0m1Z1  ee2      Z3ee%e'e&e(e)e*e+f   Z4 G d de      Z5 G d de5      Z6 G d de5      Z7 G d de      Z8 G d de8      Z9 G d de8      Z: e-       Z;da<ee5   e=d<    e7       Z>	 	 	 d$de?de?dee5   dee?   ddf
dZ@de5d eAddfd!ZBde5ddfd"ZCde5fd#ZDy)%a  
The OpenTelemetry metrics API  describes the classes used to generate
metrics.

The :class:`.MeterProvider` provides users access to the :class:`.Meter` which in
turn is used to create :class:`.Instrument` objects. The :class:`.Instrument` objects are
used to record measurements.

This module provides abstract (i.e. unimplemented) classes required for
metrics, and a concrete no-op implementation :class:`.NoOpMeter` that allows applications
to use the API package alone without a supporting implementation.

To get a meter, you need to provide the package name from which you are
calling the meter APIs to OpenTelemetry by calling `MeterProvider.get_meter`
with the calling instrumentation name and the version of your package.

The following code shows how to obtain a meter using the global :class:`.MeterProvider`::

    from opentelemetry.metrics import get_meter

    meter = get_meter("example-meter")
    counter = meter.create_counter("example-counter")

.. versionadded:: 1.10.0
    N)ABCabstractmethod)	getLogger)environ)Lock)ListOptionalSequenceSetTupleUnioncast)OTEL_PYTHON_METER_PROVIDER)	CallbackTCounterGauge	HistogramNoOpCounter	NoOpGaugeNoOpHistogramNoOpObservableCounterNoOpObservableGaugeNoOpObservableUpDownCounterNoOpUpDownCounterObservableCounterObservableGaugeObservableUpDownCounterUpDownCounter_ProxyCounter_ProxyGauge_ProxyHistogram_ProxyObservableCounter_ProxyObservableGauge_ProxyObservableUpDownCounter_ProxyUpDownCounter)Once)_load_provider)
Attributesc                   P    e Zd ZdZe	 	 	 d
dedee   dee   dee   ddf
d	       Zy)MeterProviderz_
    MeterProvider is the entry point of the API. It provides access to `Meter` instances.
    Nnameversion
schema_url
attributesreturnMeterc                      y)aD  Returns a `Meter` for use by the given instrumentation library.

        For any two calls it is undefined whether the same or different
        `Meter` instances are returned, even for different library names.

        This function may return different `Meter` types (e.g. a no-op meter
        vs. a functional meter).

        Args:
            name: The name of the instrumenting module.
                ``__name__`` may not be used as this can result in
                different meter names if the meters are in different files.
                It is better to use a fixed string that can be imported where
                needed and used consistently as the name of the meter.

                This should *not* be the name of the module that is
                instrumented but the name of the module doing the instrumentation.
                E.g., instead of ``"requests"``, use
                ``"opentelemetry.instrumentation.requests"``.

            version: Optional. The version string of the
                instrumenting library.  Usually this should be the same as
                ``importlib.metadata.version(instrumenting_library_name)``.

            schema_url: Optional. Specifies the Schema URL of the emitted telemetry.
            attributes: Optional. Attributes that are associated with the emitted telemetry.
        N selfr+   r,   r-   r.   s        ]/var/www/openai/venv/lib/python3.12/site-packages/opentelemetry/metrics/_internal/__init__.py	get_meterzMeterProvider.get_meterc           NNN)	__name__
__module____qualname____doc__r   strr	   r(   r6   r2   r8   r5   r*   r*   ^   sc      "&$(+/!! #! SM	!
 Z(! 
! !r8   r*   c                   F    e Zd ZdZ	 	 	 d
dedee   dee   dee   ddf
d	Zy)NoOpMeterProviderzQThe default MeterProvider used when no MeterProvider implementation is available.Nr+   r,   r-   r.   r/   r0   c                     t        |||      S )zReturns a NoOpMeter.r,   r-   )	NoOpMeterr3   s        r5   r6   zNoOpMeterProvider.get_meter   s     w:FFr8   r9   )r:   r;   r<   r=   r>   r	   r(   r6   r2   r8   r5   r@   r@      sX    [
 "&$(+/GG #G SM	G
 Z(G 
Gr8   r@   c                   Z    e Zd ZddZ	 	 	 ddedee   dee   dee   ddf
d	Zd
eddfdZ	y)_ProxyMeterProviderr/   Nc                 >    t               | _        g | _        d | _        y N)r   _lock_meters_real_meter_providerr4   s    r5   __init__z_ProxyMeterProvider.__init__   s    V
*,=A!r8   r+   r,   r-   r.   r0   c                     | j                   5  | j                  &| j                  j                  |||      cd d d        S t        |||      }| j                  j                  |       |cd d d        S # 1 sw Y   y xY wNrB   )rH   rJ   r6   _ProxyMeterrI   append)r4   r+   r,   r-   r.   meters         r5   r6   z_ProxyMeterProvider.get_meter   sh     ZZ((400::': Z  g*MELL& ZZs   )A4 *A44A=meter_providerc                     | j                   5  || _        | j                  D ]  }|j                  |        	 d d d        y # 1 sw Y   y xY wrG   )rH   rJ   rI   on_set_meter_provider)r4   rR   rQ   s      r5   rT   z)_ProxyMeterProvider.on_set_meter_provider   s7    ZZ(6D%++N; & ZZs   *AA
r/   Nr9   )
r:   r;   r<   rL   r>   r	   r(   r6   r*   rT   r2   r8   r5   rE   rE      sh    B "&$(+/ # SM	
 Z( 
"<M <d <r8   rE   c                       e Zd ZdZ	 	 ddedee   dee   ddf fdZedefd       Zedee   fd	       Z	edee   fd
       Z
dededededeeef   f
dZe	 	 ddedededefd       Ze	 	 ddedededefd       Ze	 	 	 ddedeee      dededef
d       Ze	 	 ddedededefd       Z	 	 ddedededefdZe	 	 	 ddedeee      dededef
d       Ze	 	 	 ddedeee      dededef
d       Z xZ S )r0   zHandles instrument creation.

    This class provides methods for creating instruments which are then
    used to produce measurements.
    Nr+   r,   r-   r/   c                     t         |           || _        || _        || _        t               | _        t               | _        y rG   )	superrL   _name_version_schema_urlset_instrument_idsr   _instrument_ids_lockr4   r+   r,   r-   	__class__s       r5   rL   zMeter.__init__   s:     	
%),$(F!r8   c                     | j                   S )z7
        The name of the instrumenting module.
        )rY   rK   s    r5   r+   z
Meter.name   s    
 zzr8   c                     | j                   S )zB
        The version string of the instrumenting library.
        )rZ   rK   s    r5   r,   zMeter.version   s    
 }}r8   c                     | j                   S )zC
        Specifies the Schema URL of the emitted telemetry
        )r[   rK   s    r5   r-   zMeter.schema_url   s    
 r8   type_unitdescriptionc                 $   dj                  |j                         j                         |j                  ||g      }d}| j                  5  || j
                  v rd}n| j
                  j                  |       ddd       ||fS # 1 sw Y   ||fS xY w)a)  
        Check if an instrument with the same name, type, unit and description
        has been registered already.

        Returns a tuple. The first value is `True` if the instrument has been
        registered already, `False` otherwise. The second value is the
        instrument id.
        ,FTN)joinstriplowerr:   r^   r]   add)r4   r+   rd   re   rf   instrument_idresults          r5   _is_instrument_registeredzMeter._is_instrument_registered   s     ZZ\!5>>4E
 && 4 44$$((7	 ' && ' &&s   
-BBc                      y)aR  Creates a `Counter` instrument

        Args:
            name: The name of the instrument to be created
            unit: The unit for observations this instrument reports. For
                example, ``By`` for bytes. UCUM units are recommended.
            description: A description for this instrument and what it measures.
        Nr2   r4   r+   re   rf   s       r5   create_counterzMeter.create_counter   r7   r8   c                      y)aY  Creates an `UpDownCounter` instrument

        Args:
            name: The name of the instrument to be created
            unit: The unit for observations this instrument reports. For
                example, ``By`` for bytes. UCUM units are recommended.
            description: A description for this instrument and what it measures.
        Nr2   rq   s       r5   create_up_down_counterzMeter.create_up_down_counter  r7   r8   	callbacksc                      y)a  Creates an `ObservableCounter` instrument

        An observable counter observes a monotonically increasing count by calling provided
        callbacks which accept a :class:`~opentelemetry.metrics.CallbackOptions` and return
        multiple :class:`~opentelemetry.metrics.Observation`.

        For example, an observable counter could be used to report system CPU
        time periodically. Here is a basic implementation::

            def cpu_time_callback(options: CallbackOptions) -> Iterable[Observation]:
                observations = []
                with open("/proc/stat") as procstat:
                    procstat.readline()  # skip the first line
                    for line in procstat:
                        if not line.startswith("cpu"): break
                        cpu, *states = line.split()
                        observations.append(Observation(int(states[0]) // 100, {"cpu": cpu, "state": "user"}))
                        observations.append(Observation(int(states[1]) // 100, {"cpu": cpu, "state": "nice"}))
                        observations.append(Observation(int(states[2]) // 100, {"cpu": cpu, "state": "system"}))
                        # ... other states
                return observations

            meter.create_observable_counter(
                "system.cpu.time",
                callbacks=[cpu_time_callback],
                unit="s",
                description="CPU time"
            )

        To reduce memory usage, you can use generator callbacks instead of
        building the full list::

            def cpu_time_callback(options: CallbackOptions) -> Iterable[Observation]:
                with open("/proc/stat") as procstat:
                    procstat.readline()  # skip the first line
                    for line in procstat:
                        if not line.startswith("cpu"): break
                        cpu, *states = line.split()
                        yield Observation(int(states[0]) // 100, {"cpu": cpu, "state": "user"})
                        yield Observation(int(states[1]) // 100, {"cpu": cpu, "state": "nice"})
                        # ... other states

        Alternatively, you can pass a sequence of generators directly instead of a sequence of
        callbacks, which each should return iterables of :class:`~opentelemetry.metrics.Observation`::

            def cpu_time_callback(states_to_include: set[str]) -> Iterable[Iterable[Observation]]:
                # accept options sent in from OpenTelemetry
                options = yield
                while True:
                    observations = []
                    with open("/proc/stat") as procstat:
                        procstat.readline()  # skip the first line
                        for line in procstat:
                            if not line.startswith("cpu"): break
                            cpu, *states = line.split()
                            if "user" in states_to_include:
                                observations.append(Observation(int(states[0]) // 100, {"cpu": cpu, "state": "user"}))
                            if "nice" in states_to_include:
                                observations.append(Observation(int(states[1]) // 100, {"cpu": cpu, "state": "nice"}))
                            # ... other states
                    # yield the observations and receive the options for next iteration
                    options = yield observations

            meter.create_observable_counter(
                "system.cpu.time",
                callbacks=[cpu_time_callback({"user", "system"})],
                unit="s",
                description="CPU time"
            )

        The :class:`~opentelemetry.metrics.CallbackOptions` contain a timeout which the
        callback should respect. For example if the callback does asynchronous work, like
        making HTTP requests, it should respect the timeout::

            def scrape_http_callback(options: CallbackOptions) -> Iterable[Observation]:
                r = requests.get('http://scrapethis.com', timeout=options.timeout_millis / 10**3)
                for value in r.json():
                    yield Observation(value)

        Args:
            name: The name of the instrument to be created
            callbacks: A sequence of callbacks that return an iterable of
                :class:`~opentelemetry.metrics.Observation`. Alternatively, can be a sequence of generators that each
                yields iterables of :class:`~opentelemetry.metrics.Observation`.
            unit: The unit for observations this instrument reports. For
                example, ``By`` for bytes. UCUM units are recommended.
            description: A description for this instrument and what it measures.
        Nr2   r4   r+   ru   re   rf   s        r5   create_observable_counterzMeter.create_observable_counter  r7   r8   c                      y)ar  Creates a :class:`~opentelemetry.metrics.Histogram` instrument

        Args:
            name: The name of the instrument to be created
            unit: The unit for observations this instrument reports. For
                example, ``By`` for bytes. UCUM units are recommended.
            description: A description for this instrument and what it measures.
        Nr2   rq   s       r5   create_histogramzMeter.create_histogramx  r7   r8   c                 .    t        j                  d       y)aR  Creates a ``Gauge`` instrument

        Args:
            name: The name of the instrument to be created
            unit: The unit for observations this instrument reports. For
                example, ``By`` for bytes. UCUM units are recommended.
            description: A description for this instrument and what it measures.
        z5create_gauge() is not implemented and will be a no-opN)warningswarnrq   s       r5   create_gaugezMeter.create_gauge  s     	MNr8   c                      y)aZ  Creates an `ObservableGauge` instrument

        Args:
            name: The name of the instrument to be created
            callbacks: A sequence of callbacks that return an iterable of
                :class:`~opentelemetry.metrics.Observation`. Alternatively, can be a generator that yields iterables
                of :class:`~opentelemetry.metrics.Observation`.
            unit: The unit for observations this instrument reports. For
                example, ``By`` for bytes. UCUM units are recommended.
            description: A description for this instrument and what it measures.
        Nr2   rw   s        r5   create_observable_gaugezMeter.create_observable_gauge  r7   r8   c                      y)ab  Creates an `ObservableUpDownCounter` instrument

        Args:
            name: The name of the instrument to be created
            callbacks: A sequence of callbacks that return an iterable of
                :class:`~opentelemetry.metrics.Observation`. Alternatively, can be a generator that yields iterables
                of :class:`~opentelemetry.metrics.Observation`.
            unit: The unit for observations this instrument reports. For
                example, ``By`` for bytes. UCUM units are recommended.
            description: A description for this instrument and what it measures.
        Nr2   rw   s        r5   !create_observable_up_down_counterz'Meter.create_observable_up_down_counter  r7   r8   NN r   Nr   r   )!r:   r;   r<   r=   r>   r	   rL   propertyr+   r,   r-   typer   boolro   r   r   rr   r   rt   r
   r   r   rx   r   rz   r   r~   r   r   r   r   __classcell__r`   s   @r5   r0   r0      s    "&$(	++ #+ SM	+
 
+ c   #    HSM    '' $',/'>A'	tSy	'4  	  	
 
   	  	
 
   48^^ HY/0^ 	^
 ^ 
^ ^@  	  	
 
 $ 	OO O 	O
 
O   48 HY/0 	
  
 &  48 HY/0 	
  
! r8   r0   c                   Z    e Zd Z	 	 ddedee   dee   ddf fdZdeddfdZ	 	 dded	ed
edefdZ		 	 dded	ed
ede
fdZ	 	 	 ddedeee      d	ed
edef
dZ	 	 dded	ed
edefdZ	 	 dded	ed
edefdZ	 	 	 ddedeee      d	ed
edef
dZ	 	 	 ddedeee      d	ed
edef
dZ xZS )rO   Nr+   r,   r-   r/   c                 d    t         |   |||       t               | _        g | _        d | _        y rN   )rX   rL   r   rH   _instruments_real_meterr_   s       r5   rL   z_ProxyMeter.__init__  s2     	w:FV
57,0r8   rR   c                     |j                  | j                  | j                  | j                        }| j                  5  || _        | j                  D ]  }|j                  |        	 ddd       y# 1 sw Y   yxY w)zCalled when a real meter provider is set on the creating _ProxyMeterProvider

        Creates a real backing meter for this instance and notifies all created
        instruments so they can create real backing instruments.
        N)r6   rY   rZ   r[   rH   r   r   on_meter_set)r4   rR   
real_meter
instruments       r5   rT   z!_ProxyMeter.on_set_meter_provider  sd     $--JJt'7'7

 ZZ)D #//
''
3 0	 ZZs   *A22A;re   rf   c                     | j                   5  | j                  r&| j                  j                  |||      cd d d        S t        |||      }| j                  j                  |       |cd d d        S # 1 sw Y   y xY wrG   )rH   r   rr   r   r   rP   r4   r+   re   rf   proxys        r5   rr   z_ProxyMeter.create_counter  sd     ZZ''66tT;O Z "$k:E$$U+ ZZ   )A3 )A33A<c                     | j                   5  | j                  r&| j                  j                  |||      cd d d        S t        |||      }| j                  j                  |       |cd d d        S # 1 sw Y   y xY wrG   )rH   r   rt   r%   r   rP   r   s        r5   rt   z"_ProxyMeter.create_up_down_counter  sg     ZZ''>>$ Z
 (dK@E$$U+ ZZr   ru   c                    | j                   5  | j                  r'| j                  j                  ||||      cd d d        S t        ||||      }| j                  j                  |       |cd d d        S # 1 sw Y   y xY wNre   rf   )rH   r   rx   r"   r   rP   r4   r+   ru   re   rf   r   s         r5   rx   z%_ProxyMeter.create_observable_counter  sp     ZZ''AA)T; Z
 ,idE $$U+ ZZ   *A6+A66A?c                     | j                   5  | j                  r&| j                  j                  |||      cd d d        S t        |||      }| j                  j                  |       |cd d d        S # 1 sw Y   y xY wrG   )rH   r   rz   r!   r   rP   r   s        r5   rz   z_ProxyMeter.create_histogram  sg     ZZ''88$ Z
 $D$<E$$U+ ZZr   c                     | j                   5  | j                  r&| j                  j                  |||      cd d d        S t        |||      }| j                  j                  |       |cd d d        S # 1 sw Y   y xY wrG   )rH   r   r~   r    r   rP   r   s        r5   r~   z_ProxyMeter.create_gauge  sd     ZZ''44T4M Z  dK8E$$U+ ZZr   c                    | j                   5  | j                  r'| j                  j                  ||||      cd d d        S t        ||||      }| j                  j                  |       |cd d d        S # 1 sw Y   y xY wr   )rH   r   r   r#   r   rP   r   s         r5   r   z#_ProxyMeter.create_observable_gauge(  sp     ZZ''??)T; Z
 *idE $$U+ ZZr   c                    | j                   5  | j                  r'| j                  j                  ||||      cd d d        S t        ||||      }| j                  j                  |       |cd d d        S # 1 sw Y   y xY wr   )rH   r   r   r$   r   rP   r   s         r5   r   z-_ProxyMeter.create_observable_up_down_counter:  ss     ZZ''II	 Z 2idE $$U+ ZZr   r   r   r   )r:   r;   r<   r>   r	   rL   r*   rT   r   rr   r   rt   r
   r   r   rx   r   rz   r   r~   r   r   r   r   r   r   s   @r5   rO   rO     s    "&$(		1	1 #	1 SM		1
 
	14M 4d 4( 	  	
 
  	  	
 
$ 48 HY/0 	
  
* 	  	
 
$ 	  	
 
  48 HY/0 	
  
* 48 HY/0 	
  
!r8   rO   c                      e Zd ZdZ	 	 ddedededefdZ	 	 ddedededefdZ	 	 ddededede	fdZ
	 	 	 dded
eee      dededef
dZ	 	 ddedededefdZ	 	 	 dded
eee      dededef
dZ	 	 	 dded
eee      dededef
dZy	)rC   zeThe default Meter used when no Meter implementation is available.

    All operations are no-op.
    r+   re   rf   r/   c                     | j                  |t        ||      d   r't        j                  d|t        j
                  ||       t        |||      S )zReturns a no-op Counter.r   YAn instrument with name %s, type %s, unit %s and description %s has been created already.r   )ro   r   _loggerwarningr   r:   rq   s       r5   rr   zNoOpMeter.create_counterV  sY     ))+t[

 OO;   4dDDr8   c                     | j                  |t        ||      d   r't        j                  d|t        j
                  ||       t        |||      S )zReturns a no-op Gauge.r   r   r   )ro   r   r   r   r   r:   rq   s       r5   r~   zNoOpMeter.create_gaugej  sT     ))$	4M
 OO; DkBBr8   c                     | j                  |t        ||      d   r't        j                  d|t        j
                  ||       t        |||      S )zReturns a no-op UpDownCounter.r   r   r   )ro   r   r   r   r   r:   rq   s       r5   rt   z NoOpMeter.create_up_down_counter~  sZ     ))#T;

 OO;&& !DkJJr8   Nru   c                     | j                  |t        ||      d   r't        j                  d|t        j
                  ||       t        ||||      S )z"Returns a no-op ObservableCounter.r   r   r   )ro   r   r   r   r   r:   rw   s        r5   rx   z#NoOpMeter.create_observable_counter  sd     ))'{

 OO;!** %#	
 	
r8   c                     | j                  |t        ||      d   r't        j                  d|t        j
                  ||       t        |||      S )zReturns a no-op Histogram.r   r   r   )ro   r   r   r   r   r:   rq   s       r5   rz   zNoOpMeter.create_histogram  sY     ))-{

 OO;"" T+FFr8   c                     | j                  |t        ||      d   r't        j                  d|t        j
                  ||       t        ||||      S )z Returns a no-op ObservableGauge.r   r   r   )ro   r   r   r   r   r:   rw   s        r5   r   z!NoOpMeter.create_observable_gauge  sd     ))%t[

 OO;(( ##	
 	
r8   c                     | j                  |t        ||      d   r't        j                  d|t        j
                  ||       t        ||||      S )z(Returns a no-op ObservableUpDownCounter.r   r   r   )ro   r   r   r   r   r:   rw   s        r5   r   z+NoOpMeter.create_observable_up_down_counter  sd     ))-t[

 OO;'00 +#	
 	
r8   r   r   )r:   r;   r<   r=   r>   r   rr   r   r~   r   rt   r	   r
   r   r   rx   r   rz   r   r   r   r   r2   r8   r5   rC   rC   P  s    	EE E 	E
 
E. 	CC C 	C
 
C. 	KK K 	K
 
K. 48

 HY/0
 	

 
 

: 	GG G 	G
 
G. 48

 HY/0
 	

 
 

: 48

 HY/0
 	

 
 
!
r8   rC   _METER_PROVIDERr+   r,   rR   r-   r/   c                 @    |
t               }|j                  | ||      S )zReturns a `Meter` for use by the given instrumentation library.

    This function is a convenience wrapper for
    `opentelemetry.metrics.MeterProvider.get_meter`.

    If meter_provider is omitted the current configured one is used.
    )get_meter_providerr6   )r+   r,   rR   r-   s       r5   r6   r6     s'     +-##D':>>r8   logc                 r     d fd}t         j                  |      }|r|st        j                  d       y y y )Nc                  4     a t        j                          y rG   )r   _PROXY_METER_PROVIDERrT   rR   s   r5   set_mpz#_set_meter_provider.<locals>.set_mp  s    ( 	33NCr8   z2Overriding of current MeterProvider is not allowedrU   )_METER_PROVIDER_SET_ONCEdo_oncer   r   )rR   r   r   did_sets   `   r5   _set_meter_providerr     s4    D '..v6G
7LM sr8   c                     t        | d       y)zSets the current global :class:`~.MeterProvider` object.

    This can only be done once, a warning will be logged if any further attempt
    is made.
    Tr   N)r   r   s    r5   set_meter_providerr     s     D1r8   c                      t         /t        t        vrt        S t	        t        d      } t        | d       t        dt               S )z8Gets the current global :class:`~.MeterProvider` object.rR   Fr   r*   )r   r   r   r   r'   r   r   r   s    r5   r   r   #  sC     %W4(((6&(8)
 	N6 11r8   )r   NN)Er=   r|   abcr   r   loggingr   osr   	threadingr   typingr   r	   r
   r   r   r   r   #opentelemetry.environment_variablesr   *opentelemetry.metrics._internal.instrumentr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   opentelemetry.util._oncer&   opentelemetry.util._providersr'   opentelemetry.util.typesr(   r:   r   _ProxyInstrumentTr*   r@   rE   r0   rO   rC   r   r   __annotations__r   r>   r6   r   r   r   r   r2   r8   r5   <module>r      s{  "4  #    D D D J     0 * 8 /
H
 ! 'C 'TG G<- <<JC JZL% L^b
 b
J  6 +/-( /+- 
 .2 $	?
?? ]+? 	?
 ?$N ND NT N2} 2 22M 2r8   