feat(metrics-util): Expose true count and sum on Drain for reservoir sampling#681
Open
michelhe wants to merge 3 commits intometrics-rs:mainfrom
Open
feat(metrics-util): Expose true count and sum on Drain for reservoir sampling#681michelhe wants to merge 3 commits intometrics-rs:mainfrom
michelhe wants to merge 3 commits intometrics-rs:mainfrom
Conversation
Expose the total number of samples pushed into the reservoir (including those dropped by sampling) as a public method on Drain. This allows consumers to get the exact total pushed count without a lossy float round-trip through sample_rate. Signed-off-by: Michel Heily <michelheily@gmail.com>
Signed-off-by: Michel Heily <michelheily@gmail.com>
…ampled_sum() Signed-off-by: Michel Heily <michelheily@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Expose two public methods on
Drainthat let downstream consumers recover accurate aggregate statistics when reservoir sampling drops values:Drain::unsampled_len()— returns the total number of samples pushed into the reservoir, including those dropped by sampling. The field was already tracked internally; this just adds a public getter.Drain::unsampled_sum()— returns the true sum of all pushed samples, including dropped ones. Atomically accumulated inReservoir::push()using afetch_updateCAS loop onAtomicU64(same pattern asGaugeFn::incrementinmetrics/src/atomics.rs). Reset inDrain::drop()alongside the existing count reset.Motivation
When the reservoir overflows, downstream consumers draining the samples only see the sampled subset. A consumer naively counting or summing values in the drain loop would get results capped at reservoir capacity — not the true totals. For example, pushing 1000 values into a 16-slot reservoir means the drain yields only 16 samples, so a naive count gives 16 and a naive sum covers only those 16 retained values, while the true count is 1000 and the true sum is 499,500.
Previously, the only way to recover the true count was
drained_count / sample_rate, which is a lossy float round-trip. There was no way to recover the true sum at all.Tests
Unit tests covering: under-capacity (no sampling), overflow (proving true values survive), reset-after-drain (A/B buffer reuse), and empty reservoir.