Skip to content

Commit

Permalink
Merge pull request #140 from epimorphics/issue/139-task-prometheus-me…
Browse files Browse the repository at this point in the history
…trics-update

Task: Prometheus Metrics - Update
  • Loading branch information
jonrandahl committed Sep 18, 2024
2 parents f19eaa1 + 4e415b8 commit e910771
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 20 deletions.
16 changes: 14 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
# Standard Reports UI: change log

## 1.5.4 - 2024-09

## Unreleased

- (Jon) Updated the application exceptions controller to instrument the
`ActiveSupport::Notifications` for internal errors
[GH-139](https://github.com/epimorphics/standard-reports-ui/issues/139)
- (Jon) Updated `config/initializers/prometheus.rb` to include the `Middleware
instrumentation` fix for the 0 memory bug by notifying Action Dispatch
subscribers on Prometheus initialise
[GH-139](https://github.com/epimorphics/standard-reports-ui/issues/139)
- (Jon) Updated `config/puma.rb` to include metrics plugin and port information
for the metrics endpoint as environment variable, with default, to enable
running multiple sibling HMLR apps locally if needed without port conflicts
[GH-139](https://github.com/epimorphics/standard-reports-ui/issues/139)
- (Jon) Updated the `lr_common_styles` gem to the latest 1.9.9 patch release.
- (Jon) Moved all mirrored configuration settings from individual environments
into the application configuration to reduce the need to manage multiple
sources of truth
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ GEM
json
lograge
railties
lr_common_styles (1.9.8)
lr_common_styles (1.9.9)
bootstrap-sass (~> 3.4.0)
font-awesome-rails (~> 4.7.0.1)
govuk_elements_rails (~> 2.0.0)
Expand Down
17 changes: 4 additions & 13 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ 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)
# Trigger the appropriate error handling method based on the exception
case e.class
when ActionController::RoutingError, ActionView::MissingTemplate
:render404
Expand All @@ -41,10 +44,7 @@ def log_request_result
end

def handle_internal_error(exception)
# Notify subscribers of the internal error event and render the appropriate error page
# or attempt to render a generic error page if no specific error page exists
# unless the exception is a 404, in which case do nothing
instrument_internal_error(exception) unless exception.status == 404
# Render the appropriate error page based on the exception
if exception.instance_of? ArgumentError
render_error(400)
else
Expand Down Expand Up @@ -118,13 +118,4 @@ def detailed_request_log(duration)
end
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength

# 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
# @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)
end
end
4 changes: 4 additions & 0 deletions config/initializers/prometheus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@
docstring: 'Histogram of response times for API requests',
buckets: Prometheus::Client::Histogram.exponential_buckets(start: 0.005, count: 16)
)

# Middleware instrumentation
# This fixes the 0 memory bug by notifying Action Dispatch subscribers on Prometheus initialise
ActiveSupport::Notifications.instrument('process_middleware.action_dispatch')
16 changes: 12 additions & 4 deletions config/puma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
min_threads_count = ENV.fetch('RAILS_MIN_THREADS', max_threads_count)
threads min_threads_count, max_threads_count

# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
#
port ENV.fetch('PORT', 3000)
# Specifies the `port` that Puma will listen on to receive requests;
# default is 3000.
port ENV.fetch('PORT', 3000)

# Specifies the `metrics_port` that Puma will listen on to export metrics;
# default is 9393.
metrics_port = ENV.fetch('METRICS_PORT', 9393)

# Specifies the `environment` that Puma will run in.
#
Expand All @@ -36,9 +40,13 @@
#
# preload_app!

# Enable the metrics plugin to export Puma's internal statistics as Prometheus metrics
plugin :metrics
# Bind the metric server to "url". "tcp://" is the only accepted protocol.
metrics_url "tcp://0.0.0.0:#{metrics_port}" if Rails.env.development?

# Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart
plugin :metrics

# Use a custom log formatter to emit Puma log messages in a JSON format
log_formatter do |str|
Expand Down

0 comments on commit e910771

Please sign in to comment.