Class: Rails::Rack::SilenceRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/rack/silence_request.rb

Overview

Allows you to silence requests made to a specific path. This is useful for preventing recurring requests like health checks 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"

This middleware can also be configured using ‘config.silence_healthcheck_path = “/up”` in Rails.

Instance Method Summary collapse

Constructor Details

#initialize(app, path:) ⇒ SilenceRequest

Returns a new instance of SilenceRequest.



20
21
22
# File 'lib/rails/rack/silence_request.rb', line 20

def initialize(app, path:)
  @app, @path = app, path
end

Instance Method Details

#call(env) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/rails/rack/silence_request.rb', line 24

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