Repository navigation
Record the 401 of a Warden-halted request in the instrumentation payload - #5877
Open
sampokuokkanen wants to merge 2 commits into
Open
sampokuokkanen wants to merge 2 commits into
sampokuokkanen wants to merge 2 commits into
Conversation
log_process_action wrote the status into the payload Rails logs from. Rails 8.2 logs from a frozen event payload, so that raised FrozenError and the request was never logged. Set it in append_info_to_payload instead, where the instrumentation payload is still open: every Rails Devise supports sets the status of a completed or raised action before calling it, so a missing status there means Warden threw.
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.
Rails main (8.2) logs request completion through the structured event reporter.
ActionController::LogSubscriber#request_completednow gets a frozen event payload built from the instrumentation payload, and passes it tolog_process_action. Devise's override writespayload[:status] ||= 401into it, so every request halted by Warden raises:The event reporter rescues it and reports it as a handled error, and the "Completed" line is never logged.
Fix
The status belongs in the instrumentation payload, not the log payload. This sets it in
append_info_to_payload, which runs in theensureofprocess_actionwhile that payload is still open. Every subscriber then sees it: the log line, structured events, and APM tools.This was tried in #4375 and reverted in #4804, because
append_info_to_payloadcould not tell an exception from a Warden throw. That no longer applies. Since Rails 7.0, the lowest version Devise supports,process_actionsets the status in itsrescuebefore theensureruns. A completed action and a raised exception both have a status by then, so a missing one means Warden threw. That is the case #4788 was about, and it is tested here.Tests
New tests in
AuthenticationInstrumentationTest:ActiveRecord::RecordNotFoundkeeps 404 (the Unexpected 401 Unauthorized response status when action is public #4788 regression);The logging test fails on Rails main without this change, and passes with it on both Rails 8.1 and main. The full suite passes on the default Gemfile and on
gemfiles/Gemfile-rails-main.