Skip to content

Commit 0843f14

Browse files
committed
Fix start_time_unix_nano for delta collection temporality
Fixes open-telemetry#4009
1 parent 72be755 commit 0843f14

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

Diff for: 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
99

10+
- Fix `start_time_unix_nano` for delta collection for sum aggregation
11+
([#4009](https://github.com/open-telemetry/opentelemetry-python/pull/4009))
1012
- Do not execute Flask Tests in debug mode
1113
([#3956](https://github.com/open-telemetry/opentelemetry-python/pull/3956))
1214
- When encountering an error encoding metric attributes in the OTLP exporter, log the key that had an error.

Diff for: opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/aggregation.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -278,16 +278,16 @@ def collect(
278278
is AggregationTemporality.DELTA
279279
):
280280

281-
if current_value is None:
282-
return None
283-
284281
previous_collection_start_nano = (
285282
self._previous_collection_start_nano
286283
)
287284
self._previous_collection_start_nano = (
288285
collection_start_nano
289286
)
290287

288+
if current_value is None:
289+
return None
290+
291291
return NumberDataPoint(
292292
attributes=self._attributes,
293293
start_time_unix_nano=previous_collection_start_nano,

Diff for: opentelemetry-sdk/tests/metrics/integration_test/test_sum_aggregation.py

+35-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from itertools import count
1616
from logging import ERROR
1717
from platform import system
18+
from time import sleep
1819
from unittest import TestCase
1920

2021
from pytest import mark
@@ -345,11 +346,43 @@ def test_synchronous_delta_temporality(self):
345346

346347
results.append(reader.get_metrics_data())
347348

348-
provider.shutdown()
349-
350349
for metrics_data in results:
351350
self.assertIsNone(metrics_data)
352351

352+
results = []
353+
354+
counter.add(1)
355+
results.append(reader.get_metrics_data())
356+
357+
sleep(0.1)
358+
results.append(reader.get_metrics_data())
359+
360+
counter.add(2)
361+
results.append(reader.get_metrics_data())
362+
363+
metric_data_0 = (
364+
results[0]
365+
.resource_metrics[0]
366+
.scope_metrics[0]
367+
.metrics[0]
368+
.data.data_points[0]
369+
)
370+
metric_data_2 = (
371+
results[2]
372+
.resource_metrics[0]
373+
.scope_metrics[0]
374+
.metrics[0]
375+
.data.data_points[0]
376+
)
377+
378+
self.assertIsNone(results[1])
379+
380+
self.assertGreater(
381+
metric_data_2.start_time_unix_nano, metric_data_0.time_unix_nano
382+
)
383+
384+
provider.shutdown()
385+
353386
@mark.skipif(
354387
system() != "Linux",
355388
reason=(

0 commit comments

Comments
 (0)