Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7d076cc

Browse files
aabmassocelotl
andauthored
Fix a few ExplicitBucketHistogram issues (open-telemetry#2377)
* Fix a few ExplicitBucketHistogram issues * make default boundaries floats * Update helper func name Co-authored-by: Diego Hurtado <[email protected]>
1 parent 297b67e commit 7d076cc

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

opentelemetry-sdk/src/opentelemetry/sdk/_metrics/aggregation.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from logging import getLogger
1818
from math import inf
1919
from threading import Lock
20-
from typing import Generic, Optional, Sequence, TypeVar
20+
from typing import Generic, List, Optional, Sequence, TypeVar
2121

2222
from opentelemetry.sdk._metrics.measurement import Measurement
2323
from opentelemetry.sdk._metrics.point import (
@@ -141,30 +141,32 @@ def collect(self) -> Optional[Gauge]:
141141
class ExplicitBucketHistogramAggregation(Aggregation[Histogram]):
142142
def __init__(
143143
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,
155155
),
156156
record_min_max: bool = True,
157157
):
158158
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()
162161
self._min = inf
163162
self._max = -inf
164163
self._sum = 0
165164
self._record_min_max = record_min_max
166165
self._start_time_unix_nano = _time_ns()
167166

167+
def _get_empty_bucket_counts(self) -> List[int]:
168+
return [0] * (len(self._boundaries) + 1)
169+
168170
def aggregate(self, measurement: Measurement) -> None:
169171

170172
value = measurement.value
@@ -175,7 +177,7 @@ def aggregate(self, measurement: Measurement) -> None:
175177

176178
self._sum += value
177179

178-
self.value[bisect_left(self._boundaries, value)] += 1
180+
self._bucket_counts[bisect_left(self._boundaries, value)] += 1
179181

180182
def collect(self) -> Optional[Histogram]:
181183
"""
@@ -184,10 +186,10 @@ def collect(self) -> Optional[Histogram]:
184186
now = _time_ns()
185187

186188
with self._lock:
187-
value = self.value
189+
value = self._bucket_counts
188190
start_time_unix_nano = self._start_time_unix_nano
189191

190-
self.value = [0 for _ in range(len(self._boundaries))]
192+
self._bucket_counts = self._get_empty_bucket_counts()
191193
self._start_time_unix_nano = now + 1
192194

193195
return Histogram(

opentelemetry-sdk/tests/metrics/test_aggregation.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,16 +232,24 @@ def test_aggregate(self):
232232
explicit_bucket_histogram_aggregation.aggregate(Measurement(5))
233233

234234
# The first bucket keeps count of values between (-inf, 0] (-1 and 0)
235-
self.assertEqual(explicit_bucket_histogram_aggregation.value[0], 2)
235+
self.assertEqual(
236+
explicit_bucket_histogram_aggregation._bucket_counts[0], 2
237+
)
236238

237239
# The second bucket keeps count of values between (0, 2] (1 and 2)
238-
self.assertEqual(explicit_bucket_histogram_aggregation.value[1], 2)
240+
self.assertEqual(
241+
explicit_bucket_histogram_aggregation._bucket_counts[1], 2
242+
)
239243

240244
# The third bucket keeps count of values between (2, 4] (3 and 4)
241-
self.assertEqual(explicit_bucket_histogram_aggregation.value[2], 2)
245+
self.assertEqual(
246+
explicit_bucket_histogram_aggregation._bucket_counts[2], 2
247+
)
242248

243249
# The fourth bucket keeps count of values between (4, inf) (3 and 4)
244-
self.assertEqual(explicit_bucket_histogram_aggregation.value[3], 1)
250+
self.assertEqual(
251+
explicit_bucket_histogram_aggregation._bucket_counts[3], 1
252+
)
245253

246254
def test_min_max(self):
247255
"""

0 commit comments

Comments
 (0)