Repository navigation
Conversation
Behind an _experimental flag, we will add window sampling to allow for better handling of high volume metric event data.
Coverage Results 📊✅ 59522 passed | ⏭️ 3035 skipped | Total: 62557 | Pass Rate: 95.15% | Execution Time: 227m 19s 📊 Comparison with Base Branch
All tests are passing successfully. ✅ Patch coverage is 97.14% (102 of 105 changed executable lines covered; target 80%). Changed files with executable lines (2)
Coverage diff@@ Coverage Diff @@
## master #8320 +/-##
==========================================
- Coverage 90.25% 90.15% -0.10%
==========================================
Files 203 200 -3
Tracked lines 27330 22181 -5149
Branches 10242 7776 -2466
==========================================
- Hits 24665 19997 -4668
+ Misses 2665 2184 -481
+ Partials 1555 1274 -281Generated by Coverage Action |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit c27cfa5. Configure here.
| # Replacing a slot in a full reservoir doesn't need more memory, only | ||
| # a new slot does | ||
| if len(self._buffer) + self._held >= self.MAX_BEFORE_DROP: | ||
| return False |
There was a problem hiding this comment.
Window close drops incoming metrics
Medium Severity
Closing a full window copies every reserved metric into _buffer up to MAX_BEFORE_DROP, then the same add still capacity-checks that buffer. The metric that closed the window, and any that arrive before the flusher drains, are discarded as queue_overflow even though that capacity is about to be freed. MAX_BEFORE_FLUSH is meant to leave headroom for this gap, but a window close can fill the buffer to the hard cap in one shot and wipe that headroom out.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit c27cfa5. Configure here.
| # Replacing a slot in a full reservoir doesn't need more memory, only | ||
| # a new slot does | ||
| if len(self._buffer) + self._held >= self.MAX_BEFORE_DROP: | ||
| return False |
There was a problem hiding this comment.
Empty reservoirs bypass memory bound
Medium Severity
A new metric name is inserted into _reservoirs before the MAX_BEFORE_DROP check. When that check fails, the empty list stays in the map and _held never counts it, so unique names arriving while at capacity can grow _reservoirs without bound until the window closes. That breaks the claim that metric memory stays bounded by MAX_BEFORE_DROP.
Reviewed by Cursor Bugbot for commit c27cfa5. Configure here.
alexander-alderman-webb
left a comment
There was a problem hiding this comment.
The basis we inherited is not good, I'll write the same thing without all these locks that keep causing problems and update relevant conventions for you.
| attributes = item["attributes"] | ||
| client_rate = attributes.get("sentry.client_sample_rate") | ||
| if isinstance(client_rate, (int, float)) and 0 < client_rate <= 1: | ||
| attributes["sentry.client_sample_rate"] = rate * client_rate |
There was a problem hiding this comment.
Needs a conventions update, sentry.client_sample_rate is defined at the sample rate for a span.
Appreciate it 👍 . Didn't know about our issues w/ locks. Let me know if you'd like me to take it on too, just give me an outline of the problem and I can chip away at it if you're busy. |


Summary
We want to try introducing sampling for metrics to help with high volume cases. More details about why and why this specific approach here, but we'll likely be using window sampling.
tldr for window sampling; Each process's SDK keeps at most a fixed number of metrics per metric name per time window, it will pick which ones to keep at random (preferring ones attached to sampled spans to preserve trace-connectedness),and then marks each kept metric with its sample rate so we can scale counts back up.
I'm going to experiment with this for our Sentry SaaS python project since it has a.) a high number of processes to check that as a constraint b.) a rate-limit exceeding metric we can also test this design against, c.) nothing critical is using metrics, and finally d.) we should dogfood our own changes.
Sampling notes:
client_sample_rate = kept / seenper window (within the metric name) which is already accepted by relay.should_captureto skipOther:
per_namein experimental options for this reason, despite it being clunky.MAX_DROPmeans metric memory is bounded still.sample_randintroduces bias towards particular traces.