Skip to content

Commit

Permalink
Merge pull request #56 from juan-apa/add-healthcheck-request-silencer
Browse files Browse the repository at this point in the history
Add healthcheck request silencer
  • Loading branch information
micaelalorenzo authored Sep 11, 2024
2 parents b06b7af + 0ab9bca commit b411fb9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
require 'action_cable/engine'
# require "rails/test_unit/railtie"

require_relative '../lib/middlewares/silence_request'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
Expand Down Expand Up @@ -51,6 +53,9 @@ class Application < Rails::Application
config.middleware.insert_after(Rack::Runtime, Rack::MethodOverride)
config.middleware.insert_after(ActionDispatch::Cookies, ActionDispatch::Session::CookieStore)

# Silence requests made to the /up (healthcheck) path
config.middleware.insert_before Rails::Rack::Logger, SilenceRequest, path: '/up'

config.require_master_key = false
end
end
29 changes: 29 additions & 0 deletions lib/middlewares/silence_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

# Taken from https://github.com/rails/rails/pull/52789.
# Once the app is updated to Rails 8, this file can be removed.
#
#
# Allows you to silence requests made to a specific path.
# This is useful for preventing recurring requests like healthchecks from clogging the logging.
# This middleware is used to do just that against the path /up in production by default.
#
# Example:
#
# config.middleware.insert_before \
# Rails::Rack::Logger, Rails::Rack::SilenceRequest, path: "/up"
#
class SilenceRequest
def initialize(app, path:)
@app = app
@path = path
end

def call(env)
if env['PATH_INFO'] == @path
Rails.logger.silence { @app.call(env) }
else
@app.call(env)
end
end
end

0 comments on commit b411fb9

Please sign in to comment.