Skip to content

feat: Add per-metric windows to lock free metrics batcher - #8327

Open
alexander-alderman-webb wants to merge 27 commits into
webb/sliding-window-metrics-batcherfrom
webb/sliding-window-metrics-batcher-2
Open

alexander-alderman-webb wants to merge 27 commits into
webb/sliding-window-metrics-batcherfrom
webb/sliding-window-metrics-batcher-2

Conversation

@alexander-alderman-webb

@alexander-alderman-webb alexander-alderman-webb commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Keeps window-sampled metrics in a thread-local reservoir, and performs uniform sampling once the window is no longer the active window. Segmenting metrics per thread while the window is being modified enables lock-free operations (only a thread touches its state).

Conceptually, each window has thread state, and each thread state has a reservoir of metrics.

The sum of metrics in a window computed by _Window.size() is imprecise. It undercount the actual size as the iteration over all reservoirs is not atomic, and overcounts as threads that start writing metrics are not counted after the iteration has started.

There can be multiple consumers if sentry_sdk.flush() runs concurrently with the background flusher. This is expected and handled by IndexError.

Issues

Reminders

@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Coverage Results 📊

✅ 59490 passed | ⏭️ 3035 skipped | Total: 62525 | Pass Rate: 95.15% | Execution Time: 226m 15s

📊 Comparison with Base Branch

Metric Change
Total Tests 📉 -84636
Passed Tests 📉 -79813
Failed Tests 📉 -1
Skipped Tests 📉 -4822

All tests are passing successfully.

✅ Patch coverage is 90.76% (108 of 119 changed executable lines covered; target 80%).
Project statement coverage is 90.06% (down 0.16 percentage points from base (41bf46c) to head (7967d7f)).

Changed files with executable lines (2)
File Patch coverage Changed executable lines
sentry_sdk/_lock_free_metrics_batcher.py 91.18% 93/102 covered; missed: 93, 94, 133, 134, 170, 197, 198, 231, 245; partial branches: 132, 169, 230, 244
sentry_sdk/client.py 88.24% 15/17 covered; missed: 141, 147; partial branches: 136, 138
Coverage diff
@@            Coverage Diff             @@
##        master     #8327       +/-##
==========================================
- Coverage    90.22%    90.06%    -0.16%
==========================================
  Files          203       201        -2
  Tracked lines     27349     22351     -4998
  Branches     10252      7826     -2426
==========================================
- Hits         24675     20129     -4546
+ Misses        2674      2222      -452
+ Partials      1557      1292      -265

Generated by Coverage Action

@alexander-alderman-webb alexander-alderman-webb changed the title feat: Add sliding window to lock free metrics batcher feat: Add per-metric windows to lock free metrics batcher Nov 18, 2026
@alexander-alderman-webb
alexander-alderman-webb marked this pull request as ready for review November 19, 2026 09:11
@alexander-alderman-webb
alexander-alderman-webb requested a review from a team as a code owner November 19, 2026 09:11

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit cb36693. Configure here.

@alexander-alderman-webb

Copy link
Copy Markdown
Collaborator Author

I just inlined _merge() as well since that's easier to read.

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.

1 participant