opentelemetry-cpp icon indicating copy to clipboard operation
opentelemetry-cpp copied to clipboard

[METRICS] Support synchronous gauge with delta temporality

Open pranitaurlam opened this issue 5 months ago • 3 comments

Fixes #4146

Summary

Removes the block in MetricCollector::GetAggregationTemporality that logged an error and silently fell back to cumulative when delta was requested for a synchronous gauge instrument.

The existing delta fast-path in TemporalMetricStorage::buildMetrics already provides the correct window-scoped last-value semantics for gauge: because SyncMetricStorage::Collect swaps and resets the live attributes_hashmap_ on every collection, delta_metrics contains only values recorded since the previous collection. Stale attribute sets that were not updated in the current window are simply absent from the snapshot and are never re-emitted.

Changes:

  • metric_collector.cc: remove the gauge+delta error/fallback; return the reader-requested temporality directly.
  • temporal_metric_storage.cc: update fast-path comment to mention gauge and its window-scoped last-value behavior.
  • sync_metric_storage_gauge_test.cc:
    • Add kDelta to both INSTANTIATE_TEST_SUITE_P suites and assert values for both temporalities.
    • Add GaugeDeltaWindowTest.NoRecordWindowEmitsNothing: record → collect → no record → collect emits nothing.
    • Add GaugeDeltaWindowTest.StaleAttributeNotReemitted: record A+B → collect → record A only → collect emits only A.
    • Add GaugeDeltaWindowTest.RecordAfterEmptyWindowEmitsNewValue: empty window followed by a new recording is exported correctly.

Test plan

  • [x] Existing cumulative gauge tests still pass with both temporalities.
  • [x] NoRecordWindowEmitsNothing: second collection with no new recordings emits 0 points.
  • [x] StaleAttributeNotReemitted: attribute not recorded in the current window is not re-exported.
  • [x] RecordAfterEmptyWindowEmitsNewValue: new recording after an empty window is exported correctly.

pranitaurlam avatar Jun 12 '26 06:06 pranitaurlam

Hi @lalitb @marcalff @ThomsonTan — I've opened #4152 to address this.

The fix removes the error/fallback in MetricCollector::GetAggregationTemporality and relies on the existing delta fast-path in TemporalMetricStorage, which already provides the correct window-scoped last-value semantics (the live attributes_hashmap_ is swapped and reset on every collection, so stale attribute sets are never in the snapshot).

Tests added for the scenarios you mentioned: record → collect → no record → collect (emits nothing), stale attribute not re-emitted, and recovery after an empty window. Happy to adjust based on your feedback.

pranitaurlam avatar Jun 12 '26 06:06 pranitaurlam

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 82.09%. Comparing base (4352a63) to head (66744a6). :warning: Report is 24 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #4152      +/-   ##
==========================================
+ Coverage   82.01%   82.09%   +0.08%     
==========================================
  Files         385      386       +1     
  Lines       16093    16207     +114     
==========================================
+ Hits        13197    13303     +106     
- Misses       2896     2904       +8     
Files with missing lines Coverage Δ
sdk/src/metrics/state/metric_collector.cc 96.62% <100.00%> (+2.96%) :arrow_up:
sdk/src/metrics/state/temporal_metric_storage.cc 97.78% <ø> (+0.06%) :arrow_up:

... and 29 files with indirect coverage changes

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov[bot] avatar Jun 12 '26 07:06 codecov[bot]

@pranitaurlam - Since #4159 already carries the production-code change and adds the multi-reader coverage, can we narrow this PR to only the single-reader delta gauge tests that are not covered there? That would mean removing the MetricCollector::GetAggregationTemporality() change from this PR and keeping only the additional single-reader coverage, so the implementation change lands in one place.

lalitb avatar Jun 17 '26 15:06 lalitb

closing for inactivity. Feel free to reopen if needed.

lalitb avatar Jun 25 '26 14:06 lalitb