Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 290d7b5

Browse files
aabmassAlex Boten
andauthored
Reset async instrumentation aggregations each collection interval (open-telemetry#2510)
Co-authored-by: Alex Boten <[email protected]>
1 parent 5347727 commit 290d7b5

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

opentelemetry-sdk/src/opentelemetry/sdk/_metrics/aggregation.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,18 @@ def collect(self) -> Optional[Sum]:
101101
value=value,
102102
)
103103

104-
if self._value is None:
105-
return None
104+
with self._lock:
105+
if self._value is None:
106+
return None
107+
value = self._value
108+
self._value = None
106109

107110
return Sum(
108111
aggregation_temporality=AggregationTemporality.CUMULATIVE,
109112
is_monotonic=self._instrument_is_monotonic,
110113
start_time_unix_nano=self._start_time_unix_nano,
111114
time_unix_nano=now,
112-
value=self._value,
115+
value=value,
113116
)
114117

115118

@@ -126,12 +129,15 @@ def collect(self) -> Optional[Gauge]:
126129
"""
127130
Atomically return a point for the current value of the metric.
128131
"""
129-
if self._value is None:
130-
return None
132+
with self._lock:
133+
if self._value is None:
134+
return None
135+
value = self._value
136+
self._value = None
131137

132138
return Gauge(
133139
time_unix_nano=_time_ns(),
134-
value=self._value,
140+
value=value,
135141
)
136142

137143

opentelemetry-sdk/tests/metrics/test_aggregation.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,19 +133,20 @@ def test_collect_cumulative(self):
133133
self.assertEqual(first_sum.value, 1)
134134
self.assertTrue(first_sum.is_monotonic)
135135

136+
# should have been reset after first collect
136137
sum_aggregation.aggregate(measurement(1))
137138
second_sum = sum_aggregation.collect()
138139

139-
self.assertEqual(second_sum.value, 2)
140+
self.assertEqual(second_sum.value, 1)
140141
self.assertTrue(second_sum.is_monotonic)
141142

142143
self.assertEqual(
143144
second_sum.start_time_unix_nano, first_sum.start_time_unix_nano
144145
)
145146

146-
self.assertIsNone(
147-
_SumAggregation(True, AggregationTemporality.CUMULATIVE).collect()
148-
)
147+
# if no point seen for a whole interval, should return None
148+
third_sum = sum_aggregation.collect()
149+
self.assertIsNone(third_sum)
149150

150151

151152
class TestLastValueAggregation(TestCase):
@@ -194,6 +195,10 @@ def test_collect(self):
194195
second_gauge.time_unix_nano, first_gauge.time_unix_nano
195196
)
196197

198+
# if no observation seen for the interval, it should return None
199+
third_gauge = last_value_aggregation.collect()
200+
self.assertIsNone(third_gauge)
201+
197202

198203
class TestExplicitBucketHistogramAggregation(TestCase):
199204
def test_aggregate(self):

0 commit comments

Comments
 (0)