Skip to content

Commit

Permalink
Use current time when creating an aggregation in _ViewInstrumentMatch
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl committed Aug 20, 2024
1 parent a4971a8 commit d23328c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {}
Expand Down Expand Up @@ -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:
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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,
)
Expand Down
21 changes: 3 additions & 18 deletions opentelemetry-sdk/tests/metrics/test_view_instrument_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand Down

0 comments on commit d23328c

Please sign in to comment.