-
-
Notifications
You must be signed in to change notification settings - Fork 525
fix(rails): use ActionDispatch::ExceptionWrapper for correct HTTP status code
#2850
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
dingsdax
wants to merge
6
commits into
master
Choose a base branch
from
fix-rails-404-status-code
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.
+69
−1
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
19819e9
fix(rails): Use ActionDispatch::ExceptionWrapper for correct HTTP sta…
dingsdax b7b27b2
Update CHANGELOG.md
dingsdax 6431448
Fix test
dingsdax b4b4f17
Update tracing tests
dingsdax e6d46b6
Fix spec 🤞
dingsdax f5b9888
Add fix for Rails < 6
dingsdax 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
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 |
|---|---|---|
|
|
@@ -112,6 +112,63 @@ | |
| end | ||
| end | ||
|
|
||
| context "with report_rescued_exceptions and public error pages" do | ||
| before do | ||
| make_basic_app do |config, app| | ||
| config.traces_sample_rate = 1.0 | ||
| config.rails.report_rescued_exceptions = true | ||
| config.excluded_exceptions -= ['ActionController::RoutingError'] | ||
| app.config.consider_all_requests_local = false | ||
| end | ||
| end | ||
|
|
||
| it "records correct status code for 404 errors served by public error pages" do | ||
| get "/nonexistent_route" | ||
|
|
||
| expect(response).to have_http_status(:not_found) | ||
| expect(transport.events.count).to be >= 1 | ||
|
|
||
| event = transport.events.first.to_h | ||
| expect(event.dig(:contexts, :trace, :data, "http.response.status_code")).to eq(404) | ||
| end | ||
| end | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to also add the test from my branch to see what happens it "records correct status when Rails rescues exception to non-500 status" do
get "/posts/999999"
expect(response).to have_http_status(:not_found)
expect(transport.events.count).to eq(1)
transaction = transport.events.last.to_h
expect(transaction[:type]).to eq("transaction")
first_span = transaction[:spans][0]
expect(first_span[:op]).to eq("view.process_action.action_controller")
expect(first_span[:description]).to eq("PostsController#show")
expect(first_span[:status]).to eq("not_found")
expect(first_span[:data]["http.response.status_code"]).to eq(404)
end |
||
|
|
||
| context "with public error pages for controller exceptions" do | ||
| before do | ||
| make_basic_app do |config, app| | ||
| config.traces_sample_rate = 1.0 | ||
| app.config.consider_all_requests_local = false | ||
| config.trace_ignore_status_codes = [(301..303), (305..399)] | ||
|
|
||
| # In Rails < 6.0, ActiveRecord::RecordNotFound is not automatically mapped to :not_found | ||
| # https://github.com/rails/rails/blob/main/guides/source/configuring.md?configaction_dispatchrescue_responses | ||
| # We need to add it to the rescue_responses hash | ||
| if Rails.gem_version < Gem::Version.new("6.0.0") | ||
| ActionDispatch::ExceptionWrapper.rescue_responses['ActiveRecord::RecordNotFound'] = :not_found | ||
| end | ||
| end | ||
| end | ||
|
|
||
| it "records correct status when Rails rescues exception to non-500 status" do | ||
| get "/posts/999999" | ||
|
|
||
| expect(response).to have_http_status(:not_found) | ||
| expect(transport.events.count).to eq(1) | ||
|
|
||
| transaction = transport.events.last.to_h | ||
|
|
||
| expect(transaction[:type]).to eq("transaction") | ||
| expect(transaction.dig(:contexts, :trace, :status)).to eq("not_found") | ||
| expect(transaction.dig(:contexts, :trace, :data, "http.response.status_code")).to eq(404) | ||
|
|
||
| first_span = transaction[:spans][0] | ||
| expect(first_span[:op]).to eq("view.process_action.action_controller") | ||
| expect(first_span[:description]).to eq("PostsController#show") | ||
| expect(first_span[:status]).to eq("internal_error") | ||
| expect(first_span[:data]["http.response.status_code"]).to eq(500) | ||
| end | ||
| end | ||
|
|
||
| describe "filtering pii data" do | ||
| context "with send_default_pii = false" do | ||
| before do | ||
|
|
@@ -210,6 +267,8 @@ | |
| config.traces_sample_rate = 1.0 | ||
| config.sdk_logger = logger | ||
| config.rails.assets_regexp = %r{/foo/} | ||
| # Allow 404 status codes to be traced (default ignores 401-404) | ||
| config.trace_ignore_status_codes = [(301..303), (305..399)] | ||
| 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
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.