Skip to content

Commit

Permalink
Add to_json method to ExponentialHistogram (#3780)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbtz-openai authored Apr 2, 2024
1 parent cb800f0 commit 5ff9046
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
44 changes: 44 additions & 0 deletions opentelemetry-sdk/tests/metrics/test_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

from opentelemetry.sdk.metrics.export import (
AggregationTemporality,
Buckets,
ExponentialHistogram,
ExponentialHistogramDataPoint,
Gauge,
Histogram,
HistogramDataPoint,
Expand Down Expand Up @@ -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,
Expand All @@ -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",
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 5ff9046

Please sign in to comment.