diff --git a/CHANGELOG.md b/CHANGELOG.md index 585524a6f85..190fbc489a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#3633](https://github.com/open-telemetry/opentelemetry-python/pull/3633)) - Fix python 3.12 deprecation warning ([#3751](https://github.com/open-telemetry/opentelemetry-python/pull/3751)) +- Add to_json method to ExponentialHistogram + ([#3780](https://github.com/open-telemetry/opentelemetry-python/pull/3780)) +- bump mypy to 0.982 + ([#3776](https://github.com/open-telemetry/opentelemetry-python/pull/3776)) - Add support for OTEL_SDK_DISABLED environment variable ([#3648](https://github.com/open-telemetry/opentelemetry-python/pull/3648)) - Fix ValueError message for PeriodicExportingMetricsReader diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/point.py b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/point.py index c30705c59a4..42420b9008e 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/point.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/point.py @@ -101,6 +101,18 @@ class ExponentialHistogram: "opentelemetry.sdk.metrics.export.AggregationTemporality" ) + def to_json(self, indent=4) -> str: + return dumps( + { + "data_points": [ + loads(data_point.to_json(indent=indent)) + for data_point in self.data_points + ], + "aggregation_temporality": self.aggregation_temporality, + }, + indent=indent, + ) + @dataclass(frozen=True) class Sum: diff --git a/opentelemetry-sdk/tests/metrics/test_point.py b/opentelemetry-sdk/tests/metrics/test_point.py index 5d6640fdea6..20dd0e72384 100644 --- a/opentelemetry-sdk/tests/metrics/test_point.py +++ b/opentelemetry-sdk/tests/metrics/test_point.py @@ -16,6 +16,9 @@ from opentelemetry.sdk.metrics.export import ( AggregationTemporality, + Buckets, + ExponentialHistogram, + ExponentialHistogramDataPoint, Gauge, Histogram, HistogramDataPoint, @@ -100,6 +103,22 @@ def setUpClass(cls): ) 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}}' + cls.exp_histogram_data_point_0 = ExponentialHistogramDataPoint( + attributes=cls.attributes_0, + start_time_unix_nano=1, + time_unix_nano=2, + count=1, + sum=10, + scale=1, + zero_count=0, + positive=Buckets(offset=0, bucket_counts=[1]), + negative=Buckets(offset=0, bucket_counts=[0]), + flags=0, + min=10, + max=10, + ) + 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}}' + cls.sum_0 = Sum( data_points=[cls.number_data_point_0, cls.number_data_point_1], aggregation_temporality=AggregationTemporality.DELTA, @@ -121,6 +140,14 @@ def setUpClass(cls): ) cls.histogram_0_str = f'{{"data_points": [{cls.histogram_data_point_0_str}, {cls.histogram_data_point_1_str}], "aggregation_temporality": 1}}' + cls.exp_histogram_0 = ExponentialHistogram( + data_points=[ + cls.exp_histogram_data_point_0, + ], + aggregation_temporality=AggregationTemporality.CUMULATIVE, + ) + cls.exp_histogram_0_str = f'{{"data_points": [{cls.exp_histogram_data_point_0_str}], "aggregation_temporality": 2}}' + cls.metric_0 = Metric( name="metric_0", description="description_0", @@ -209,6 +236,15 @@ def test_histogram_data_point(self): self.histogram_data_point_1_str, ) + def test_exp_histogram_data_point(self): + + self.maxDiff = None + + self.assertEqual( + self.exp_histogram_data_point_0.to_json(indent=None), + self.exp_histogram_data_point_0_str, + ) + def test_sum(self): self.assertEqual(self.sum_0.to_json(indent=None), self.sum_0_str) @@ -225,6 +261,14 @@ def test_histogram(self): self.histogram_0.to_json(indent=None), self.histogram_0_str ) + def test_exp_histogram(self): + + self.maxDiff = None + + self.assertEqual( + self.exp_histogram_0.to_json(indent=None), self.exp_histogram_0_str + ) + def test_metric(self): self.assertEqual(self.metric_0.to_json(indent=None), self.metric_0_str)