U
    4Jex,                     @   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 eeZe	d	Ze	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,Z-G d,d- d-e#e, e,Z.G d.d/ d/e%Z/G d0d1 d1e/Z0G d2d3 d3e#e/ e/Z1G d4d5 d5e$Z2G d6d7 d7e2Z3G d8d9 d9e"e2 e2Z4G d:d; d;e%Z5G d<d= d=e5Z6G d>d? d?e#e5 e5Z7dS )@    )ABCabstractmethod)	dataclass)	getLogger)compile)	CallableDict	GeneratorGenericIterableOptionalSequenceTypeVarUnion)metrics)Observation)
Attributesz[a-zA-Z][-_.a-zA-Z0-9]{0,62}z[\x00-\x7F]{0,63}T)frozenc                   @   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)__name__
__module____qualname____doc__r   float__annotations__ r   r   T/tmp/pip-unpacked-wheel-7_167w8m/mysql/opentelemetry/metrics/_internal/instrument.pyr   -   s   
r   InstrumentT
Instrument)boundNc                   @   sN   e Zd ZdZed	eeeddddZeeeeeee	e f dddZ
dS )
r   z7Abstract class that serves as base for all instruments. Nnameunitdescriptionreturnc                 C   s   d S Nr   selfr#   r$   r%   r   r   r   __init__C   s    zInstrument.__init__c                 C   sn   i }t | dk	r| |d< nd|d< |dkr0d}t|dk	rH||d< nd|d< |dkrbd|d< n||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_descriptionL   s    


z'Instrument._check_name_unit_description)r!   r!   )r   r   r   r   r   strr*   staticmethodr   r   r/   r   r   r   r   r   @   s        c                   @   sF   e Zd ZdeeeddddZddddd	Zededd
dZdS )_ProxyInstrumentr!   Nr"   c                 C   s   || _ || _|| _d | _d S r'   )_name_unit_description_real_instrumentr(   r   r   r   r*   q   s    z_ProxyInstrument.__init__metrics.Metermeterr&   c                 C   s   |  || _dS )z;Called when a real meter is set on the creating _ProxyMeterN)_create_real_instrumentr6   r)   r9   r   r   r   on_meter_set|   s    z_ProxyInstrument.on_meter_setc                 C   s   dS )z:Create an instance of the real instrument. Implement this.Nr   r;   r   r   r   r:      s    z(_ProxyInstrument._create_real_instrument)r!   r!   )	r   r   r   r0   r*   r<   r   r   r:   r   r   r   r   r2   p   s     r2   c                       s4   e Zd Zdeeee  eedd fddZ  ZS )_ProxyAsynchronousInstrumentNr!   r#   	callbacksr$   r%   r&   c                    s   t  ||| || _d S r'   )superr*   
_callbacksr)   r#   r?   r$   r%   	__class__r   r   r*      s    z%_ProxyAsynchronousInstrument.__init__)Nr!   r!   )	r   r   r   r0   r   r   	CallbackTr*   __classcell__r   r   rC   r   r=      s      
r=   c                   @   s   e Zd ZdZdS )Synchronousz*Base class for all synchronous instrumentsNr   r   r   r   r   r   r   r   rG      s   rG   c                       s<   e Zd ZdZedeeee  eedd fddZ	  Z
S )Asynchronousz+Base class for all asynchronous instrumentsNr!   r>   c                    s   t  j|||d d S N)r$   r%   r@   r*   rB   rC   r   r   r*      s    zAsynchronous.__init__)Nr!   r!   )r   r   r   r   r   r0   r   r   rE   r*   rF   r   r   rC   r   rI      s      
rI   c                   @   s4   e Zd ZdZedeeef ee	 ddddZ
dS )CounterzOA Counter is a synchronous `Instrument` which supports non-negative increments.Namount
attributesr&   c                 C   s   d S r'   r   r)   rN   rO   r   r   r   add   s    zCounter.add)Nr   r   r   r   r   r   intr   r   r   rQ   r   r   r   r   rL      s    
rL   c                       sR   e Zd ZdZd
eeedd fddZdeeef e	e
 dd fdd	Z  ZS )NoOpCounterz"No-op implementation of `Counter`.r!   Nr"   c                    s   t  j|||d d S rJ   rK   r(   rC   r   r   r*      s    zNoOpCounter.__init__rM   c                    s   t  j||dS N)rO   r@   rQ   rP   rC   r   r   rQ      s    zNoOpCounter.add)r!   r!   )Nr   r   r   r   r0   r*   r   rS   r   r   r   rQ   rF   r   r   rC   r   rT      s      
rT   c                   @   s<   e Zd Zd	eeef ee ddddZde	dddZ
dS )
_ProxyCounterNrM   c                 C   s   | j r| j || d S r'   r6   rQ   rP   r   r   r   rQ      s    z_ProxyCounter.addr7   r8   c                 C   s   | | j| j| jS r'   )Zcreate_counterr3   r4   r5   r;   r   r   r   r:      s    z%_ProxyCounter._create_real_instrument)N)r   r   r   r   rS   r   r   r   rQ   rL   r:   r   r   r   r   rX      s    
rX   c                   @   s4   e Zd ZdZedeeef ee	 ddddZ
dS )UpDownCounterzXAn UpDownCounter is a synchronous `Instrument` which supports increments and decrements.NrM   c                 C   s   d S r'   r   rP   r   r   r   rQ      s    zUpDownCounter.add)NrR   r   r   r   r   rZ      s    
rZ   c                       sR   e Zd ZdZd
eeedd fddZdeeef e	e
 dd fdd	Z  ZS )NoOpUpDownCounterz(No-op implementation of `UpDownCounter`.r!   Nr"   c                    s   t  j|||d d S rJ   rK   r(   rC   r   r   r*      s    zNoOpUpDownCounter.__init__rM   c                    s   t  j||dS rU   rV   rP   rC   r   r   rQ      s    zNoOpUpDownCounter.add)r!   r!   )NrW   r   r   rC   r   r[      s      
r[   c                   @   s<   e Zd Zd	eeef ee ddddZde	dddZ
dS )
_ProxyUpDownCounterNrM   c                 C   s   | j r| j || d S r'   rY   rP   r   r   r   rQ      s    z_ProxyUpDownCounter.addr7   r8   c                 C   s   | | j| j| jS r'   )Zcreate_up_down_counterr3   r4   r5   r;   r   r   r   r:      s    z+_ProxyUpDownCounter._create_real_instrument)N)r   r   r   r   rS   r   r   r   rQ   rZ   r:   r   r   r   r   r\      s    
r\   c                   @   s   e Zd ZdZdS )ObservableCounterzAn ObservableCounter is an asynchronous `Instrument` which reports monotonically
    increasing value(s) when the instrument is being observed.
    NrH   r   r   r   r   r]      s   r]   c                       s8   e Zd ZdZdeeee  eedd fddZ  Z	S )NoOpObservableCounterz,No-op implementation of `ObservableCounter`.Nr!   r>   c                    s   t  j||||d d S rJ   rK   rB   rC   r   r   r*     s    zNoOpObservableCounter.__init__)Nr!   r!   
r   r   r   r   r0   r   r   rE   r*   rF   r   r   rC   r   r^     s      
r^   c                   @   s   e Zd ZdedddZdS )_ProxyObservableCounterr7   r8   c                 C   s   | | j| j| j| jS r'   )Zcreate_observable_counterr3   rA   r4   r5   r;   r   r   r   r:     s       z/_ProxyObservableCounter._create_real_instrumentN)r   r   r   r]   r:   r   r   r   r   r`     s   r`   c                   @   s   e Zd ZdZdS )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.
    NrH   r   r   r   r   ra     s   ra   c                       s8   e Zd ZdZdeeee  eedd fddZ  Z	S )NoOpObservableUpDownCounterz2No-op implementation of `ObservableUpDownCounter`.Nr!   r>   c                    s   t  j||||d d S rJ   rK   rB   rC   r   r   r*   %  s    z$NoOpObservableUpDownCounter.__init__)Nr!   r!   r_   r   r   rC   r   rb   "  s      
rb   c                   @   s   e Zd ZdedddZdS )_ProxyObservableUpDownCounterr7   r8   c                 C   s   | | j| j| j| jS r'   )Z!create_observable_up_down_counterr3   rA   r4   r5   r;   r   r   r   r:   3  s       z5_ProxyObservableUpDownCounter._create_real_instrumentN)r   r   r   ra   r:   r   r   r   r   rc   /  s   rc   c                   @   s4   e Zd ZdZedeeef ee	 dd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.
    NrM   c                 C   s   d S r'   r   rP   r   r   r   recordA  s    zHistogram.record)N)r   r   r   r   r   r   rS   r   r   r   re   r   r   r   r   rd   ;  s    
rd   c                       sR   e Zd ZdZd
eeedd fddZdeeef e	e
 dd fdd	Z  ZS )NoOpHistogramz$No-op implementation of `Histogram`.r!   Nr"   c                    s   t  j|||d d S rJ   rK   r(   rC   r   r   r*   M  s    zNoOpHistogram.__init__rM   c                    s   t  j||dS rU   )r@   re   rP   rC   r   r   re   U  s    zNoOpHistogram.record)r!   r!   )N)r   r   r   r   r0   r*   r   rS   r   r   r   re   rF   r   r   rC   r   rf   J  s      
rf   c                   @   s<   e Zd Zd	eeef ee ddddZde	dddZ
dS )
_ProxyHistogramNrM   c                 C   s   | j r| j || d S r'   )r6   re   rP   r   r   r   re   ^  s    z_ProxyHistogram.recordr7   r8   c                 C   s   | | j| j| jS r'   )Zcreate_histogramr3   r4   r5   r;   r   r   r   r:   f  s    z'_ProxyHistogram._create_real_instrument)N)r   r   r   r   rS   r   r   r   re   rd   r:   r   r   r   r   rg   ]  s    
rg   c                   @   s   e Zd ZdZdS )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.
    NrH   r   r   r   r   rh   j  s   rh   c                       s8   e Zd ZdZdeeee  eedd fddZ  Z	S )NoOpObservableGaugez*No-op implementation of `ObservableGauge`.Nr!   r>   c                    s   t  j||||d d S rJ   rK   rB   rC   r   r   r*   t  s    zNoOpObservableGauge.__init__)Nr!   r!   r_   r   r   rC   r   ri   q  s      
ri   c                   @   s   e Zd ZdedddZdS )_ProxyObservableGauger7   r8   c                 C   s   | | j| j| j| jS r'   )Zcreate_observable_gauger3   rA   r4   r5   r;   r   r   r   r:     s       z-_ProxyObservableGauge._create_real_instrumentN)r   r   r   rh   r:   r   r   r   r   rj   ~  s   rj   )8abcr   r   Zdataclassesr   loggingr   rer   Z
re_compiletypingr   r   r	   r
   r   r   r   r   r   Zmysql.opentelemetryr   Z1mysql.opentelemetry.metrics._internal.observationr   Zmysql.opentelemetry.util.typesr   r   Z_loggerr+   r-   r   r   rE   r   r2   r=   rG   rI   rL   rT   rX   rZ   r[   r\   r]   r^   r`   ra   rb   rc   rd   rf   rg   rh   ri   rj   r   r   r   r   <module>   sd   ,0
 	

