Skip to content

Commit 4306280

Browse files
rbtz-openaiocelotl
authored andcommitted
Add to_json method to ExponentialHistogram (open-telemetry#3780)
1 parent aeb165e commit 4306280

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
([#3633](https://github.com/open-telemetry/opentelemetry-python/pull/3633))
1616
- Fix python 3.12 deprecation warning
1717
([#3751](https://github.com/open-telemetry/opentelemetry-python/pull/3751))
18+
- Add to_json method to ExponentialHistogram
19+
([#3780](https://github.com/open-telemetry/opentelemetry-python/pull/3780))
20+
- bump mypy to 0.982
21+
([#3776](https://github.com/open-telemetry/opentelemetry-python/pull/3776))
1822
- Add support for OTEL_SDK_DISABLED environment variable
1923
([#3648](https://github.com/open-telemetry/opentelemetry-python/pull/3648))
2024
- Fix ValueError message for PeriodicExportingMetricsReader

opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/point.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,18 @@ class ExponentialHistogram:
101101
"opentelemetry.sdk.metrics.export.AggregationTemporality"
102102
)
103103

104+
def to_json(self, indent=4) -> str:
105+
return dumps(
106+
{
107+
"data_points": [
108+
loads(data_point.to_json(indent=indent))
109+
for data_point in self.data_points
110+
],
111+
"aggregation_temporality": self.aggregation_temporality,
112+
},
113+
indent=indent,
114+
)
115+
104116

105117
@dataclass(frozen=True)
106118
class Sum:

opentelemetry-sdk/tests/metrics/test_point.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
from opentelemetry.sdk.metrics.export import (
1818
AggregationTemporality,
19+
Buckets,
20+
ExponentialHistogram,
21+
ExponentialHistogramDataPoint,
1922
Gauge,
2023
Histogram,
2124
HistogramDataPoint,
@@ -100,6 +103,22 @@ def setUpClass(cls):
100103
)
101104
cls.histogram_data_point_1_str = f'{{"attributes": {cls.attributes_1_str}, "start_time_unix_nano": 2, "time_unix_nano": 3, "count": 4, "sum": 4.4, "bucket_counts": [2, 1, 1], "explicit_bounds": [1.2, 2.3, 3.4, 4.5], "min": 0.3, "max": 4.4}}'
102105

106+
cls.exp_histogram_data_point_0 = ExponentialHistogramDataPoint(
107+
attributes=cls.attributes_0,
108+
start_time_unix_nano=1,
109+
time_unix_nano=2,
110+
count=1,
111+
sum=10,
112+
scale=1,
113+
zero_count=0,
114+
positive=Buckets(offset=0, bucket_counts=[1]),
115+
negative=Buckets(offset=0, bucket_counts=[0]),
116+
flags=0,
117+
min=10,
118+
max=10,
119+
)
120+
cls.exp_histogram_data_point_0_str = f'{{"attributes": {cls.attributes_0_str}, "start_time_unix_nano": 1, "time_unix_nano": 2, "count": 1, "sum": 10, "scale": 1, "zero_count": 0, "positive": {{"offset": 0, "bucket_counts": [1]}}, "negative": {{"offset": 0, "bucket_counts": [0]}}, "flags": 0, "min": 10, "max": 10}}'
121+
103122
cls.sum_0 = Sum(
104123
data_points=[cls.number_data_point_0, cls.number_data_point_1],
105124
aggregation_temporality=AggregationTemporality.DELTA,
@@ -121,6 +140,14 @@ def setUpClass(cls):
121140
)
122141
cls.histogram_0_str = f'{{"data_points": [{cls.histogram_data_point_0_str}, {cls.histogram_data_point_1_str}], "aggregation_temporality": 1}}'
123142

143+
cls.exp_histogram_0 = ExponentialHistogram(
144+
data_points=[
145+
cls.exp_histogram_data_point_0,
146+
],
147+
aggregation_temporality=AggregationTemporality.CUMULATIVE,
148+
)
149+
cls.exp_histogram_0_str = f'{{"data_points": [{cls.exp_histogram_data_point_0_str}], "aggregation_temporality": 2}}'
150+
124151
cls.metric_0 = Metric(
125152
name="metric_0",
126153
description="description_0",
@@ -209,6 +236,15 @@ def test_histogram_data_point(self):
209236
self.histogram_data_point_1_str,
210237
)
211238

239+
def test_exp_histogram_data_point(self):
240+
241+
self.maxDiff = None
242+
243+
self.assertEqual(
244+
self.exp_histogram_data_point_0.to_json(indent=None),
245+
self.exp_histogram_data_point_0_str,
246+
)
247+
212248
def test_sum(self):
213249

214250
self.assertEqual(self.sum_0.to_json(indent=None), self.sum_0_str)
@@ -225,6 +261,14 @@ def test_histogram(self):
225261
self.histogram_0.to_json(indent=None), self.histogram_0_str
226262
)
227263

264+
def test_exp_histogram(self):
265+
266+
self.maxDiff = None
267+
268+
self.assertEqual(
269+
self.exp_histogram_0.to_json(indent=None), self.exp_histogram_0_str
270+
)
271+
228272
def test_metric(self):
229273

230274
self.assertEqual(self.metric_0.to_json(indent=None), self.metric_0_str)

0 commit comments

Comments
 (0)