diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c84fe1..9013852 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,18 @@ # Changes to the UKHPI app by version and date +## 2.0.1 - 2024-12 + +- (Jon) Improves error metrics reporting to ensure that logging always happens + with the appropriate severity depending on the exception status while reducing + the types of errors that can trigger a an error metric and therefore a + notification in slack + [GH-149](https://github.com/epimorphics/hmlr-linked-data/issues/149) +- (Jon) Updated the `maybe_report_to_sentry` method to call the + `instrument_internal_error` method for reporting internal errors to the + Prometheus metrics when necessary +- (Jon) Renamed the `handle_error` method to `handle_exceptions` to better + reflect the method's purpose + ## 2.0.0 - 2024-11 - (Bogdan) Updated all gems by regenerating `Gemfile.lock` diff --git a/Dockerfile b/Dockerfile index 1f36aff..8f6a344 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,8 +13,7 @@ RUN apk add --update \ tzdata \ yarn \ && rm -rf /var/cache/apk/* \ - && gem install rubygems-update -v 3.4.22 \ - && update_rubygems \ + && gem update --system \ && gem install bundler:$BUNDLER_VERSION \ && bundle config --global frozen 1 diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index dfde505..533ea0f 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -79,10 +79,23 @@ def log_response(status, error_log) # Notify subscriber(s) of an internal error event with the payload of the # exception once done - # @param [Exception] exp the exception that caused the error + # @param [exc] exp the exception that caused the error # @return [ActiveSupport::Notifications::Event] provides an object-oriented # interface to the event - def instrument_internal_error(exception) - ActiveSupport::Notifications.instrument('internal_error.application', exception: exception) + def instrument_internal_error(exc) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity + err = { + message: exc&.message || exc, + status: exc&.status || Rack::Utils::SYMBOL_TO_STATUS_CODE[exc] + } + err[:type] = exc.class&.name if exc&.class + err[:cause] = exc&.cause if exc&.cause + err[:backtrace] = exc&.backtrace if exc&.backtrace && Rails.env.development? + # Log the exception to the Rails logger with the appropriate severity + Rails.logger.send(err[:status] < 500 ? :warn : :error, JSON.generate(err)) + # Return unless the status code is 500 or greater to ensure subscribers are NOT notified + return unless err[:status] >= 500 + + # Instrument the internal error event to notify subscribers of the error + ActiveSupport::Notifications.instrument('internal_error.application', exception: err) end end diff --git a/app/controllers/exceptions_controller.rb b/app/controllers/exceptions_controller.rb index 538649d..5ccdb85 100644 --- a/app/controllers/exceptions_controller.rb +++ b/app/controllers/exceptions_controller.rb @@ -4,7 +4,7 @@ class ExceptionsController < ApplicationController layout 'application' - def handle_error + def handle_exceptions env = request.env exception = env['action_dispatch.exception'] status_code = ActionDispatch::ExceptionWrapper.new(env, exception).status_code @@ -26,6 +26,9 @@ def maybe_report_to_sentry(exception, status_code) return nil if Rails.env.development? # Why are we reporting to Sentry in dev? return nil unless status_code >= 500 + # add the exception to the prometheus metrics + instrument_internal_error(exception) + sevent = Sentry.capture_exception(exception) sevent&.event_id end diff --git a/app/lib/version.rb b/app/lib/version.rb index da348ad..4d205e6 100644 --- a/app/lib/version.rb +++ b/app/lib/version.rb @@ -3,7 +3,7 @@ module Version MAJOR = 2 MINOR = 0 - PATCH = 0 + PATCH = 1 SUFFIX = nil VERSION = "#{MAJOR}.#{MINOR}.#{PATCH}#{SUFFIX && ".#{SUFFIX}"}" end diff --git a/config/initializers/exceptions.rb b/config/initializers/exceptions.rb index 548b982..4855158 100644 --- a/config/initializers/exceptions.rb +++ b/config/initializers/exceptions.rb @@ -3,5 +3,5 @@ # Custom error handling via a Rack middleware action Rails.application.config.exceptions_app = lambda do |env| - ExceptionsController.action(:handle_error).call(env) + ExceptionsController.action(:handle_exceptions).call(env) end