Skip to content

Commit 6aee819

Browse files
authored
Check data point before yielding (#2745)
1 parent 953e422 commit 6aee819

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.12.0rc1-0.31b0...HEAD)
99

10+
- Fix yield of `None`-valued points
11+
([#2745](https://github.com/open-telemetry/opentelemetry-python/pull/2745))
1012
- Add missing `to_json` methods
1113
([#2722](https://github.com/open-telemetry/opentelemetry-python/pull/2722)
1214
- Fix type hints for textmap `Getter` and `Setter`

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ def collect(
130130

131131
with self._lock:
132132
for aggregation in self._attributes_aggregation.values():
133-
yield aggregation.collect(
133+
data_point = aggregation.collect(
134134
aggregation_temporality, collection_start_nanos
135135
)
136+
if data_point is not None:
137+
yield data_point

opentelemetry-sdk/tests/metrics/test_view_instrument_match.py

+72
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,78 @@ def test_collect(self):
212212
self.assertEqual(number_data_point.attributes, {"c": "d"})
213213
self.assertEqual(number_data_point.value, 0)
214214

215+
def test_data_point_check(self):
216+
instrument1 = Counter(
217+
"instrument1",
218+
Mock(),
219+
Mock(),
220+
description="description",
221+
unit="unit",
222+
)
223+
instrument1.instrumentation_scope = self.mock_instrumentation_scope
224+
225+
view_instrument_match = _ViewInstrumentMatch(
226+
view=View(
227+
instrument_name="instrument1",
228+
name="name",
229+
aggregation=DefaultAggregation(),
230+
),
231+
instrument=instrument1,
232+
instrument_class_aggregation=MagicMock(
233+
**{
234+
"__getitem__.return_value": Mock(
235+
**{
236+
"_create_aggregation.return_value": Mock(
237+
**{
238+
"collect.side_effect": [
239+
Mock(),
240+
Mock(),
241+
None,
242+
Mock(),
243+
]
244+
}
245+
)
246+
}
247+
)
248+
}
249+
),
250+
)
251+
252+
view_instrument_match.consume_measurement(
253+
Measurement(
254+
value=0,
255+
instrument=Mock(name="instrument1"),
256+
attributes={"c": "d", "f": "g"},
257+
)
258+
)
259+
view_instrument_match.consume_measurement(
260+
Measurement(
261+
value=0,
262+
instrument=Mock(name="instrument1"),
263+
attributes={"h": "i", "j": "k"},
264+
)
265+
)
266+
view_instrument_match.consume_measurement(
267+
Measurement(
268+
value=0,
269+
instrument=Mock(name="instrument1"),
270+
attributes={"l": "m", "n": "o"},
271+
)
272+
)
273+
view_instrument_match.consume_measurement(
274+
Measurement(
275+
value=0,
276+
instrument=Mock(name="instrument1"),
277+
attributes={"p": "q", "r": "s"},
278+
)
279+
)
280+
281+
result = view_instrument_match.collect(
282+
AggregationTemporality.CUMULATIVE, 0
283+
)
284+
285+
self.assertEqual(len(list(result)), 3)
286+
215287
def test_setting_aggregation(self):
216288
instrument1 = Counter(
217289
name="instrument1",

0 commit comments

Comments
 (0)