Skip to content

Commit 8559504

Browse files
committed
Add test for filtered attributes through View
1 parent 627da87 commit 8559504

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

opentelemetry-sdk/tests/metrics/test_view_instrument_match.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,52 @@ def test_consume_measurement_with_exemplars(self):
615615

616616
self.assertIn(data_points[0].exemplars[0].value, [4.0, 5.0])
617617

618+
def test_consume_measurement_with_exemplars_and_view_attributes_filter(
619+
self,
620+
):
621+
value = 22
622+
# Create an instance of _Counter
623+
instrument1 = _Counter(
624+
name="instrument1",
625+
instrumentation_scope=None, # No mock, set to None or actual scope if available
626+
measurement_consumer=None, # No mock, set to None or actual consumer if available
627+
)
628+
629+
view_instrument_match = _ViewInstrumentMatch(
630+
view=View(
631+
instrument_name="instrument1",
632+
name="name",
633+
attribute_keys={"X", "Y"},
634+
),
635+
instrument=instrument1,
636+
instrument_class_aggregation={_Counter: DefaultAggregation()},
637+
)
638+
639+
view_instrument_match.consume_measurement(
640+
Measurement(
641+
value=value,
642+
time_unix_nano=time_ns(),
643+
instrument=instrument1,
644+
context=Context(),
645+
attributes={"X": "x-value", "Y": "y-value", "Z": "z-value"},
646+
)
647+
)
648+
649+
# Collect the data points
650+
data_points = list(
651+
view_instrument_match.collect(AggregationTemporality.CUMULATIVE, 0)
652+
)
653+
654+
# Ensure only one data point is collected
655+
self.assertEqual(len(data_points), 1)
656+
657+
# Verify that exemplars have been correctly stored and collected
658+
self.assertEqual(len(data_points[0].exemplars), 1)
659+
660+
# Check the exemplar has the dropped attribute
661+
exemplar = list(data_points[0].exemplars)[0]
662+
self.assertEqual(exemplar.value, value)
663+
self.assertDictEqual(exemplar.filtered_attributes, {"Z": "z-value"})
618664

619665

620666
class TestAlignedHistogramBucketExemplarReservoir(TestCase):

0 commit comments

Comments
 (0)