Skip to content

feat(metrics): Add experimental window sampling with stratify_by - #8301

Closed
sentry-junior[bot] wants to merge 2 commits into
masterfrom
k-fish/feat/metrics-window-sampling
Closed

sentry-junior[bot] wants to merge 2 commits into
masterfrom
k-fish/feat/metrics-window-sampling

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

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_TIME metrics per window, chosen by reservoir sampling directly in the existing buffer. At flush, every kept metric gets sentry.client_sample_rate = kept / seen so 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 as sample_rate or backpressure client 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 for a/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 at allowance - 1 strata, and any values beyond that share an "other" stratum, so every stratum keeps at least one metric.

Open decisions:

  • Whether stratify_by also needs a global default in _experiments. Right now it can only be set per call.
  • For one window, the first call's sample_per_second and stratify_by for a name win.
  • Back pressure fires once about 20 names fill their allowance within one window, and it halves every name equally. Water-filling across names would protect low-volume names. That is not part of this PR.

Design context: the "Sampling - Metrics are broken for medium+ customers" doc in Notion.

via Kevan Fisher.

--

View Session [Sentry]

sentry-junior Bot and others added 2 commits November 16, 2026 21:31
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>
@k-fish k-fish closed this Nov 16, 2026
@k-fish

k-fish commented Nov 16, 2026

Copy link
Copy Markdown
Member

Bad bot

@sentry-junior
sentry-junior Bot deleted the k-fish/feat/metrics-window-sampling branch November 16, 2026 21:43
@github-actions

Copy link
Copy Markdown
Contributor

Coverage Results 📊

✅ 139330 passed | ⏭️ 7857 skipped | Total: 147187 | Pass Rate: 94.66% | Execution Time: 443m 55s

📊 Comparison with Base Branch

Metric Change
Total Tests 📈 +102
Passed Tests 📈 +103
Failed Tests 📉 -1
Skipped Tests —

➖ Removed Tests (1)

View removed tests
  • test_profile_captured
    • File: tests.profiler.test_transaction_profiler

All tests are passing successfully.

✅ Patch coverage is 94.96% (132 of 139 changed executable lines covered; target 80%).
Project statement coverage is 90.24%.

Changed files with executable lines (3)
File Patch coverage Changed executable lines
sentry_sdk/_metrics_batcher.py 94.26% 115/122 covered; missed: 113, 114, 134, 135, 151, 281, 282; partial branches: 150, 245
sentry_sdk/_batcher.py 100.00% 8/8 covered
sentry_sdk/metrics.py 100.00% 9/9 covered

Generated by Coverage Action

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.

1 participant