U
    4Je܆                     @   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
 d dlmZ d dlmZmZmZmZ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$ d dl%mZ&m'Z'm(Z(mZ)m*Z*m+Z+m,Z, d dl-m.Z. ede+e*Z/ee0Z1G dd deZ2G dd deee/ Z3G dd de3Z4G dd de3e, Z5G dd de3e( Z6G dd de3e) Z7G dd de3e) Z8G dd deZ9G d d! d!e9Z:G d"d# d#e9Z;G d$d% d%e9Z<G d&d' d'e9Z=G d(d) d)e9Z>G d*d+ d+e9Z?d,S )-    )ABCabstractmethod)bisect_left)IntEnum)	getLogger)inf)Lock)GenericListOptionalSequenceTypeVar)	AsynchronousCounter	Histogram
InstrumentObservableCounterObservableGaugeObservableUpDownCounterSynchronousUpDownCounter)Buckets)ExponentMapping)LogarithmMapping)Measurement)r   ExponentialHistogramDataPointGauger   HistogramDataPointNumberDataPointSum)
Attributes_DataPointVarTc                   @   s   e Zd ZdZdZdZdZdS )AggregationTemporalityz`
    The temporality to use when aggregating data.

    Can be one of the following values:
    r         N)__name__
__module____qualname____doc__UNSPECIFIEDDELTA
CUMULATIVE r,   r,   Y/tmp/pip-unpacked-wheel-7_167w8m/mysql/opentelemetry/sdk/metrics/_internal/aggregation.pyr"   >   s   r"   c                   @   sH   e Zd ZedddZeeddddZeee	e
e dd	d
ZdS )_Aggregation
attributesc                 C   s   t  | _|| _d | _d S N)r   _lock_attributes_previous_pointselfr0   r,   r,   r-   __init__K   s    z_Aggregation.__init__Nmeasurementreturnc                 C   s   d S r1   r,   r6   r9   r,   r,   r-   	aggregateP   s    z_Aggregation.aggregateaggregation_temporalitycollection_start_nanor:   c                 C   s   d S r1   r,   r6   r>   r?   r,   r,   r-   collectT   s    z_Aggregation.collect)r%   r&   r'   r    r7   r   r   r<   r"   intr   r!   rA   r,   r,   r,   r-   r.   J   s   r.   c                   @   s2   e Zd ZeddddZeeee dddZ	dS )_DropAggregationNr8   c                 C   s   d S r1   r,   r;   r,   r,   r-   r<   ^   s    z_DropAggregation.aggregater=   c                 C   s   d S r1   r,   r@   r,   r,   r-   rA   a   s    z_DropAggregation.collect)
r%   r&   r'   r   r<   r"   rB   r   r!   rA   r,   r,   r,   r-   rC   ]   s
   rC   c                       sN   e Zd Zeeeed fddZeddddZ	eee
e dd	d
Z  ZS )_SumAggregation)r0   instrument_is_monotonicinstrument_temporalitystart_time_unix_nanoc                    s<   t  | || _|| _|| _| jtjkr2d| _nd | _d S Nr   )superr7   _start_time_unix_nano_instrument_temporalityZ_instrument_is_monotonicr"   r*   _value)r6   r0   rE   rF   rG   	__class__r,   r-   r7   j   s    z_SumAggregation.__init__Nr8   c              	   C   s4   | j $ | jd krd| _| j|j | _W 5 Q R X d S rH   )r2   rL   valuer;   r,   r,   r-   r<   |   s    
z_SumAggregation.aggregater=   c              	   C   s   | j tjkr8| j | j}| j}d| _|| _W 5 Q R X n<| j0 | jdkrXW 5 Q R  dS | j}d| _| j}W 5 Q R X t| j|||d}| jdks| j |kr|| _|S |tjkr|j	| jj	 }| jj
}n|j	| jj	 }| jj}t| j||j
|d}|| _|S )zx
        Atomically return a point for the current value of the metric and
        reset the aggregation value.
        r   Nr0   rG   time_unix_nanorO   )rK   r"   r*   r2   rL   rJ   r   r3   r4   rO   rQ   rG   )r6   r>   r?   rO   rG   current_pointZoutput_start_time_unix_nanor,   r,   r-   rA      sH    	



z_SumAggregation.collect)r%   r&   r'   r    boolr"   rB   r7   r   r<   r   r   rA   __classcell__r,   r,   rM   r-   rD   i   s   rD   c                       sF   e Zd Zed fddZedddZeee	e
 ddd	Z  ZS )
_LastValueAggregationr/   c                    s   t  | d | _d S r1   )rI   r7   rL   r5   rM   r,   r-   r7      s    z_LastValueAggregation.__init__)r9   c              	   C   s   | j  |j| _W 5 Q R X d S r1   )r2   rO   rL   r;   r,   r,   r-   r<      s    z_LastValueAggregation.aggregater=   c              	   C   sH   | j * | jdkr W 5 Q R  dS | j}d| _W 5 Q R X t| jd||dS )P
        Atomically return a point for the current value of the metric.
        Nr   rP   )r2   rL   r   r3   )r6   r>   r?   rO   r,   r,   r-   rA      s    
z_LastValueAggregation.collect)r%   r&   r'   r    r7   r   r<   r"   rB   r   r!   rA   rT   r,   r,   rM   r-   rU      s   rU   c                       sf   e Zd Zdeeee ed fddZe	e dddZ
ed	d
ddZeeee dddZ  ZS )#_ExplicitBucketHistogramAggregationg        g      @g      $@g      9@g      I@g     R@g      Y@g     @o@g     @@g     p@g     @@g     @g     @g     L@g     @T)r0   rG   
boundariesrecord_min_maxc                    sL   t  | t|| _|  | _t| _t | _d| _	|| _
|| _tj| _d S rH   )rI   r7   tuple_boundaries_get_empty_bucket_counts_bucket_countsr   _min_max_sum_record_min_maxrJ   r"   r*   rK   )r6   r0   rG   rY   rZ   rM   r,   r-   r7      s    

z,_ExplicitBucketHistogramAggregation.__init__)r:   c                 C   s   dgt | jd  S Nr   r#   )lenr\   )r6   r,   r,   r-   r]     s    z<_ExplicitBucketHistogramAggregation._get_empty_bucket_countsNr8   c                 C   sT   |j }| jr(t| j|| _t| j|| _|  j|7  _| jt| j	|  d7  < d S )Nr#   )
rO   rb   minr_   maxr`   ra   r^   r   r\   )r6   r9   rO   r,   r,   r-   r<     s    z-_ExplicitBucketHistogramAggregation.aggregater=   c           	      C   s|  | j ` t| js W 5 Q R  dS | j}| j}| j}| j}| j}|  | _|| _d| _t| _t | _W 5 Q R X t	| j
||t||t|| j||d	}| jdks| j|kr|| _|S |j}|j}|tjkr| jj}|j| jj }t|j| jj}t|j| jj}dd t|j| jjD }n0| jj}|j| jj }dd t|j| jjD }t	| j
||jt||t||j||d	}|| _|S )rV   Nr   )	r0   rG   rQ   countsumbucket_countsexplicit_boundsre   rf   c                 S   s   g | ]\}}|| qS r,   r,   .0Z
curr_countZ
prev_countr,   r,   r-   
<listcomp>E  s   z?_ExplicitBucketHistogramAggregation.collect.<locals>.<listcomp>c                 S   s   g | ]\}}|| qS r,   r,   rk   r,   r,   r-   rm   O  s   )r2   anyr^   rJ   ra   r`   r_   r]   r   r   r3   rh   r[   r\   r4   rK   rf   re   r"   r+   rG   zipri   rQ   rj   )	r6   r>   r?   ri   rG   sum_max_min_rR   r,   r,   r-   rA     s~    


z+_ExplicitBucketHistogramAggregation.collect)rX   T)r%   r&   r'   r    rB   r   floatrS   r7   r
   r]   r   r<   r"   r   r!   rA   rT   r,   r,   rM   r-   rW      s     %rW   c                       s   e Zd ZdZdZdeeed fddZeddd	d
Z	e
eee dddZdd Zdd Zdd ZedddZdd Z  ZS )&_ExponentialBucketHistogramAggregationr$   i @     )r0   rG   max_sizec                    s   t  | || jk r&td| d|| jkr@td| d|| _d| _d| _d| _t	| _
t	 | _t | _t | _ttj| _tj| _|| _d | _d | _d | _d | _d | _d | _d | _d S )NzBuckets max size z6 is smaller than minimum max size {self._min_max_size}z5 is larger than maximum max size {self._max_max_size}r   )rI   r7   _min_max_size
ValueError_max_max_size	_max_sizera   _count_zero_countr   r_   r`   r   	_positive	_negativer   Z
_max_scale_mappingr"   r*   rK   rJ   _previous_scale_previous_start_time_unix_nano_previous_sum_previous_max_previous_min_previous_positive_previous_negative)r6   r0   rG   rv   rM   r,   r-   r7   s  s6    




z/_ExponentialBucketHistogramAggregation.__init__Nr8   c           
   	   C   s  | j  |j}|| jk r || _|| jkr0|| _|  jd7  _|dkrb|  jd7  _W 5 Q R  d S |  j|7  _|dkr| j}n| }| j}| j	
|}d}t|dkr||_||_||_nV||jk r|j| | jkrd}|}|j}n,||jkr||j | jkrd}|j}|}|r>| | ||| j| j | j	
|}||jk r~|j| }|t|jkrv||d | j ||_n>||jkr||j }|t|jkr||d | j ||_||j }	|	dk r|	t|j7 }	||	 W 5 Q R X d S )Nr#   r   FT)r2   rO   r_   r`   r{   r|   ra   r}   r~   r   Zmap_to_indexrd   index_start	index_end
index_baserz   
_downscale_get_scale_changecountsgrowincrement_bucket)
r6   r9   rO   bucketsindexZis_rescaling_neededlowhighspanbucket_indexr,   r,   r-   r<     sp    







z0_ExponentialBucketHistogramAggregation.aggregater=   c                 C   s  | j  | jdkr"W 5 Q R  dS | j}| j}| j}| j}| j}| j}| j}	|	t krZd}	| j	}
|
tkrld}
| j| jkr~d}n| j
j}t | _t | _|| _d| _d| _d| _t| _	t | _t| j||||||t|j|jdt|j|jdd|
|	d}| jdks| j|krH|| _|| _|	| _|
| _|| _|| _|| _|W  5 Q R  S t| j|}| | j||\}}| | j||\}}t|| || || || }| | j
j| | j| j |tjkr| j}|| j }t |	| j}t|
| j}| !| j|||| | !| j|||| n@| j}|| j }|	}|
}| !| j|||| | !| j|||| t| j||||||t|j|jdt|j|jdd||d}|| _|| _|| _|| _|| _|W  5 Q R  S Q R X dS )rV   r   N)offsetri   )r0   rG   rQ   rg   rh   scaleZ
zero_countpositivenegativeflagsre   rf   )"r2   r{   r~   r}   r|   rJ   ra   r`   r   r_   r   r   r   r   r3   BucketsPointr   r   r   rK   r   r   r   r   r   r   re   _get_low_high_previous_currentr   r   r"   r+   rf   _merge)r6   r>   r?   Zcurrent_negativeZcurrent_positiveZcurrent_zero_countZcurrent_countZcurrent_start_time_unix_nanoZcurrent_sumZcurrent_maxZcurrent_mincurrent_scalerR   	min_scaleZlow_positiveZhigh_positiveZlow_negativeZhigh_negativerG   rp   rq   rr   r,   r,   r-   rA     s   



    

	
z._ExponentialBucketHistogramAggregation.collectc           
      C   s`   |  ||\}}|  ||\}}||kr2|}|}	n&||krD|}|}	nt||}t||}	||	fS r1   )_get_low_highre   rf   )
r6   Zprevious_point_bucketsZcurrent_point_bucketsr   Zprevious_point_lowZprevious_point_highZcurrent_point_lowZcurrent_point_highr   r   r,   r,   r-   r     s"      

zE_ExponentialBucketHistogramAggregation._get_low_high_previous_currentc                 C   s0   |j dgkrdS | jj| }|j|? |j|? fS )Nr   )r   )r   r   Z_scaler   r   )r6   r   r   shiftr,   r,   r-   r     s    z4_ExponentialBucketHistogramAggregation._get_low_highc                 C   s0   d}|| | j kr,|d? }|d? }|d7 }q|S rc   rz   )r6   r   r   changer,   r,   r-   r     s    
z8_ExponentialBucketHistogramAggregation._get_scale_change)r   c                 C   s`   |dkrd S |dk rt d| jj| }|| || |dkrNt|}nt|}|| _d S )Nr   zInvalid change of scale)	Exceptionr   r   Z	downscaler   r   )r6   r   r   r   Z	new_scalemappingr,   r,   r-   r     s    


z1_ExponentialBucketHistogramAggregation._downscalec                 C   s  || }t |jD ]\}}|dkr$q|j| |? }	|	|jk r~|j|	 }
|
| jkrXtd|
t|jkrx||
d | j |	|_|	|jkr|	|j }
|
| jkrtd|
t|jkr||
d | j |	|_|	|j	 }|dk r|t|j7 }|t
jkr| }|j||d qd S )Nr   zIncorrect merge scaler#   )	increment)	enumerater   r   r   r   rz   r   rd   r   r   r"   r*   r   )r6   Zprevious_bucketsZcurrent_bucketsr   r   r>   Zcurrent_changeZcurrent_bucket_indexZcurrent_bucketr   r   r   r,   r,   r-   r     s2    







z-_ExponentialBucketHistogramAggregation._merge)ru   )r%   r&   r'   rw   ry   r    rB   r7   r   r<   r"   r   r!   rA   r   r   r   r   r   rT   r,   r,   rM   r-   rt   g  s&   
 @l .rt   c                   @   s(   e Zd ZdZeeeeedddZ	dS )Aggregationz/
    Base class for all aggregation types.
    
instrumentr0   rG   r:   c                 C   s   dS )zCreates an aggregationNr,   r6   r   r0   rG   r,   r,   r-   _create_aggregationB  s    zAggregation._create_aggregationN)
r%   r&   r'   r(   r   r   r    rB   r.   r   r,   r,   r,   r-   r   =  s   r   c                   @   s$   e Zd ZdZeeeedddZdS )DefaultAggregationa  
    The default aggregation to be used in a `View`.

    This aggregation will create an actual aggregation depending on the
    instrument type, as specified next:

    ==================================================== ====================================
    Instrument                                           Aggregation
    ==================================================== ====================================
    `mysql.opentelemetry.sdk.metrics.Counter`                  `SumAggregation`
    `mysql.opentelemetry.sdk.metrics.UpDownCounter`            `SumAggregation`
    `mysql.opentelemetry.sdk.metrics.ObservableCounter`        `SumAggregation`
    `mysql.opentelemetry.sdk.metrics.ObservableUpDownCounter`  `SumAggregation`
    `mysql.opentelemetry.sdk.metrics.Histogram`                `ExplicitBucketHistogramAggregation`
    `mysql.opentelemetry.sdk.metrics.ObservableGauge`          `LastValueAggregation`
    ==================================================== ====================================
    r   c                 C   s   t |trt|dtj|dS t |tr8t|dtj|dS t |trTt|dtj|dS t |trpt|dtj|dS t |t	rt
||S t |trt|S tdt| dd S )NT)rE   rF   rG   FzInvalid instrument type z found)
isinstancer   rD   r"   r*   r   r   r+   r   r   rW   r   rU   r   typer   r,   r,   r-   r   _  sB    






z&DefaultAggregation._create_aggregationN	r%   r&   r'   r(   r   r    rB   r.   r   r,   r,   r,   r-   r   L  s   r   c                   @   s0   e Zd Zd	edddZeeeedddZdS )
%ExponentialBucketHistogramAggregationru   rv   c                 C   s
   || _ d S r1   r   )r6   rv   r,   r,   r-   r7     s    z.ExponentialBucketHistogramAggregation.__init__r   c                 C   s   t ||| jdS )Nr   )rt   rz   r   r,   r,   r-   r     s
    z9ExponentialBucketHistogramAggregation._create_aggregationN)ru   )	r%   r&   r'   rB   r7   r   r    r.   r   r,   r,   r,   r-   r     s    r   c                   @   s<   e Zd ZdZdee eddddZee	e
edd	d
ZdS )"ExplicitBucketHistogramAggregationaG  This aggregation informs the SDK to collect:

    - Count of Measurement values falling within explicit bucket boundaries.
    - Arithmetic sum of Measurement values in population. This SHOULD NOT be collected when used with instruments that record negative measurements, e.g. UpDownCounter or ObservableGauge.
    - Min (optional) Measurement value in population.
    - Max (optional) Measurement value in population.


    Args:
        boundaries: Array of increasing values representing explicit bucket boundary values.
        record_min_max: Whether to record min and max.
    rX   TN)rY   rZ   r:   c                 C   s   || _ || _d S r1   )r\   rb   )r6   rY   rZ   r,   r,   r-   r7     s    z+ExplicitBucketHistogramAggregation.__init__r   c                 C   s   t ||| j| jS r1   )rW   r\   rb   r   r,   r,   r-   r     s    z6ExplicitBucketHistogramAggregation._create_aggregation)rX   T)r%   r&   r'   r(   r   rs   rS   r7   r   r    rB   r.   r   r,   r,   r,   r-   r     s     r   c                   @   s$   e Zd ZdZeeeedddZdS )SumAggregationzbThis aggregation informs the SDK to collect:

    - The arithmetic sum of Measurement values.
    r   c                 C   s@   t j}t|trt j}nt|tr(t j}t|t|tt	f||S r1   )
r"   r)   r   r   r*   r   r+   rD   r   r   )r6   r   r0   rG   Ztemporalityr,   r,   r-   r     s    

z"SumAggregation._create_aggregationNr   r,   r,   r,   r-   r     s   r   c                   @   s$   e Zd ZdZeeeedddZdS )LastValueAggregationz
    This aggregation informs the SDK to collect:

    - The last Measurement.
    - The timestamp of the last Measurement.
    r   c                 C   s   t |S r1   )rU   r   r,   r,   r-   r     s    z(LastValueAggregation._create_aggregationNr   r,   r,   r,   r-   r     s   	r   c                   @   s$   e Zd ZdZeeeedddZdS )DropAggregationz=Using this aggregation will make all measurements be ignored.r   c                 C   s   t |S r1   )rC   r   r,   r,   r-   r     s    z#DropAggregation._create_aggregationNr   r,   r,   r,   r-   r     s   r   N)@abcr   r   bisectr   enumr   loggingr   mathr   	threadingr   typingr	   r
   r   r   r   Zmysql.opentelemetry.metricsr   r   r   r   r   r   r   r   r   ZGmysql.opentelemetry.sdk.metrics._internal.exponential_histogram.bucketsr   ZXmysql.opentelemetry.sdk.metrics._internal.exponential_histogram.mapping.exponent_mappingr   ZYmysql.opentelemetry.sdk.metrics._internal.exponential_histogram.mapping.logarithm_mappingr   Z5mysql.opentelemetry.sdk.metrics._internal.measurementr   Z/mysql.opentelemetry.sdk.metrics._internal.pointr   r   r   ZHistogramPointr   r   r   Zmysql.opentelemetry.util.typesr    r!   r%   Z_loggerr"   r.   rC   rD   rU   rW   rt   r   r   r   r   r   r   r   r,   r,   r,   r-   <module>   sB   ,$	V 
   YB4