
    g                        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 d dlmZmZ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 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, d dl-m.Z. d dl/m&Z0 d dl/m1Z1m2Z2m3Z3m4Z4 d dl/m5Z6 d dl/mZ7 d dl8m9Z9  ede3e2      Z: e
e;      Z< G d de      Z= G d deee:         Z> G d de>      Z? G d de>e4         Z@ G d de>e6         ZA G d d e>e7         ZB G d! d"e>e7         ZC G d# d$e      ZD G d% d&eD      ZE G d' d(eD      ZF G d) d*eD      ZG G d+ d,eD      ZH G d- d.eD      ZI G d/ d0eD      ZJy1)2    )ABCabstractmethod)bisect_left)IntEnum)partial)	getLogger)inf)Lock)CallableGenericListOptionalSequenceTypeTypeVar)
AsynchronousCounter	Histogram
InstrumentObservableCounterObservableGaugeObservableUpDownCounterSynchronousUpDownCounter_Gauge)ExemplarExemplarReservoirBuilder)Buckets)Mapping)ExponentMapping)LogarithmMapping)Measurement)ExponentialHistogramDataPointHistogramDataPointNumberDataPointSum)Gauge)r   )
Attributes_DataPointVarTc                       e Zd ZdZdZdZdZy)AggregationTemporalityz`
    The temporality to use when aggregating data.

    Can be one of the following values:
    r         N)__name__
__module____qualname____doc__UNSPECIFIEDDELTA
CUMULATIVE     d/var/www/openai/venv/lib/python3.12/site-packages/opentelemetry/sdk/metrics/_internal/aggregation.pyr+   r+   I   s     KEJr6   r+   c                       e Zd ZdedefdZe	 ddededdfd       Z	ed	e
d
edee   fd       Zdee   fdZdededdfdZy)_Aggregation
attributesreservoir_builderc                 V    t               | _        || _         |       | _        d | _        y N)r
   _lock_attributes
_reservoir_previous_point)selfr:   r;   s      r7   __init__z_Aggregation.__init__V   s'    
 V
%+-#r6   measurementshould_sample_exemplarreturnNc                      y)zAggregate a measurement.

        Args:
            measurement: Measurement to aggregate
            should_sample_exemplar: Whether the measurement should be sampled by the exemplars reservoir or not.
        Nr5   rB   rD   rE   s      r7   	aggregatez_Aggregation.aggregate`       r6   "collection_aggregation_temporalitycollection_start_nanoc                      y r=   r5   rB   rK   rL   s      r7   collectz_Aggregation.collectk   s     	r6   c                 L    | j                   j                  | j                        S )zpReturns the collected exemplars.

        Returns:
            The exemplars collected by the reservoir
        )r@   rO   r?   rB   s    r7   _collect_exemplarsz_Aggregation._collect_exemplarss   s     &&t'7'788r6   c                     |rG| j                   j                  |j                  |j                  |j                  |j
                         yy)a7  Offer the measurement to the exemplar reservoir for sampling.

        It should be called within the each :ref:`aggregate` call.

        Args:
            measurement: The new measurement
            should_sample_exemplar: Whether the measurement should be sampled by the exemplars reservoir or not.
        N)r@   offervaluetime_unix_nanor:   contextrH   s      r7   _sample_exemplarz_Aggregation._sample_exemplar{   sB     "OO!!!!**&&##	 "r6   T)r.   r/   r0   r(   r   rC   r   r"   boolrI   r+   intr   r)   rO   r   r   rR   rX   r5   r6   r7   r9   r9   U   s    $$ 4$ GK&@D	  ,B  # 
.	!	 9HX$6 9&@D	r6   r9   c                   >    e Zd Z	 d	dededdfdZdededee	   fdZ
y)
_DropAggregationrD   rE   rF   Nc                      y r=   r5   rH   s      r7   rI   z_DropAggregation.aggregate   s     	r6   rK   rL   c                      y r=   r5   rN   s      r7   rO   z_DropAggregation.collect   s    
 	r6   rY   )r.   r/   r0   r"   rZ   rI   r+   r[   r   r)   rO   r5   r6   r7   r]   r]      sH    GK&@D	
,B  # 
.	!	r6   r]   c            
       f     e Zd Zdededededef
 fdZ	 dde	ded	d
fdZ
deded	ee   fdZ xZS )_SumAggregationr:   instrument_is_monotonic"instrument_aggregation_temporalitystart_time_unix_nanor;   c                     t         |   ||       || _        || _        || _        d | _        | j                  | _        d| _        y Nr   )superrC   _start_time_unix_nano#_instrument_aggregation_temporality_instrument_is_monotonic_value_previous_collection_start_nano_previous_value)rB   r:   rb   rc   rd   r;   	__class__s         r7   rC   z_SumAggregation.__init__   sP     	%67%9". 	0 )@%/3/I/I, r6   rD   rE   rF   Nc                     | j                   5  | j                  d| _        | j                  |j                  z   | _        d d d        | j                  ||       y # 1 sw Y   xY wrf   )r>   rk   rU   rX   rH   s      r7   rI   z_SumAggregation.aggregate   sR     ZZ{{"++(9(99DK	  	k+AB Zs   2AA#rK   rL   c                 v   | j                   5  | j                  }d| _        | j                  t        j                  u r|t        j                  u rP| j
                  }|| _        |
	 ddd       yt        | j                  | j                         |||      cddd       S |d}|| j                  z   | _	        t        | j                  | j                         | j                  || j                        cddd       S |
	 ddd       y|t        j                  u rZ|| j                  z
  }|| _	        | j
                  }|| _        t        | j                  | j                         |||      cddd       S t        | j                  | j                         | j                  ||      cddd       S # 1 sw Y   yxY w)a  
        Atomically return a point for the current value of the metric and
        reset the aggregation value.

        Synchronous instruments have a method which is called directly with
        increments for a given quantity:

        For example, an instrument that counts the amount of passengers in
        every vehicle that crosses a certain point in a highway:

        synchronous_instrument.add(2)
        collect(...)  # 2 passengers are counted
        synchronous_instrument.add(3)
        collect(...)  # 3 passengers are counted
        synchronous_instrument.add(1)
        collect(...)  # 1 passenger is counted

        In this case the instrument aggregation temporality is DELTA because
        every value represents an increment to the count,

        Asynchronous instruments have a callback which returns the total value
        of a given quantity:

        For example, an instrument that measures the amount of bytes written to
        a certain hard drive:

        callback() -> 1352
        collect(...) # 1352 bytes have been written so far
        callback() -> 2324
        collect(...) # 2324 bytes have been written so far
        callback() -> 4542
        collect(...) # 4542 bytes have been written so far

        In this case the instrument aggregation temporality is CUMULATIVE
        because every value represents the total of the measurement.

        There is also the collection aggregation temporality, which is passed
        to this method. The collection aggregation temporality defines the
        nature of the returned value by this aggregation.

        When the collection aggregation temporality matches the
        instrument aggregation temporality, then this method returns the
        current value directly:

        synchronous_instrument.add(2)
        collect(DELTA) -> 2
        synchronous_instrument.add(3)
        collect(DELTA) -> 3
        synchronous_instrument.add(1)
        collect(DELTA) -> 1

        callback() -> 1352
        collect(CUMULATIVE) -> 1352
        callback() -> 2324
        collect(CUMULATIVE) -> 2324
        callback() -> 4542
        collect(CUMULATIVE) -> 4542

        When the collection aggregation temporality does not match the
        instrument aggregation temporality, then a conversion is made. For this
        purpose, this aggregation keeps a private attribute,
        self._previous_value.

        When the instrument is synchronous:

        self._previous_value is the sum of every previously
        collected (delta) value. In this case, the returned (cumulative) value
        will be:

        self._previous_value + value

        synchronous_instrument.add(2)
        collect(CUMULATIVE) -> 2
        synchronous_instrument.add(3)
        collect(CUMULATIVE) -> 5
        synchronous_instrument.add(1)
        collect(CUMULATIVE) -> 6

        Also, as a diagram:

        time ->

        self._previous_value
        |-------------|

        value (delta)
                      |----|

        returned value (cumulative)
        |------------------|

        When the instrument is asynchronous:

        self._previous_value is the value of the previously
        collected (cumulative) value. In this case, the returned (delta) value
        will be:

        value - self._previous_value

        callback() -> 1352
        collect(DELTA) -> 1352
        callback() -> 2324
        collect(DELTA) -> 972
        callback() -> 4542
        collect(DELTA) -> 2218

        Also, as a diagram:

        time ->

        self._previous_value
        |-------------|

        value (cumulative)
        |------------------|

        returned value (delta)
                      |----|
        Nr:   	exemplarsrd   rV   rU   r   )r>   rk   ri   r+   r3   rl   r%   r?   rR   rm   rh   )rB   rK   rL   rU   previous_collection_start_nanoresult_values         r7   rO   z_SumAggregation.collect   s   z ZZKKEDK 88)//0 7-334 << 3 . 8 }#- Z0 +#'#3#3"&"9"9";-K'<#1 Z@ =E',t/C/C'C$&#//"557)-)C)C#8..K Z` } g Zl 3)//0  %t';';;',$ 88 / 8M4&#//"557)G#8&E ZT #++113%)%?%?4U ZZs,   AF/.'F/AF/<F/A"F/41F//F8rY   )r.   r/   r0   r(   rZ   r+   r[   r   rC   r"   rI   r   r%   rO   __classcell__rn   s   @r7   ra   ra      s    !! "&! -C	!
 "! 4!, HL	C&	C@D	C		CM,BM  #M 
/	"	Mr6   ra   c                   V     e Zd Zdedef fdZ	 ddedefdZde	de
d	ee   fd
Z xZS )_LastValueAggregationr:   r;   c                 4    t         |   ||       d | _        y r=   )rg   rC   rk   )rB   r:   r;   rn   s      r7   rC   z_LastValueAggregation.__init__  s    
 	%67r6   rD   rE   c                     | j                   5  |j                  | _        d d d        | j                  ||       y # 1 sw Y   xY wr=   )r>   rU   rk   rX   rH   s      r7   rI   z_LastValueAggregation.aggregate  s8     ZZ%++DK  	k+AB Zs	   :ArK   rL   rF   c                     | j                   5  | j                  
	 ddd       y| j                  }d| _        ddd       | j                         }t        | j                  |d|      S # 1 sw Y   3xY w)P
        Atomically return a point for the current value of the metric.
        Nrq   )r>   rk   rR   r%   r?   )rB   rK   rL   rU   rr   s        r7   rO   z_LastValueAggregation.collect  sp     ZZ{{" Z KKEDK	  ++-	''!%0
 	
 Zs   A)A))A2rY   )r.   r/   r0   r(   r   rC   r"   rZ   rI   r+   r[   r   r)   rO   ru   rv   s   @r7   rx   rx     s_     4 HLC&C@DC
,B
  #
 
.	!	
r6   rx   c                        e Zd Z	 	 ddededededee   de	f fdZ
dee   fd	Z	 dd
ede	ddfdZdededee   fdZ xZS )#_ExplicitBucketHistogramAggregationr:   rc   rd   r;   
boundariesrecord_min_maxc                 >   t         |   |t        ||             || _        || _        t        |      | _        || _        d | _        t        | _
        t         | _        d| _        d | _        t        | _        t         | _        d| _        | j                  | _        y )N)r   r;   r   )rg   rC   r   ri   rh   tuple_boundaries_record_min_maxrk   r	   _min_max_sumrm   _previous_min_previous_max_previous_sumrl   )rB   r:   rc   rd   r;   r   r   rn   s          r7   rC   z,_ExplicitBucketHistogramAggregation.__init__  s    2 	%!j 	 	
 / 	0 &:" ,-	D		# !T/3/I/I,r6   rF   c                 :    dgt        | j                        dz   z  S Nr   r,   )lenr   rQ   s    r7   _get_empty_bucket_countsz<_ExplicitBucketHistogramAggregation._get_empty_bucket_counts  s    sc$**+a/00r6   rD   rE   Nc                    | j                   5  | j                  | j                         | _        |j                  }| xj                  |z  c_        | j
                  r6t        | j                  |      | _        t        | j                  |      | _	        | j                  t        | j                  |      xx   dz  cc<   d d d        | j                  ||       y # 1 sw Y   xY w)Nr,   )r>   rk   r   rU   r   r   minr   maxr   r   r   rX   )rB   rD   rE   measurement_values       r7   rI   z-_ExplicitBucketHistogramAggregation.aggregate  s     ZZ{{"";;= + 1 1II**I##		+<=			+<=	KKD$4$46GHIQNI  	k+AB Zs   B0CC!rK   rL   c                    | j                   5  | j                  }| j                  }| j                  }| j                  }d| _        d| _        t
        | _        t
         | _        | j                  t        j                  u r|t        j                  u rq| j                  }|| _	        |
	 ddd       yt        | j                  | j                         ||t        |      |t        |      | j                  ||
      cddd       S || j!                         }| j"                  | j!                         | _        t%        || j"                        D 	cg c]
  \  }}	||	z    c}	}| _        t'        || j(                        | _        t+        || j,                        | _        || j.                  z   | _        t        | j                  | j                         | j0                  |t        | j"                        | j.                  t        | j"                        | j                  | j(                  | j,                  
      cddd       S 	 ddd       yc c}	}w # 1 sw Y   yxY w)r|   Nr   )
r:   rr   rd   rV   countsumbucket_countsexplicit_boundsr   r   )r>   rk   r   r   r   r	   ri   r+   r3   rl   r$   r?   rR   r   r   r   r   rm   zipr   r   r   r   r   rh   )
rB   rK   rL   rU   sum_min_max_rs   value_elementprevious_value_elements
             r7   rO   z+_ExplicitBucketHistogramAggregation.collect  s5    ZZKKE99D99D99DDKDIDIDI 88)//0 7-334 << 3 . 8 }#; Z> .#'#3#3"&"9"9";-K'<!%j &+El(,(8(8  ? ZX = 99;E''/+/+H+H+JD( UD$8$89(
 :%. "$:: :($ &)t/A/A%B"%(t/A/A%B"%)D,>,>%>")#//"557)-)C)C#8d223**"'(<(<"=$($4$4****{ ZT U Zd(e Zs3   BI1AIAII	CI?I	II)g        g      @g      $@g      9@g      I@g     R@g      Y@g     @o@g     @@g     p@g     @@g     @g     @g     L@g     @TrY   )r.   r/   r0   r(   r+   r[   r   r   floatrZ   rC   r   r   r"   rI   r   r)   rO   ru   rv   s   @r7   r~   r~     s    '
"  $/1J1J -C1J "	1J
 41J UO1J. /1Jf1$s) 1 HLC&C@DC	C&S,BS  #S 
.	!	Sr6   r~   c                        e Zd ZdZdZ	 	 ddedededededef fd	Z		 dd
e
deddfdZdededee   fdZd Zed        Zededefd       Zd Zedefd       ZdedefdZ xZS )&_ExponentialBucketHistogramAggregationr-   i @  r:   r;   rc   rd   max_size	max_scalec           
         || j                   k  rt        d| d      || j                  kD  rt        d| d      |dkD  rt        j	                  d|       t
        |   |t        |t        d|                   || _	        || _
        || _        || _        d | _        d | _        t        | _        t         | _        d| _        d| _        d| _        d | _        d | _        d | _        t        | _        t         | _        d| _        d| _        d| _        d | _        | j                  | _        | j?                  | j                        | _         y )	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}   zGmax_scale is set to %s which is larger than the recommended value of 20)sizer   r   )!_min_max_size
ValueError_max_max_size_loggerwarningrg   rC   r   r   ri   rh   	_max_size
_max_scale_value_positive_value_negativer	   r   r   r   _count_zero_count_scale_previous_value_positive_previous_value_negativer   r   r   _previous_count_previous_zero_count_previous_scalerl   _new_mapping_mapping)rB   r:   r;   rc   rd   r   r   rn   s          r7   rC   z/_ExponentialBucketHistogramAggregation.__init__e  so   . d(((#H: .8 8 
 d(((#H: .8 8  r>OO: 	%!B(9 	 	
 / 	0 &:"!###	D		(,%(,% !T $%!#/3/I/I,))$//:r6   rD   rE   rF   Nc                    | j                   5  | j                  t               | _        | j                  t               | _        |j                  }| xj
                  |z  c_        t        | j                  |      | _        t        | j                  |      | _	        | xj                  dz  c_
        |dk(  r?| xj                  dz  c_        | j                  | j                  k(  rd| _        	 d d d        y |dkD  r| j                  }n| }| j                  }| j                  j                  |      }d}d\  }}t        |      dk(  r||_        ||_        ||_        nw||j                   k  r-|j"                  |z
  | j&                  k\  rd}|}|j"                  }n;||j"                  kD  r,||j                   z
  | j&                  k\  rd}|j                   }|}|r| j)                  ||      }	| j+                  |	| j                  | j                         | j-                  | j                  j.                  |	z
        | _        | j                  j                  |      }| j                  j.                  | _        ||j                   k  rN|j"                  |z
  }
|
t        |j0                        k\  r|j3                  |
dz   | j&                         ||_        n\||j"                  kD  rM||j                   z
  }
|
t        |j0                        k\  r|j3                  |
dz   | j&                         ||_        ||j$                  z
  }|dk  r|t        |j0                        z  }|j5                  |       d d d        | j7                  ||       y # 1 sw Y   xY w)Nr,   r   F)r   r   T)r>   r   r   r   rU   r   r   r   r   r   r   r   r   r   map_to_indexr   index_start	index_end
index_baser   _get_scale_change
_downscaler   scalecountsgrowincrement_bucketrX   )rB   rD   rE   r   rU   indexis_rescaling_neededlowhighscale_changespanbucket_indexs               r7   rI   z0_ExponentialBucketHistogramAggregation.aggregate  s    
 ZZ##+'.y$##+'.y$ + 1 1II**IDII'89DIDII'89DIKK1K A%  A% ;;$"2"22"#DK+ Z. !1$,, &7$6!,, MM../@AE"'IC5zQ$)!"'#(  )))__u,?&*# 'U...4>>A&*#''"#55c4@ ((((
 !% 1 1MM'',6! 223DE----DKu(((.3u||,,JJtax8$)!(u0003u||,,JJtax8"' 5#3#33LaELL 11 ""<0Q T 	k+ABU Zs   CN4I2NN
rK   rL   c                 
   | j                   5  | j                  }| j                  }| j                  }| j                  }| j
                  }| j                  }| j                  }	| j                  }
d| _        d| _        d| _        t        | _        t         | _        d| _        d| _        d| _        | j                  t        j                  u r^|t        j                  u r| j                  }|| _        ||
	 ddd       yt        | j                  | j!                         |||||
|	t#        |j$                  |j'                               t#        |j$                  |j'                               d||      cddd       S |+| j(                  t+               }|j-                         | _        |+| j.                  t+               }|j-                         | _        |
)| j0                  | j2                  j4                  }
|
| _        |!| j(                  |j-                         | _        |!| j.                  |j-                         | _        |
| j0                  |
| _        |&| j(                  | j(                  j-                         }|&| j.                  | j.                  j-                         }|
| j0                  | j0                  }
t7        | j0                  |
      }| j9                  | j(                  ||
|      \  }}| j9                  | j.                  ||
|      \  }}t7        || j;                  ||      z
  || j;                  ||      z
        }| j=                  | j0                  |z
  | j(                  | j.                         | j?                  | j(                  ||
||       | j?                  | j.                  ||
||       t7        || j@                        | _         tC        || jD                        | _"        || jF                  z   | _#        || jH                  z   | _$        |	| jJ                  z   | _%        || _        t        | j                  | j!                         | jL                  || jH                  | jF                  | j0                  | jJ                  t#        | j(                  j$                  | j(                  j'                               t#        | j.                  j$                  | j.                  j'                               d| j@                  | jD                        cddd       S 	 ddd       y# 1 sw Y   yxY w)r|   Nr   )offsetr   )r:   rr   rd   rV   r   r   r   
zero_countpositivenegativeflagsr   r   )'r>   r   r   r   r   r   r   r   r   r	   ri   r+   r3   rl   r#   r?   rR   BucketsPointr   get_offset_countsr   r   
copy_emptyr   r   r   r   r   _get_low_high_previous_currentr   r   _merger   r   r   r   r   r   rh   )rB   rK   rL   value_positivevalue_negativer   r   r   r   r   r   rs   	min_scalelow_positivehigh_positivelow_negativehigh_negatives                    r7   rO   z._ExponentialBucketHistogramAggregation.collect)  s"    ZZ!11N!11N99D99D99DKKE))JKKE#'D #'D DIDIDIDK DDK 88)//0 7-334 << 3 . 8 &-.2H#K ZN 9#'#3#3"&"9"9";-K'<# ##-!-#1#8#8+9+K+K+M" ".#1#8#8+9+K+K+M"
    )O ZV #*55= &-YN4B4M4M4OD1"*55=%,YN4B4M4M4OD1=T%9%9%A MM//E+0D( #.55=* 5C4M4M4OD1".55=4B4M4M4OD1$)=)=)E+0D( #*55A%)%B%B%M%M%ON"*55A%)%B%B%M%M%ON=T%9%9%E 00E 4 4e<	 7755&!	 ,m 7755&!	 ,m  ,,\=IJ,,\=IJ	 ((941111  11"6 11"6 &)t/A/A%B"%(t/A/A%B"%)D,>,>%>"',t/C/C'C$!:!:: ) (1$4#//"557)-)C)C#8..**..#88)#<<CC 99KKM *#<<CC 99KKM ****1W ZL M ZZs    C)T4?A5T4>N"T4*T44T=c                     | j                  || j                  |      \  }}| j                  |||      \  }}||kD  r|}	|}
|	|
fS ||kD  r|}	|}
|	|
fS t        ||      }	t        ||      }
|	|
fS r=   )_get_low_highr   r   r   )rB   previous_point_bucketscurrent_point_bucketscurrent_scaler   previous_point_lowprevious_point_highcurrent_point_lowcurrent_point_highr   r   s              r7   r   zE_ExponentialBucketHistogramAggregation._get_low_high_previous_current  s     594F4F"D$8$8)5
1	0 372D2D!=)3
/	. 11$C&D Dy  "55#C%D Dy (*;<C*,>?DDyr6   c                 j    | j                   dgk(  ry||z
  }| j                  |z	  | j                  |z	  fS )Nr   )r   )r   r   r   )bucketsr   r   shifts       r7   r   z4_ExponentialBucketHistogramAggregation._get_low_high7  s>    >>aS 	!""e+W->->%-GGGr6   r   c                 8    | dk  rt        |       S t        |       S rf   )r    r!   )r   s    r7   r   z3_ExponentialBucketHistogramAggregation._new_mapping@  s    A:"5))&&r6   c                 r    d}||z
  | j                   k\  r"|dz	  }|dz	  }|dz  }||z
  | j                   k\  r"|S r   )r   )rB   r   r   changes       r7   r   z8_ExponentialBucketHistogramAggregation._get_scale_changeF  sK    SjDNN*19D(CaKF	 SjDNN* r6   r   c                 t    | dk(  ry | dk  rt        d      |j                  |        |j                  |        y )Nr   zInvalid change of scale)	Exception	downscale)r   r   r   s      r7   r   z1_ExponentialBucketHistogramAggregation._downscaleQ  s;    Q;A:5666"6"r6   previous_bucketscurrent_bucketsc                 N   ||z
  }t        |j                        D ]  \  }}|dk(  r|j                  |z   }	|	|j                  kD  r|	t	        |j                        z  }	|	|z	  }
|
|j
                  k  rg|j                  |
z
  }|| j                  k\  rt        d      |t	        |j                        k\  r|j                  |dz   | j                         |
|_        |
|j                  kD  rg|
|j
                  z
  }|| j                  k\  rt        d      |t	        |j                        k\  r|j                  |dz   | j                         |
|_        |
|j                  z
  }|dk  r|t	        |j                        z  }|t        j                  u r| }|j                  ||        y )Nr   zIncorrect merge scaler,   )	increment)	enumerater   r   r   r   r   r   r   r   r+   r3   r   )rB   r   r   r   r   aggregation_temporalitycurrent_changecurrent_bucket_indexcurrent_bucketcurrent_indexr   r   r   s                r7   r   z-_ExponentialBucketHistogramAggregation._merge]  s    '24=""5
0 . " ,669MMM888_%;%;!<<!^3E'333'11E94>>)#$;<<3/6677$))$(DNNC/4 ,'111/;;;4>>)#$;<<3/6677$))$(DNNC-2 * #3#>#>>La$4$;$; <<&*@*F*FF"0-- . a5
r6      r   rY   )r.   r/   r0   r   r   r(   r   r+   r[   rC   r"   rZ   rI   r   r)   rO   r   staticmethodr   r   r   r   r   r   r   ru   rv   s   @r7   r   r   Y  s-    M M Q;Q; 4Q; -C	Q;
 "Q; Q; Q;h HLoC&oC@DoC	oCbp,Bp  #p 
.	!	pd8 H H 'C 'G ' '
	 	#3 	# 	#<!< !<r6   r   c                   H    e Zd ZdZedededeee	   ge
f   dede	f
d       Zy)	Aggregationz/
    Base class for all aggregation types.
    
instrumentr:   reservoir_factoryrd   rF   c                      y)zCreates an aggregationNr5   rB   r  r:   r  rd   s        r7   _create_aggregationzAggregation._create_aggregation  rJ   r6   N)r.   r/   r0   r1   r   r   r(   r   r   r9   r   r[   r  r5   r6   r7   r   r     sa     	%	% 	% $, "::
		% "	% 
	% 	%r6   r   c            
       >    e Zd ZdZdededeee   ge	f   de
def
dZy)	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
    ==================================================== ====================================
    `opentelemetry.sdk.metrics.Counter`                  `SumAggregation`
    `opentelemetry.sdk.metrics.UpDownCounter`            `SumAggregation`
    `opentelemetry.sdk.metrics.ObservableCounter`        `SumAggregation`
    `opentelemetry.sdk.metrics.ObservableUpDownCounter`  `SumAggregation`
    `opentelemetry.sdk.metrics.Histogram`                `ExplicitBucketHistogramAggregation`
    `opentelemetry.sdk.metrics.ObservableGauge`          `LastValueAggregation`
    ==================================================== ====================================
    r  r:   r  rd   rF   c                    t        |t              r(t        | |t              dt        j                  |      S t        |t
              r(t        | |t              dt        j                  |      S t        |t              r(t        | |t              dt        j                  |      S t        |t              r(t        | |t              dt        j                  |      S t        |t              r't        | |t              t        j                  |      S t        |t              rt        | |t                    S t        |t              rt        | |t                    S t        dt        |       d      )NT)r;   rb   rc   rd   F)r;   rc   rd   r   zInvalid instrument type z found)
isinstancer   ra   r+   r3   r   r   r4   r   r   r~   r   rx   r   r   typer  s        r7   r  z&DefaultAggregation._create_aggregation  sl    j'*""3O"D(,*00%9  j-0""3O"D(-*00%9  j"34""3O"D(,*55%9  j"9:""3O"D(-*55%9  j),6"37# +00%9	 	 j/2("34I"J 
 j&)("34I"J  24
3C2DFKLLr6   Nr.   r/   r0   r1   r   r(   r   r   r9   r   r[   r  r5   r6   r7   r  r    s^    $NMNM NM $, "::
	NM "NM 
NMr6   r  c            
       P    e Zd Z	 	 ddedefdZdededeee	   ge
f   dede	f
d	Zy
)%ExponentialBucketHistogramAggregationr   r   c                      || _         || _        y r=   )r   r   )rB   r   r   s      r7   rC   z.ExponentialBucketHistogramAggregation.__init__  s    
 "#r6   r  r:   r  rd   rF   c                    t         j                  }t        |t              rt         j                  }n t        |t
              rt         j                  }t        | |t              ||| j                  | j                        S )N)r   r   )
r+   r2   r	  r   r3   r   r4   r   r   r   rB   r  r:   r  rd   rc   s         r7   r  z9ExponentialBucketHistogramAggregation._create_aggregation  sn     .D-O-O*j+.1G1M1M.
L1&11 / 6DE. ^^oo
 	
r6   Nr   )r.   r/   r0   r[   rC   r   r(   r   r   r9   r   r  r5   r6   r7   r  r    sl     $$ $

 
 $, "::
	
 "
 

r6   r  c            
       ^    e Zd ZdZ	 	 ddee   deddfdZdede	d	e
ee   gef   d
edef
dZy)"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.
    r   r   rF   Nc                      || _         || _        y r=   )r   r   )rB   r   r   s      r7   rC   z+ExplicitBucketHistogramAggregation.__init__D  s    * &-r6   r  r:   r  rd   c                     t         j                  }t        |t              rt         j                  }n t        |t
              rt         j                  }t        ||| |t              | j                  | j                        S r=   )
r+   r2   r	  r   r3   r   r4   r~   r   r   r  s         r7   r  z6ExplicitBucketHistogramAggregation._create_aggregation\  sr     .D-O-O*j+.1G1M1M.
L1&11 / 3. AB  
 	
r6   r   )r.   r/   r0   r1   r   r   rZ   rC   r   r(   r   r   r9   r   r[   r  r5   r6   r7   r  r  6  s    '
"  $'.UO.& '.( 
).0

 
 $, "::
	
 "
 

r6   r  c            
       >    e Zd ZdZdededeee   ge	f   de
def
dZy)	SumAggregationzbThis aggregation informs the SDK to collect:

    - The arithmetic sum of Measurement values.
    r  r:   r  rd   rF   c           	          t         j                  }t        |t              rt         j                  }n t        |t
              rt         j                  }t        |t        |t        t        f      || |t                    S r=   )
r+   r2   r	  r   r3   r   r4   ra   r   r   r  s         r7   r  z"SumAggregation._create_aggregation}  so     .D-O-O*j+.1G1M1M.
L1&11 / zG->#?@. o.
 	
r6   Nr  r5   r6   r7   r  r  w  sR    


 
 $, "::
	
 "
 

r6   r  c            
       >    e Zd ZdZdededeee   ge	f   de
def
dZy)	LastValueAggregationz
    This aggregation informs the SDK to collect:

    - The last Measurement.
    - The timestamp of the last Measurement.
    r  r:   r  rd   rF   c                 0    t        | |t                     S )Nr   )rx   r  s        r7   r  z(LastValueAggregation._create_aggregation  s     %/0EF
 	
r6   Nr  r5   r6   r7   r  r    sR    

 
 $, "::
	
 "
 

r6   r  c            
       >    e Zd ZdZdededeee   ge	f   de
def
dZy)	DropAggregationz=Using this aggregation will make all measurements be ignored.r  r:   r  rd   rF   c                 .    t        | |t                     S r=   )r]   r  s        r7   r  z#DropAggregation._create_aggregation  s      )*:;
 	
r6   Nr  r5   r6   r7   r  r    sP    G

 
 $, "::
	
 "
 

r6   r  N)Kabcr   r   bisectr   enumr   	functoolsr   loggingr   mathr	   	threadingr
   typingr   r   r   r   r   r   r   opentelemetry.metricsr   r   r   r   r   r   r   r   r   r   ,opentelemetry.sdk.metrics._internal.exemplarr   r   Aopentelemetry.sdk.metrics._internal.exponential_histogram.bucketsr   Aopentelemetry.sdk.metrics._internal.exponential_histogram.mappingr   Ropentelemetry.sdk.metrics._internal.exponential_histogram.mapping.exponent_mappingr    Sopentelemetry.sdk.metrics._internal.exponential_histogram.mapping.logarithm_mappingr!   /opentelemetry.sdk.metrics._internal.measurementr"   )opentelemetry.sdk.metrics._internal.pointr   r#   r$   r%   r&   r'   
GaugePointHistogramPointopentelemetry.util.typesr(   r)   r.   r   r+   r9   r]   ra   rx   r~   r   r   r  r  r  r  r  r  r5   r6   r7   <module>r1     s`  " $       M M M   H M  J 0)?<NO
H
	W 	73/ 7t| nl3' nb'
L4 '
T],~*F ]B@	\.-I @	F%# %$aM aMH!
K !
H>
 >
B
[ 
@
; 
.
k 
r6   