Repository navigation
feat(metrics): Add experimental window sampling with stratify_by - #8301
Closed
sentry-junior[bot] wants to merge 2 commits into
Closed
sentry-junior[bot] wants to merge 2 commits into
sentry-junior[bot] wants to merge 2 commits into
Conversation
Behind _experiments={"metrics_window_sampling": True | {"sample_per_second": N}}.
The existing ~5s flush window is the sampling window. Each metric name
keeps at most sample_per_second * FLUSH_WAIT_TIME metrics per window
(50 at the default 10/s), picked by reservoir sampling directly in the
regular buffer (append, or replace in place). Override per call with
sample_per_second=. At flush, kept metrics get
sentry.client_sample_rate = kept/seen, multiplied into any rate the
caller set, so counts extrapolate.
Back pressure: when the buffer reaches MAX_BEFORE_FLUSH and flushes
early, every name's allowance is halved for the next window; each window
that flushes on the timer doubles it back, up to the requested rate.
Client reports reuse existing reasons: sample_rate for drops at the
requested rate, backpressure for the extra drops from a lowered
allowance. Byte outcomes are estimated from one serialized metric per
name.
Batcher gets a behavior-preserving _add_locked hook; per-name state is
cleared with the buffer on every flush and after fork.
Co-Authored-By: Kevan Fisher <kevan.fisher@sentry.io>
`stratify_by=[attribute keys]` on count/gauge/distribution splits a metric name's window allowance between strata (distinct values of those attributes) by water-filling: small strata keep every metric and the rest of the allowance goes to the big ones. Each stratum gets its own `sentry.client_sample_rate = kept / seen`, so a rare value is not sampled away by a hot one and counts still extrapolate. A new metric in a stratum that has kept everything so far takes a random slot from the biggest stratum; otherwise it goes through Algorithm R within its stratum. Slots are still replaced in place, so a name never holds more than its allowance in the buffer. Strata are capped at allowance - 1 per name, past that values share an "other" stratum, so every stratum keeps at least one metric. Back pressure, flush, client reports, and fork reset are unchanged. Co-Authored-By: Kevan Fisher <kevan.fisher@sentry.io>
Member
|
Bad bot |
Contributor
Coverage Results 📊✅ 139330 passed | ⏭️ 7857 skipped | Total: 147187 | Pass Rate: 94.66% | Execution Time: 443m 55s 📊 Comparison with Base Branch
➖ Removed Tests (1)View removed tests
All tests are passing successfully. ✅ Patch coverage is 94.96% (132 of 139 changed executable lines covered; target 80%). Changed files with executable lines (3)
Generated by Coverage Action |
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.
Experimental, behind
_experiments={"metrics_window_sampling": True | {"sample_per_second": N}}. It is off by default.Window sampling. The ~5s flush window is also the sampling window. Each metric name keeps at most
sample_per_second * FLUSH_WAIT_TIMEmetrics per window, chosen by reservoir sampling directly in the existing buffer. At flush, every kept metric getssentry.client_sample_rate = kept / seenso counts still extrapolate.sample_per_second=overrides the rate for a single call. When the buffer reaches its flush cap, every name's allowance is halved for the next window and recovers as later windows flush on the timer. Drops are reported assample_rateorbackpressureclient reports.stratify_by.stratify_by=["job_type"]splits a name's allowance between the distinct values of those attributes by water-filling. Small strata keep every metric, and the rest of the allowance goes to the big ones. Each stratum gets its own sample rate. With an allowance of 10 and 2/2/1000 metrics fora/b/c, the result is 2 + 2 + 6. A rare value no longer gets sampled away by a hot one, and a name still never holds more than its allowance in the buffer. Each name is capped atallowance - 1strata, and any values beyond that share an "other" stratum, so every stratum keeps at least one metric.Open decisions:
stratify_byalso needs a global default in_experiments. Right now it can only be set per call.sample_per_secondandstratify_byfor a name win.Design context: the "Sampling - Metrics are broken for medium+ customers" doc in Notion.
via Kevan Fisher.
--
View Session [Sentry]