17
17
from logging import getLogger
18
18
from math import inf
19
19
from threading import Lock
20
- from typing import Generic , Optional , Sequence , TypeVar
20
+ from typing import Generic , List , Optional , Sequence , TypeVar
21
21
22
22
from opentelemetry .sdk ._metrics .measurement import Measurement
23
23
from opentelemetry .sdk ._metrics .point import (
@@ -141,30 +141,32 @@ def collect(self) -> Optional[Gauge]:
141
141
class ExplicitBucketHistogramAggregation (Aggregation [Histogram ]):
142
142
def __init__ (
143
143
self ,
144
- boundaries : Sequence [int ] = (
145
- 0 ,
146
- 5 ,
147
- 10 ,
148
- 25 ,
149
- 50 ,
150
- 75 ,
151
- 100 ,
152
- 250 ,
153
- 500 ,
154
- 1000 ,
144
+ boundaries : Sequence [float ] = (
145
+ 0.0 ,
146
+ 5.0 ,
147
+ 10.0 ,
148
+ 25.0 ,
149
+ 50.0 ,
150
+ 75.0 ,
151
+ 100.0 ,
152
+ 250.0 ,
153
+ 500.0 ,
154
+ 1000.0 ,
155
155
),
156
156
record_min_max : bool = True ,
157
157
):
158
158
super ().__init__ ()
159
- # pylint: disable=unnecessary-comprehension
160
- self ._boundaries = [boundary for boundary in (* boundaries , inf )]
161
- self .value = [0 for _ in range (len (self ._boundaries ))]
159
+ self ._boundaries = tuple (boundaries )
160
+ self ._bucket_counts = self ._get_empty_bucket_counts ()
162
161
self ._min = inf
163
162
self ._max = - inf
164
163
self ._sum = 0
165
164
self ._record_min_max = record_min_max
166
165
self ._start_time_unix_nano = _time_ns ()
167
166
167
+ def _get_empty_bucket_counts (self ) -> List [int ]:
168
+ return [0 ] * (len (self ._boundaries ) + 1 )
169
+
168
170
def aggregate (self , measurement : Measurement ) -> None :
169
171
170
172
value = measurement .value
@@ -175,7 +177,7 @@ def aggregate(self, measurement: Measurement) -> None:
175
177
176
178
self ._sum += value
177
179
178
- self .value [bisect_left (self ._boundaries , value )] += 1
180
+ self ._bucket_counts [bisect_left (self ._boundaries , value )] += 1
179
181
180
182
def collect (self ) -> Optional [Histogram ]:
181
183
"""
@@ -184,10 +186,10 @@ def collect(self) -> Optional[Histogram]:
184
186
now = _time_ns ()
185
187
186
188
with self ._lock :
187
- value = self .value
189
+ value = self ._bucket_counts
188
190
start_time_unix_nano = self ._start_time_unix_nano
189
191
190
- self .value = [ 0 for _ in range ( len ( self ._boundaries ))]
192
+ self ._bucket_counts = self ._get_empty_bucket_counts ()
191
193
self ._start_time_unix_nano = now + 1
192
194
193
195
return Histogram (
0 commit comments