From d23328c9b3d555bea45423275a2d3c4e43dff206 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Tue, 20 Aug 2024 16:41:06 -0600 Subject: [PATCH] Use current time when creating an aggregation in _ViewInstrumentMatch --- .../_internal/_view_instrument_match.py | 5 ++--- .../integration_test/test_time_align.py | 12 +++++------ .../metrics/test_view_instrument_match.py | 21 +++---------------- 3 files changed, 11 insertions(+), 27 deletions(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/_view_instrument_match.py b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/_view_instrument_match.py index 7dd7f58f272..cba09696129 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/_view_instrument_match.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/_view_instrument_match.py @@ -40,7 +40,6 @@ def __init__( instrument: Instrument, instrument_class_aggregation: Dict[type, Aggregation], ): - self._start_time_unix_nano = time_ns() self._view = view self._instrument = instrument self._attributes_aggregation: Dict[frozenset, _Aggregation] = {} @@ -107,7 +106,7 @@ def consume_measurement(self, measurement: Measurement) -> None: self._view._aggregation._create_aggregation( self._instrument, attributes, - self._start_time_unix_nano, + time_ns(), ) ) else: @@ -116,7 +115,7 @@ def consume_measurement(self, measurement: Measurement) -> None: ]._create_aggregation( self._instrument, attributes, - self._start_time_unix_nano, + time_ns(), ) self._attributes_aggregation[aggr_key] = aggregation diff --git a/opentelemetry-sdk/tests/metrics/integration_test/test_time_align.py b/opentelemetry-sdk/tests/metrics/integration_test/test_time_align.py index ad34f5622ff..a5b71dd4782 100644 --- a/opentelemetry-sdk/tests/metrics/integration_test/test_time_align.py +++ b/opentelemetry-sdk/tests/metrics/integration_test/test_time_align.py @@ -56,11 +56,11 @@ def test_time_align_cumulative(self): .data.data_points ) - self.assertEqual( + self.assertLess( data_points_0_0[0].start_time_unix_nano, data_points_0_0[1].start_time_unix_nano, ) - self.assertEqual( + self.assertLess( data_points_0_1[0].start_time_unix_nano, data_points_0_1[1].start_time_unix_nano, ) @@ -103,11 +103,11 @@ def test_time_align_cumulative(self): .data.data_points ) - self.assertEqual( + self.assertLess( data_points_1_0[0].start_time_unix_nano, data_points_1_0[1].start_time_unix_nano, ) - self.assertEqual( + self.assertLess( data_points_1_1[0].start_time_unix_nano, data_points_1_1[1].start_time_unix_nano, ) @@ -181,11 +181,11 @@ def test_time_align_delta(self): .data.data_points ) - self.assertEqual( + self.assertLess( data_points_0_0[0].start_time_unix_nano, data_points_0_0[1].start_time_unix_nano, ) - self.assertEqual( + self.assertLess( data_points_0_1[0].start_time_unix_nano, data_points_0_1[1].start_time_unix_nano, ) diff --git a/opentelemetry-sdk/tests/metrics/test_view_instrument_match.py b/opentelemetry-sdk/tests/metrics/test_view_instrument_match.py index d76e4a7c282..20e4355593a 100644 --- a/opentelemetry-sdk/tests/metrics/test_view_instrument_match.py +++ b/opentelemetry-sdk/tests/metrics/test_view_instrument_match.py @@ -217,7 +217,7 @@ def test_collect(self): @patch( "opentelemetry.sdk.metrics._internal._view_instrument_match.time_ns", - side_effect=[0, time_ns()], + side_effect=[0, 1, 2], ) def test_collect_resets_start_time_unix_nano(self, mock_time_ns): instrument = Mock(name="instrument") @@ -234,10 +234,7 @@ def test_collect_resets_start_time_unix_nano(self, mock_time_ns): ), ) start_time_unix_nano = 0 - self.assertEqual(mock_time_ns.call_count, 1) - self.assertEqual( - view_instrument_match._start_time_unix_nano, start_time_unix_nano - ) + self.assertEqual(mock_time_ns.call_count, 0) # +1 call to _create_aggregation view_instrument_match.consume_measurement( @@ -254,10 +251,6 @@ def test_collect_resets_start_time_unix_nano(self, mock_time_ns): ) self.assertIsNotNone(collected_data_points) self.assertEqual(len(collected_data_points), 1) - self.assertEqual( - view_instrument_match._start_time_unix_nano, - collection_start_time_unix_nano, - ) # +1 call to _create_aggregation view_instrument_match.consume_measurement( @@ -266,7 +259,7 @@ def test_collect_resets_start_time_unix_nano(self, mock_time_ns): ) ) view_instrument_match._view._aggregation._create_aggregation.assert_called_with( - instrument, {"foo": "bar1"}, collection_start_time_unix_nano + instrument, {"foo": "bar1"}, 1 ) collection_start_time_unix_nano = time_ns() collected_data_points = view_instrument_match.collect( @@ -277,10 +270,6 @@ def test_collect_resets_start_time_unix_nano(self, mock_time_ns): collected_data_points = view_instrument_match.collect( AggregationTemporality.CUMULATIVE, collection_start_time_unix_nano ) - self.assertEqual( - view_instrument_match._start_time_unix_nano, - collection_start_time_unix_nano, - ) # # +1 call to create_aggregation view_instrument_match.consume_measurement( Measurement( @@ -306,10 +295,6 @@ def test_collect_resets_start_time_unix_nano(self, mock_time_ns): view_instrument_match._view._aggregation._create_aggregation.call_count, 5, ) - self.assertEqual( - view_instrument_match._start_time_unix_nano, - collection_start_time_unix_nano, - ) def test_data_point_check(self): instrument1 = _Counter(