Skip to content

fix(tracing): establish tracing capture context early enough - #3057

Open
solnic wants to merge 3 commits into
masterfrom
3015-railsrackloggers-started-log-line-and-anything-else-logged-before-sentryrailscaptureexceptions-gets-an-unrelated-trace_idspan_id
Open

solnic wants to merge 3 commits into
masterfrom
3015-railsrackloggers-started-log-line-and-anything-else-logged-before-sentryrailscaptureexceptions-gets-an-unrelated-trace_idspan_id

Conversation

@solnic

@solnic solnic commented Aug 19, 2026 •

Copy link
Copy Markdown
Collaborator

This is based on #3016


This adds a new Sentry::Rails::CaptureContext middleware that establishes the propagation context once, early.

CaptureExceptions reuses it instead of generating its own, and a transaction with no incoming trace continues the established one rather than fabricating a new trace_id.

It sits insert_after ActionDispatch::Executor - above Rails::Rack::Logger so the fix covers the logging, below the file-serving middlewares so static requests never touch Sentry.


Fixes #3015

@solnic solnic changed the title 3015 railsrackloggers started log line and anything else logged before sentryrailscaptureexceptions gets an unrelated trace idspan fix(tracing): establish tracing capture context early enough Aug 19, 2026
Comment thread spec/apps/rails-mini/app.rb Dismissed
@solnic
solnic force-pushed the 3015-railsrackloggers-started-log-line-and-anything-else-logged-before-sentryrailscaptureexceptions-gets-an-unrelated-trace_idspan_id branch 2 times, most recently from cbde3a6 to ceaba54 Compare September 4, 2026 11:05
@solnic
solnic marked this pull request as ready for review September 4, 2026 12:44
@solnic
solnic force-pushed the 3015-railsrackloggers-started-log-line-and-anything-else-logged-before-sentryrailscaptureexceptions-gets-an-unrelated-trace_idspan_id branch from 5cbb0df to 06d248f Compare October 6, 2026 08:49
Comment thread sentry-rails/lib/sentry/rails/capture_exceptions.rb
@solnic
solnic force-pushed the 3015-railsrackloggers-started-log-line-and-anything-else-logged-before-sentryrailscaptureexceptions-gets-an-unrelated-trace_idspan_id branch 6 times, most recently from b287c37 to 6c6ffa7 Compare October 6, 2026 11:41
@solnic
solnic force-pushed the 3015-railsrackloggers-started-log-line-and-anything-else-logged-before-sentryrailscaptureexceptions-gets-an-unrelated-trace_idspan_id branch from 6c6ffa7 to 0d3e728 Compare October 6, 2026 12:43
@solnic
solnic removed this pull request from stack #3059 October 6, 2026 12:46
@solnic
solnic changed the base branch from master to fix/debug-transport-log-envelopes October 6, 2026 12:46
@solnic
solnic added this pull request to stack #3114 October 6, 2026 12:46
@solnic
solnic force-pushed the 3015-railsrackloggers-started-log-line-and-anything-else-logged-before-sentryrailscaptureexceptions-gets-an-unrelated-trace_idspan_id branch from 0d3e728 to ef1cd41 Compare October 6, 2026 13:07
Base automatically changed from fix/debug-transport-log-envelopes to master October 6, 2026 13:22
runephilosof and others added 3 commits October 6, 2026 15:22
Rails::Rack::Logger's "Started ..." line (and anything logged before
CaptureExceptions runs) got an unrelated trace_id, since
CaptureExceptions runs after ActionDispatch::ShowExceptions.

Add Sentry::Rails::CaptureContext, a minimal middleware unshifted to
the front of the stack that establishes the propagation context early.
CaptureExceptions now consumes and reuses it instead of regenerating
a new trace_id/span_id.

Co-Authored-By: GitHub Copilot <noreply@example.com>
@solnic
solnic force-pushed the 3015-railsrackloggers-started-log-line-and-anything-else-logged-before-sentryrailscaptureexceptions-gets-an-unrelated-trace_idspan_id branch from ef1cd41 to 809eae3 Compare October 6, 2026 13:23
@solnic
solnic removed this pull request from stack #3114 October 6, 2026 13:24
@solnic
solnic added this pull request to stack #3115 October 6, 2026 13:24
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.

Rails::Rack::Logger's "Started ..." log line (and anything else logged before Sentry::Rails::CaptureExceptions) gets an unrelated trace_id/span_id

3 participants