-
-
Notifications
You must be signed in to change notification settings - Fork 539
Fix trace_id mismatch for logs emitted before Sentry::Rails::CaptureExceptions runs #3016
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
runephilosof-abtion
wants to merge
1
commit into
getsentry:master
Choose a base branch
from
runephilosof-abtion:fix/early-request-log-trace-context
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Sentry | ||
| module Rails | ||
| # A minimal middleware that establishes Sentry's trace/scope context as early | ||
| # as possible in the middleware stack. | ||
| # | ||
| # +Sentry::Rails::CaptureExceptions+ is intentionally positioned right after | ||
| # +ActionDispatch::ShowExceptions+ so it can skip transaction creation for | ||
| # static asset requests served by earlier middlewares (like +Rack::Sendfile+ | ||
| # and +ActionDispatch::Static+). Because of that placement, anything logged | ||
| # before it runs - most notably +Rails::Rack::Logger+'s "Started ..." line - | ||
| # would otherwise get a trace_id/span_id unrelated to the rest of the request. | ||
| # | ||
| # This middleware only establishes the propagation context (using the | ||
| # incoming `sentry-trace`/`baggage` headers, if present); it does not start a | ||
| # transaction or capture exceptions. +Sentry::Rack::CaptureExceptions+ detects | ||
| # that context has already been established for this request (via | ||
| # +PropagationContext::ESTABLISHED_ENV_KEY+) and reuses it instead of | ||
| # generating a new, mismatched one. | ||
| class CaptureContext | ||
| def initialize(app) | ||
| @app = app | ||
| end | ||
|
|
||
| def call(env) | ||
| return @app.call(env) unless Sentry.initialized? | ||
|
|
||
| Sentry.clone_hub_to_current_thread | ||
| Sentry.get_current_scope.generate_propagation_context(env) | ||
| env[Sentry::PropagationContext::ESTABLISHED_ENV_KEY] = true | ||
|
|
||
| @app.call(env) | ||
| end | ||
| end | ||
| end | ||
| end | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "spec_helper" | ||
|
|
||
| RSpec.describe Sentry::Rails::CaptureContext do | ||
| # Records the current scope's trace_id every time it's called, so specs can | ||
| # compare what a piece of middleware would see at different points in the stack. | ||
| class CaptureContextSpecProbe | ||
| def self.captured_trace_ids | ||
| @captured_trace_ids ||= [] | ||
| end | ||
|
|
||
| def initialize(app) | ||
| @app = app | ||
| end | ||
|
|
||
| def call(env) | ||
| self.class.captured_trace_ids << Sentry.get_current_scope.get_trace_context[:trace_id] | ||
| @app.call(env) | ||
| end | ||
| end | ||
|
|
||
| describe "#call" do | ||
| before do | ||
| make_basic_app | ||
| end | ||
|
|
||
| it "establishes a propagation context and flags the env" do | ||
| trace_id_in_app = nil | ||
|
|
||
| app = lambda do |env| | ||
| trace_id_in_app = Sentry.get_current_scope.get_trace_context[:trace_id] | ||
| [200, {}, ["ok"]] | ||
| end | ||
|
|
||
| env = Rack::MockRequest.env_for("/test") | ||
| described_class.new(app).call(env) | ||
|
|
||
| expect(env[Sentry::PropagationContext::ESTABLISHED_ENV_KEY]).to eq(true) | ||
| expect(trace_id_in_app).to be_a(String) | ||
| end | ||
|
|
||
| it "is a no-op when Sentry is not initialized" do | ||
| allow(Sentry).to receive(:initialized?).and_return(false) | ||
|
|
||
| called = false | ||
| app = lambda do |env| | ||
| called = true | ||
| [200, {}, ["ok"]] | ||
| end | ||
|
|
||
| env = Rack::MockRequest.env_for("/test") | ||
| described_class.new(app).call(env) | ||
|
|
||
| expect(called).to eq(true) | ||
| expect(env[Sentry::PropagationContext::ESTABLISHED_ENV_KEY]).to be_nil | ||
| end | ||
| end | ||
|
|
||
| context "when composed with CaptureExceptions", type: :request do | ||
| before do | ||
| CaptureContextSpecProbe.captured_trace_ids.clear | ||
| end | ||
|
|
||
| context "without tracing enabled" do | ||
| before do | ||
| make_basic_app do |config, app| | ||
| app.config.middleware.insert_before(Sentry::Rails::CaptureExceptions, CaptureContextSpecProbe) | ||
| app.config.middleware.insert_after(Sentry::Rails::CaptureExceptions, CaptureContextSpecProbe) | ||
| end | ||
| end | ||
|
|
||
| it "keeps the same trace_id before and after CaptureExceptions runs" do | ||
| get "/world" | ||
|
|
||
| early_trace_id, late_trace_id = CaptureContextSpecProbe.captured_trace_ids | ||
|
|
||
| expect(early_trace_id).to be_a(String) | ||
| expect(late_trace_id).to eq(early_trace_id) | ||
| end | ||
| end | ||
|
|
||
| context "with tracing enabled" do | ||
| before do | ||
| make_basic_app do |config, app| | ||
| config.traces_sample_rate = 1.0 | ||
| app.config.middleware.insert_before(Sentry::Rails::CaptureExceptions, CaptureContextSpecProbe) | ||
| app.config.middleware.insert_after(Sentry::Rails::CaptureExceptions, CaptureContextSpecProbe) | ||
| end | ||
| end | ||
|
|
||
| it "keeps the same trace_id from before CaptureExceptions through the started transaction" do | ||
| get "/world" | ||
|
|
||
| early_trace_id, late_trace_id = CaptureContextSpecProbe.captured_trace_ids | ||
|
|
||
| expect(early_trace_id).to be_a(String) | ||
| # the "late" trace_id comes from the actual transaction CaptureExceptions started | ||
| expect(late_trace_id).to eq(early_trace_id) | ||
| end | ||
| end | ||
| end | ||
| end |
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
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
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.