From 0115d2f579ed1ea6161b1e44fbe64586200e4e66 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Thu, 11 Jul 2024 17:26:09 -0600 Subject: [PATCH] Set _LastValueAggregation start_time_unix_nano to None (#4004) * Sest _LastValueAggregation start_time_unix_nano to None Fixes #3678 * Use None in test utils too --------- Co-authored-by: Riccardo Magliocchetti --- .../tests/test_metrics_encoder.py | 1 + .../src/opentelemetry/sdk/metrics/_internal/aggregation.py | 2 +- opentelemetry-sdk/tests/metrics/test_aggregation.py | 4 ++++ .../src/opentelemetry/test/metrictestutil.py | 2 +- 4 files changed, 7 insertions(+), 2 deletions(-) diff --git a/exporter/opentelemetry-exporter-otlp-proto-common/tests/test_metrics_encoder.py b/exporter/opentelemetry-exporter-otlp-proto-common/tests/test_metrics_encoder.py index 70863bb44a..6d68b48f25 100644 --- a/exporter/opentelemetry-exporter-otlp-proto-common/tests/test_metrics_encoder.py +++ b/exporter/opentelemetry-exporter-otlp-proto-common/tests/test_metrics_encoder.py @@ -296,6 +296,7 @@ def test_encode_gauge_int(self): ), ], time_unix_nano=1641946016139533244, + start_time_unix_nano=0, as_int=9000, ) ], diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/aggregation.py b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/aggregation.py index b389cc6ba5..3a09cdcfea 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/aggregation.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/aggregation.py @@ -370,7 +370,7 @@ def collect( return NumberDataPoint( attributes=self._attributes, - start_time_unix_nano=0, + start_time_unix_nano=None, time_unix_nano=collection_start_nano, value=value, ) diff --git a/opentelemetry-sdk/tests/metrics/test_aggregation.py b/opentelemetry-sdk/tests/metrics/test_aggregation.py index 37656b0a34..fb87d177e8 100644 --- a/opentelemetry-sdk/tests/metrics/test_aggregation.py +++ b/opentelemetry-sdk/tests/metrics/test_aggregation.py @@ -236,6 +236,8 @@ def test_collect(self): self.assertEqual(first_number_data_point.value, 1) + self.assertIsNone(first_number_data_point.start_time_unix_nano) + last_value_aggregation.aggregate(measurement(1)) # CI fails the last assertion without this @@ -249,6 +251,8 @@ def test_collect(self): self.assertEqual(second_number_data_point.value, 1) + self.assertIsNone(second_number_data_point.start_time_unix_nano) + self.assertGreater( second_number_data_point.time_unix_nano, first_number_data_point.time_unix_nano, diff --git a/tests/opentelemetry-test-utils/src/opentelemetry/test/metrictestutil.py b/tests/opentelemetry-test-utils/src/opentelemetry/test/metrictestutil.py index 08354bf0de..33f1039ef8 100644 --- a/tests/opentelemetry-test-utils/src/opentelemetry/test/metrictestutil.py +++ b/tests/opentelemetry-test-utils/src/opentelemetry/test/metrictestutil.py @@ -83,7 +83,7 @@ def _generate_gauge( data_points=[ NumberDataPoint( attributes=attributes, - start_time_unix_nano=1641946015139533244, + start_time_unix_nano=None, time_unix_nano=1641946016139533244, value=value, )