Skip to content

Commit 193aa0a

Browse files
committed
Add another test for histograms
1 parent c06dbc3 commit 193aa0a

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ def collect(
140140
Atomically return a point for the current value of the metric and
141141
reset the aggregation value.
142142
"""
143+
143144
if self._instrument_temporality is AggregationTemporality.DELTA:
144145

145146
with self._lock:
@@ -289,6 +290,7 @@ def collect(
289290
Atomically return a point for the current value of the metric.
290291
"""
291292
with self._lock:
293+
292294
if not any(self._bucket_counts):
293295
return None
294296

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from unittest import TestCase
16+
17+
from opentelemetry.metrics import set_meter_provider
18+
from opentelemetry.sdk.metrics import MeterProvider
19+
from opentelemetry.sdk.metrics.export import InMemoryMetricReader
20+
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
21+
22+
in_memory_metric_reader = InMemoryMetricReader()
23+
24+
provider = MeterProvider(
25+
resource=Resource.create({SERVICE_NAME: "otel-test"}),
26+
metric_readers=[in_memory_metric_reader],
27+
)
28+
set_meter_provider(provider)
29+
30+
meter = provider.get_meter("my-meter")
31+
32+
histogram = meter.create_histogram("my_histogram")
33+
counter = meter.create_counter("my_counter")
34+
35+
36+
class TestHistogramExport(TestCase):
37+
def test_histogram_counter_collection(self):
38+
39+
histogram.record(5, {"attribute": "value"})
40+
counter.add(1, {"attribute": "value_counter"})
41+
42+
metric_data = in_memory_metric_reader.get_metrics_data()
43+
44+
self.assertEqual(
45+
len(metric_data.resource_metrics[0].scope_metrics[0].metrics), 2
46+
)
47+
48+
self.assertEqual(
49+
(
50+
metric_data.resource_metrics[0]
51+
.scope_metrics[0]
52+
.metrics[0]
53+
.data.data_points[0]
54+
.bucket_counts
55+
),
56+
(0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
57+
)
58+
self.assertEqual(
59+
(
60+
metric_data.resource_metrics[0]
61+
.scope_metrics[0]
62+
.metrics[1]
63+
.data.data_points[0]
64+
.value
65+
),
66+
1,
67+
)
68+
69+
metric_data = in_memory_metric_reader.get_metrics_data()
70+
71+
# FIXME ExplicitBucketHistogramAggregation is resetting counts to zero
72+
# even if aggregation temporality is cumulative.
73+
self.assertEqual(
74+
len(metric_data.resource_metrics[0].scope_metrics[0].metrics), 1
75+
)
76+
self.assertEqual(
77+
(
78+
metric_data.resource_metrics[0]
79+
.scope_metrics[0]
80+
.metrics[0]
81+
.data.data_points[0]
82+
.value
83+
),
84+
1,
85+
)

0 commit comments

Comments
 (0)