Skip to content

Commit 61ebe89

Browse files
committed
Move exponential histogram tests from integration to unit
1 parent 4aecf46 commit 61ebe89

File tree

2 files changed

+47
-55
lines changed

2 files changed

+47
-55
lines changed

opentelemetry-sdk/tests/metrics/integration_test/test_console_exporter.py

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,12 @@
1616
from json import loads
1717
from unittest import TestCase
1818

19-
from opentelemetry.metrics import Histogram, get_meter, set_meter_provider
19+
from opentelemetry.metrics import get_meter, set_meter_provider
2020
from opentelemetry.sdk.metrics import MeterProvider
2121
from opentelemetry.sdk.metrics.export import (
2222
ConsoleMetricExporter,
2323
PeriodicExportingMetricReader,
2424
)
25-
from opentelemetry.sdk.metrics.view import (
26-
ExponentialBucketHistogramAggregation,
27-
View,
28-
)
2925
from opentelemetry.test.globals_test import reset_metrics_globals
3026

3127

@@ -77,56 +73,6 @@ def test_console_exporter(self):
7773
self.assertEqual(metrics["attributes"], {"a": "b"})
7874
self.assertEqual(metrics["value"], 1)
7975

80-
def test_exp_histogram_exporter(self):
81-
output = StringIO()
82-
exporter = ConsoleMetricExporter(out=output)
83-
reader = PeriodicExportingMetricReader(
84-
exporter, export_interval_millis=100
85-
)
86-
provider = MeterProvider(
87-
metric_readers=[reader],
88-
views=[
89-
View(
90-
instrument_type=Histogram,
91-
aggregation=ExponentialBucketHistogramAggregation(),
92-
),
93-
],
94-
)
95-
set_meter_provider(provider)
96-
meter = get_meter(__name__)
97-
hist = meter.create_histogram(
98-
"name", description="description", unit="unit"
99-
)
100-
hist.record(1, attributes={"a": "b"})
101-
provider.shutdown()
102-
103-
output.seek(0)
104-
result_0 = loads("".join(output.readlines()))
105-
106-
self.assertGreater(len(result_0), 0)
107-
108-
metrics = result_0["resource_metrics"][0]["scope_metrics"][0]
109-
110-
self.assertEqual(metrics["scope"]["name"], "test_console_exporter")
111-
112-
metrics = metrics["metrics"][0]
113-
114-
self.assertEqual(metrics["name"], "name")
115-
self.assertEqual(metrics["description"], "description")
116-
self.assertEqual(metrics["unit"], "unit")
117-
118-
metrics = metrics["data"]
119-
120-
self.assertEqual(metrics["aggregation_temporality"], 2)
121-
self.assertEqual(len(metrics["data_points"]), 1)
122-
123-
metrics = metrics["data_points"][0]
124-
125-
self.assertEqual(metrics["attributes"], {"a": "b"})
126-
self.assertEqual(metrics["count"], 1)
127-
self.assertEqual(metrics["sum"], 1)
128-
self.assertEqual(metrics["zero_count"], 0)
129-
13076
def test_console_exporter_no_export(self):
13177

13278
output = StringIO()

opentelemetry-sdk/tests/metrics/test_point.py

Lines changed: 46 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,23 @@ 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+
122+
103123
cls.sum_0 = Sum(
104124
data_points=[cls.number_data_point_0, cls.number_data_point_1],
105125
aggregation_temporality=AggregationTemporality.DELTA,
@@ -121,6 +141,15 @@ def setUpClass(cls):
121141
)
122142
cls.histogram_0_str = f'{{"data_points": [{cls.histogram_data_point_0_str}, {cls.histogram_data_point_1_str}], "aggregation_temporality": 1}}'
123143

144+
cls.exp_histogram_0 = ExponentialHistogram(
145+
data_points=[
146+
cls.exp_histogram_data_point_0,
147+
],
148+
aggregation_temporality=AggregationTemporality.CUMULATIVE,
149+
)
150+
cls.exp_histogram_0_str = f'{{"data_points": [{cls.exp_histogram_data_point_0_str}], "aggregation_temporality": 2}}'
151+
152+
124153
cls.metric_0 = Metric(
125154
name="metric_0",
126155
description="description_0",
@@ -209,6 +238,15 @@ def test_histogram_data_point(self):
209238
self.histogram_data_point_1_str,
210239
)
211240

241+
def test_exp_histogram_data_point(self):
242+
243+
self.maxDiff = None
244+
245+
self.assertEqual(
246+
self.exp_histogram_data_point_0.to_json(indent=None),
247+
self.exp_histogram_data_point_0_str,
248+
)
249+
212250
def test_sum(self):
213251

214252
self.assertEqual(self.sum_0.to_json(indent=None), self.sum_0_str)
@@ -225,6 +263,14 @@ def test_histogram(self):
225263
self.histogram_0.to_json(indent=None), self.histogram_0_str
226264
)
227265

266+
def test_exp_histogram(self):
267+
268+
self.maxDiff = None
269+
270+
self.assertEqual(
271+
self.exp_histogram_0.to_json(indent=None), self.exp_histogram_0_str
272+
)
273+
228274
def test_metric(self):
229275

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

0 commit comments

Comments
 (0)