From dc98256623318d8f412f4fde2fb594624bf6f5c7 Mon Sep 17 00:00:00 2001 From: "Jon R. Humphrey" Date: Wed, 9 Oct 2024 10:31:35 +0100 Subject: [PATCH] refactor: adjusted internal error instrumentation Wrapped the Internal Error Instrumentation in an `unless` block to ensure the application does not report internal errors to the Prometheus metrics when the error is a 404 or 422 thereby reducing the noise in the Slack alerts channel --- app/controllers/application_controller.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 31e9aca..5776c10 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -27,8 +27,10 @@ def log_request_result # or attempt to render a generic error page if no specific error page exists unless Rails.application.config.consider_all_requests_local rescue_from StandardError do |e| - # Instrument ActiveSupport::Notifications for internal errors: - ActiveSupport::Notifications.instrument('internal_error.application', exception: e) + # Instrument ActiveSupport::Notifications for internal errors but only for non-404 errors: + unless e.is_a?(ActionController::RoutingError) || e.is_a?(ActionView::MissingTemplate) + ActiveSupport::Notifications.instrument('internal_error.application', exception: e) + end # Trigger the appropriate error handling method based on the exception case e.class when ActionController::RoutingError, ActionView::MissingTemplate @@ -104,7 +106,7 @@ def detailed_request_log(duration) body: request.body.gets&.gsub("\n", '\n'), method: request.method, status: response.status, - message: Rack::Utils::HTTP_STATUS_CODES[response.status] + message: response.message || Rack::Utils::HTTP_STATUS_CODES[response.status] } case response.status