fix: send buffered telemetry on the background worker - #3079
Open
ikraamg wants to merge 1 commit into
Open
Conversation
ikraamg
force-pushed
the
buffer-sends-off-thread
branch
2 times, most recently
from
September 12, 2026 20:09
4d9fe38 to
f189dda
Compare
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit f189dda. Configure here.
TelemetryEventBuffer#send_items runs under @Mutex, from both the timer thread and the add_item call that reaches max_items. It built the envelope and did the HTTP request to Sentry right there, so the request thread that happened to add the 100th log event paid a full TCP + TLS + POST round trip, and every other thread calling add_item (every SQL statement, with sentry-rails' ActiveRecord log subscriber) waited on the mutex behind it. Hand the send to Sentry.background_worker, the same path error events and Client#capture_envelope already take. The envelope is still built under the lock; only the network leaves it. The rescue moves inside the posted block so a failed send is logged the way it was. A full worker queue discards the block without raising, so that case records the batch as a queue_overflow client report, as capture_event does for a dropped event. Measured on a Rails API serving ~37 req/s at ~12 queries each: the inline flush was 16% of request time and lifted p90 from 44 ms to 66 ms. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
ikraamg
force-pushed
the
buffer-sends-off-thread
branch
from
September 12, 2026 20:21
f189dda to
ea46783
Compare
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.

Description
TelemetryEventBuffer#send_itemsruns under@mutex, both from the 5 s timer thread and from theadd_itemcall that reachesmax_items. It built the envelope and did the HTTP request to Sentry right there. Two effects:sentry-rails7.0 turning structured logging on by default with the ActiveRecord subscriber, that is every ~100 SQL statements per process.add_item(again, every SQL statement) blocked on the mutex behind that send.This hands the send to
Sentry.background_worker, the same path error events andClient#capture_envelopealready use. The envelope is still built under the lock; only the network leaves it. The rescue moves inside the posted block so a failed send is logged the way it was, and withbackground_worker_threads = 0the behavior is unchanged (immediate executor).Measured on a Rails API serving ~37 req/s at ~12 queries per request, after upgrading 6.4.1 → 7.0.0 with default settings: the inline flush was 15.8% of request time in an rbspy profile of the Puma workers (
Sentry::TelemetryEventBuffer#send_items→Net::HTTP#connect→TCPSocket#initialize), and request p90 stepped from 44 ms to 66 ms in the hour the upgrade deployed while p50 barely moved (28 → 30 ms). Applies toMetricEventBuffertoo, since it shares the base class.Tests: the shared buffer examples get two cases that run a real one-thread worker and assert the envelope is sent off the thread that filled the buffer (both
add_itemoverflow andflush). Fullsentry-rubysuite green locally (1520 examples).🤖 Generated with Claude Code
https://claude.ai/code/session_012QsjWMGPww2cFqGMX71vkB