-
Notifications
You must be signed in to change notification settings - Fork 22k
Silence healthcheck requests from the log #52789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b3ac426
Add SilenceRequest middleware
dhh 190e06b
Make config.silence_healthcheck a way to quiet "/up"
dhh 8f24664
The remote ip part is actually not necessary
dhh 5d577b0
No longer used
dhh 7bfa765
Add CHANGELOG
dhh 3da1ca3
Make it more explicit that the argument is a path
dhh 8809c65
Better explain the purpose
dhh c6b5e49
No need to repeat the env
dhh 8b231d8
Why oh why
dhh 4bd8ca9
Document the new setting
dhh a746de3
Slim down the implementation
dhh 7a0325c
Update guides/source/configuring.md
dhh 56b2a2c
Double quotes
dhh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
# :markup: markdown | ||
|
||
require "active_support/logger_silence" | ||
|
||
module Rails | ||
module Rack | ||
# 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" | ||
# | ||
# This middleware can also be configured using `config.silence_healthcheck = "/up"` in Rails. | ||
class SilenceRequest | ||
def initialize(app, path:) | ||
@app, @path = app, path | ||
end | ||
|
||
def call(env) | ||
if env["PATH_INFO"] == @path | ||
Rails.logger.silence { @app.call(env) } | ||
else | ||
@app.call(env) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
require "abstract_unit" | ||
require "rack/test" | ||
require "minitest/mock" | ||
|
||
class RackSilenceRequestTest < ActiveSupport::TestCase | ||
test "silence request only to specific path" do | ||
mock_logger = Minitest::Mock.new | ||
mock_logger.expect :silence, nil | ||
|
||
app = Rails::Rack::SilenceRequest.new(lambda { |env| [200, env, "app"] }, path: "/up") | ||
|
||
Rails.stub(:logger, mock_logger) do | ||
app.call(Rack::MockRequest.env_for("http://example.com/up")) | ||
app.call(Rack::MockRequest.env_for("http://example.com/down")) | ||
end | ||
|
||
assert mock_logger.verify | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dhh did you mean to delete this test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did not. Not sure what happened there. Please do PR to restore!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#52812