-
Notifications
You must be signed in to change notification settings - Fork 375
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
Improve Rails middleware error handling #345
Improve Rails middleware error handling #345
Conversation
@@ -158,7 +158,7 @@ class TracingControllerTest < ActionController::TestCase | |||
assert_equal(1, span.status, 'span should be flagged as an error') | |||
assert_equal('ZeroDivisionError', span.get_tag('error.type'), 'type should contain the class name of the error') | |||
assert_equal('divided by 0', span.get_tag('error.msg'), 'msg should state we tried to divided by 0') | |||
assert_nil(span.get_tag('error.stack')) | |||
refute_nil(span.get_tag('error.stack')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@palazzem We were asserting that we should not put stack traces on the controller spans when it raises an error, only its parent rack.request
span. Is this right? Seems like it isn't.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this was done to avoid having the stacktrace in 2 spans (the rack and the controller)
ad5512b
to
d21fcb6
Compare
Working on adding some middleware tests, to guard against regression. They're a bit trickier to implement, but I think I'm close to having something working. |
❤️ |
Middleware tests are in this pull request: #347 |
d21fcb6
to
b589fd3
Compare
52306ce
to
7c4cf8a
Compare
7c4cf8a
to
bad4e73
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should merge the other tests PR, be sure this big PR is properly rebased and move it in 0.12.x release branch.
Left one question just to be sure we're properly testing everything.
if defined?(::ActionDispatch::ExceptionWrapper) | ||
# Gets the equivalent status code for the exception (not all are 5XX) | ||
# You can add custom errors via `config.action_dispatch.rescue_responses` | ||
status = ::ActionDispatch::ExceptionWrapper.status_code_for_exception(exception.class.name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this compatibility has been tested in the other PR, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it has. And it's kind of a copy-paste from the Rails code we had before.
…pecs Added: Rails middleware specs
There are currently two issues with Rails middleware errors:
Exceptions raised in Rails middleware which sits above
Datadog::Contrib::Rails::ExceptionMiddleware
but belowActionDispatch::ShowExceptions
can result in the exception stack trace being excluded from the trace.Exceptions raised in Rails middleware that are equivalent to 4XX errors (e.g.
ActionController::RoutingError
) can cause the trace to incorrectly be tagged as an error.This pull request aims to address both of these, respectively by:
Datadog::Contrib::Rails::ExceptionMiddleware
directly belowActionDispatch::ShowExceptions
middleware. Users should take care to avoid placing custom middleware between these two, otherwise they'll see this issue re-occur.Datadog::Contrib::Rails::ExceptionMiddleware
to only tag traces whose exceptions have known equivalent statuses to 5XX. Exceptions that are not whitelisted viaconfig.action_dispatch.rescue_responses
will be tagged as errors by default. This includes custom errors: if custom middleware raises custom exceptions that should not be tagged as an error, then they should be added toconfig.action_dispatch.rescue_responses
inapplication.rb
.With the changes in this pull request, Rails applications produce the following behavior:
Trace for exception raised from custom Rails middleware (with custom ErrorsController):
Trace for a known 'not found' exception raised from custom Rails middleware (with custom ErrorsController):
Trace for custom exception equivalent to 'not found' raised from custom Rails middleware (with custom ErrorsController):