Patch ActionController::Base
and API
instead of Metal
#4375
+2
−1
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.
What does this PR do?
Makes a small adjustment to where the patch for
rails.action_controller
is applied. Originally itprepend
edActionController::Metal
, but this PR proposes to move it earlier toActionController::Base
andActionController::API
.Motivation:
In Rails, you will commonly see something like
These are exceptions that are being handled gracefully, but Datadog will tag the
rails.action_controller
with errors. This causes APM to report errors on these controller actions, which can also cause alerts via the error tracking feature. The problem here is that this is reporting on something that is in fact being handled correctly by the code, but the tracer simply isn't aware of it.This is happening because
datadog
is patchingActionController::Metal
, which adds the exception to the payload before theActionController
has a chance to run the rescue handler.There is a workaround, but it requires you to configure
action_dispatch.rescue_responses
;datadog
will check this to determine whether or not this is an expected exception. But this isn't really tenable; that configuration is largely intended for Rails'ShowExceptions
to handled raised exceptions, so you wouldn't typically configure these with exceptions that you are already handling in the controller.By simply moving the patch, the entirety of
process_action
is traced, which gives Rails a chance to run the rescue handlers.Change log entry
Additional Notes:
This may address a few existing issues:
rack.request
span that are stopped in middleware or before action doesn't have resource set #3482: This one seems to be reporting that therack.request
resource is not correctly set when abefore_action
raises an exception. This is because with the original patch, the action callbacks ran before the trace; now they will run under the trace. This will allow the patch to now set therack.request
resource before anybefore_action
s run.rails.action_controller
span). This will no longer be the case. This is worth mentioning because there appears to be some differing opinions around whether or not this should be included as part of therails.action_controller
span. I personally think it should, because it is part of theprocess_action
call.With the proposed change, it means more work is being traced. Given that the
rails.action_controller
span is intended to measure the controller action, I do think that this is more representative of what is happening in the controller.I have chosen to also patch
ActionController::API
to support Rails'api_mode
. Unsure if this needs to be smarter by first checking if the module exists before attempting to apply the patch. I can make that change if need be.How to test the change?
I did follow the guide in https://github.com/DataDog/dd-trace-rb/blob/master/docs/DevelopmentGuide.md, and it does appear that the existing suite is still green. I may not have done it right, though. The change seems pretty superficial to me, but if there are any tests that I should add or modify, please let me know. I'm really not sure what to do here, my apologies.
I have applied this patch manually in our own API application, and everything is working as expected as far as I can tell. I don't expect "just trust me!" to be super convincing, though 😆.