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

[METRICS] Drop stale async attribute sets from cumulative exports

Open pranitaurlam opened this issue 5 months ago • 2 comments

Fixes #4108

Summary

The OpenTelemetry SDK spec states:

The implementation SHOULD NOT produce aggregated metric data for a previously-observed attribute set which is not observed during a successful callback.

Bug: Under cumulative temporality, async instruments (ObservableCounter, ObservableGauge, ObservableUpDownCounter) kept emitting attribute sets indefinitely after the callback stopped reporting them. The frozen values would appear in every subsequent export with the stale last-observed value (and for ObservableGauge, even with a frozen timestamp).

Root cause in TemporalMetricStorage::buildMetrics(): the cumulative merge unconditionally carried every entry from last_reported_metrics_ into the output even if it was absent from the current delta.

Changes (3 files)

temporal_metric_storage.h / .cc

  • Add is_async_ boolean (default false) to TemporalMetricStorage
  • In the cumulative merge lambda, add else if (!is_async_) guard: sync instruments still carry forward all attribute sets (existing behaviour, spec-correct); async instruments skip stale entries not present in the current delta

async_metric_storage.h

  • Pass is_async = true when constructing TemporalMetricStorage
  • In Collect(), rebuild cumulative_hash_map_ to contain only entries present in delta_metrics — prevents unbounded memory growth and ensures correct delta computation if an attribute set reappears after a gap

Behaviour

Scenario Before After
Async cumulative, attribute dropped by callback Emitted forever with stale value Dropped from next export
Sync cumulative, attribute not measured this cycle Carried forward (correct) Unchanged
Delta temporality (async or sync) Correct (unaffected) Unchanged

Sister SDKs: opentelemetry-dotnet #6883 and opentelemetry-rust #2618 have shipped equivalent fixes.

cc @marcalff @lalitb @ThomsonTan @kyusic

pranitaurlam avatar Jun 07 '26 18:06 pranitaurlam

Hi @marcalff @lalitb @ThomsonTan @kyusic — could you please approve the workflows for PR #4140 (fix #4108)? Thank you!

pranitaurlam avatar Jun 07 '26 18:06 pranitaurlam

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 82.02%. Comparing base (4352a63) to head (0832e68). :warning: Report is 2 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #4140      +/-   ##
==========================================
+ Coverage   82.01%   82.02%   +0.02%     
==========================================
  Files         385      385              
  Lines       16093    16104      +11     
==========================================
+ Hits        13197    13208      +11     
  Misses       2896     2896              
Files with missing lines Coverage Δ
...telemetry/sdk/metrics/state/async_metric_storage.h 92.46% <100.00%> (+1.55%) :arrow_up:
sdk/src/metrics/state/temporal_metric_storage.cc 97.78% <100.00%> (+0.06%) :arrow_up:
: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 07 '26 21:06 codecov[bot]

@pranitaurlam could you please resolve the merge conflict?

ThomsonTan avatar Jun 24 '26 22:06 ThomsonTan