Skip to content

Commit

Permalink
Revert "Merge pull request #468 from epimorphics/spike/reduce-metric-…
Browse files Browse the repository at this point in the history
…alerts"

This reverts commit b18c3df, reversing
changes made to 625b8e2.
  • Loading branch information
bogdanadrianmarc committed Dec 23, 2024
1 parent b18c3df commit 7409601
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 37 deletions.
13 changes: 0 additions & 13 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
# 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`
Expand Down
23 changes: 5 additions & 18 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def log_request_result

private

# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
# rubocop:disable Metrics/AbcSize
def detailed_request_log(duration)
env = request.env

Expand Down Expand Up @@ -67,27 +67,14 @@ def detailed_request_log(duration)
Rails.logger.info(JSON.generate(log_fields))
end
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
# rubocop:enable Metrics/AbcSize

# Notify subscriber(s) of an internal error event with the payload of the
# exception once done
# @param [exc] exp the exception that caused the error
# @param [Exception] exp the exception that caused the error
# @return [ActiveSupport::Notifications::Event] provides an object-oriented
# interface to the event
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)
def instrument_internal_error(exception)
ActiveSupport::Notifications.instrument('internal_error.application', exception: exception)
end
end
7 changes: 3 additions & 4 deletions app/controllers/exceptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
class ExceptionsController < ApplicationController
layout 'application'

def handle_exceptions
def render_error
env = request.env
exception = env['action_dispatch.exception']
status_code = ActionDispatch::ExceptionWrapper.new(env, exception).status_code

sentry_code = maybe_report_to_sentry(exception, status_code)
# add the exception to the prometheus metrics
instrument_internal_error(exception)

render :error_page,
locals: { status: status_code, sentry_code: sentry_code },
Expand All @@ -23,9 +25,6 @@ def maybe_report_to_sentry(exception, status_code)
return nil if Rails.env.development? # Why are we reporting to Senty 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
Expand Down
2 changes: 1 addition & 1 deletion app/lib/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Version
MAJOR = 2
MINOR = 0
PATCH = 1
PATCH = 0
SUFFIX = nil
VERSION = "#{MAJOR}.#{MINOR}.#{PATCH}#{SUFFIX && ".#{SUFFIX}"}"
end
2 changes: 1 addition & 1 deletion config/initializers/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
# Custom error handling via a Rack middleware action
Rails.application.config.exceptions_app =
lambda do |env|
ExceptionsController.action(:handle_exceptions).call(env)
ExceptionsController.action(:render_error).call(env)
end

0 comments on commit 7409601

Please sign in to comment.