Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add heartbeat route for monitoring, filter heartbeat requests from th…
…e logs and configure log file rotation.
- Loading branch information
1 parent
11a6580
commit fae89aa
Showing
5 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 @@ | ||
<% content_for :title, "heartbeat" %> |
27 changes: 27 additions & 0 deletions
27
config/initializers/rails/rack/rack_logger_monkey_patch.rb
This file contains 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,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module RackLoggerMonkeyPatch | ||
|
||
def call(env) | ||
if should_suppress?(env["PATH_INFO"]) | ||
Rails.logger.silence(Logger::WARN) { super } | ||
else | ||
super | ||
end | ||
end | ||
|
||
private | ||
|
||
# Suppress logging of heartbeat GETs as these are high volume and clutter the logs | ||
def should_suppress?(path) | ||
return false if WasteCarriersEngine::FeatureToggle.active?(:disable_rack_logger_filter) | ||
|
||
path&.match(/#{heartbeat_path}/).present? | ||
end | ||
|
||
def heartbeat_path | ||
@heartbeat_path ||= Rails.application.config.wcrs_logger_heartbeat_path | ||
end | ||
end | ||
|
||
Rails::Rack::Logger.prepend RackLoggerMonkeyPatch |
This file contains 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 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 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,36 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe RackLoggerMonkeyPatch do | ||
describe "#info" do | ||
let(:log_file_path) { Rails.root.join("tmp/foo.log") } | ||
let(:log_contents) { File.read(log_file_path) } | ||
|
||
before { Rails.logger = ActiveSupport::Logger.new(log_file_path) } | ||
|
||
after { FileUtils.rm_f(log_file_path) } | ||
|
||
context "with a non-heartbeat route" do | ||
before { get "/start" } | ||
|
||
it { expect(log_contents).to match(/Started GET /) } | ||
end | ||
|
||
context "with the heartbeat route" do | ||
before { get Rails.application.config.wcrs_logger_heartbeat_path } | ||
|
||
it { expect(log_contents).not_to match(/Started GET /) } | ||
end | ||
|
||
context "when the 'disable_rack_logger_filter' feature-toggle is active" do | ||
before do | ||
allow(WasteCarriersEngine::FeatureToggle).to receive(:active?).with(:disable_rack_logger_filter).and_return true | ||
|
||
get Rails.application.config.wcrs_logger_heartbeat_path | ||
end | ||
|
||
it { expect(log_contents).to match(/Started GET /) } | ||
end | ||
end | ||
end |