Skip to content

fix(sdk): keep exemplars per timeseries - #2180

Open
gowtham-padala wants to merge 1 commit into
open-telemetry:mainfrom
gowtham-padala:fix/exemplar-reservoir-per-series
Open

gowtham-padala wants to merge 1 commit into
open-telemetry:mainfrom
gowtham-padala:fix/exemplar-reservoir-per-series

Conversation

@gowtham-padala

Copy link
Copy Markdown
Contributor

Closes #2168

Summary

Every series of a metric stream shared one exemplar reservoir, so one series' measurements replaced another's exemplars. A histogram that records 0.2 for http.route=/a and then 0.2 for http.route=/b exported an exemplar only for /b. The spec requires a reservoir per timeseries.

Following the issue, there is still one reservoir object per stream, but its state is kept per series index, so ExemplarReservoirInterface and StreamFactory don't change.

Changes

  • BucketStorage keys its entries by series index and bucket. collect() returns every series' exemplars, keyed "<index>:<bucket>", and clears the state, so series that stop recording don't hold memory. The keys are unique per series and bucket, because DeltaStorage::mergeInto() merges exemplars with +=, which keeps only one entry per key.
  • FixedSizeReservoir keeps a measurement count per series, and fills empty slots before sampling, as in the spec's SimpleFixedSizeExemplarReservoir algorithm. Before, it drew random_int(0, $measurements) from the first measurement, so the second one could overwrite the first.
  • FixedSizeReservoir defaults to 1 exemplar instead of 4, since PHP has no threads to size it by. This is under Changed in the changelog.
  • BucketStorage no longer takes a size, since it no longer preallocates. It is @internal.

Testing

  • HistogramBucketReservoirTest: two series offering into the same bucket each get their exemplar back.
  • FixedSizeReservoirTest: two measurements into an empty reservoir of size 4 give two exemplars; a series with one measurement keeps its exemplar after another series offers 100; a reservoir built with no size returns one exemplar per series; collect() resets it.
  • BucketStorageTest: unique keys per series and bucket, and collect() clears the storage.
  • New tests/Integration/SDK/Metrics/ExemplarTest: the /a and /b example through a MeterProvider with the in-memory exporter and the always_on filter. Both data points carry an exemplar.
  • The new tests fail on main and pass with this change. On PHP 8.2 with the dev image, the unit and integration suites pass apart from tests that also fail on main locally (composer resource detector, TraceTest reference tests). phpstan and php-cs-fixer are clean.

@gowtham-padala
gowtham-padala requested a review from a team as a code owner December 5, 2026 02:55
@gowtham-padala
gowtham-padala force-pushed the fix/exemplar-reservoir-per-series branch from dc143ad to 7794756 Compare December 5, 2026 02:55
@codecov

codecov Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.05%. Comparing base (1bffbed) to head (7794756).

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##               main    #2180      +/-   ##
============================================
+ Coverage     74.02%   74.05%   +0.03%     
  Complexity     3786     3786              
============================================
  Files           493      493              
  Lines         10653    10651       -2     
============================================
+ Hits           7886     7888       +2     
+ Misses         2767     2763       -4     
Flag Coverage Δ
8.2 73.99% <100.00%> (+<0.01%) ⬆️
8.3 74.01% <100.00%> (+0.04%) ⬆️
8.4 74.00% <100.00%> (+0.03%) ⬆️
8.5 73.99% <100.00%> (+<0.01%) ⬆️
8.6 74.01% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/SDK/Metrics/Exemplar/BucketStorage.php 100.00% <100.00%> (+2.70%) ⬆️
src/SDK/Metrics/Exemplar/FixedSizeReservoir.php 100.00% <100.00%> (ø)
.../SDK/Metrics/Exemplar/HistogramBucketReservoir.php 100.00% <100.00%> (ø)

... and 2 files with indirect coverage changes


Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1bffbed...7794756. Read the comment docs.

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

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Keep exemplars per timeseries, not per metric stream

1 participant