Skip to content

Commit

Permalink
Add heartbeat route for monitoring, filter heartbeat requests from th…
Browse files Browse the repository at this point in the history
…e logs and configure log file rotation.
  • Loading branch information
PaulDoyle-DEFRA committed May 7, 2024
1 parent 11a6580 commit fae89aa
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/views/pages/heartbeat.html.erb
@@ -0,0 +1 @@
<% content_for :title, "heartbeat" %>
27 changes: 27 additions & 0 deletions config/initializers/rails/rack/rack_logger_monkey_patch.rb
@@ -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
11 changes: 11 additions & 0 deletions spec/dummy/config/application.rb
Expand Up @@ -93,5 +93,16 @@ class Application < Rails::Application
config.application_version = "0.0.1".freeze
config.application_name = "waste-carriers-renewals"
config.git_repository_url = "https://github.com/DEFRA/#{config.application_name}"

# Logger
config.wcrs_logger_max_files = ENV.fetch("WCRS_LOGGER_MAX_FILES", 3).to_i
config.wcrs_logger_max_filesize = ENV.fetch("WCRS_LOGGER_MAX_FILESIZE", 10_000_000).to_i
config.wcrs_logger_heartbeat_path = ENV.fetch("wcrs_logger_heartbeat_path", "/pages/heartbeat")

config.logger = Logger.new(
Rails.root.join("log/#{Rails.env}.log"),
Rails.application.config.wcrs_logger_max_files,
Rails.application.config.wcrs_logger_max_filesize
)
end
end
2 changes: 2 additions & 0 deletions spec/dummy/config/environment.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# Load the Rails application.
require File.expand_path('../application', __FILE__)

Expand Down
36 changes: 36 additions & 0 deletions spec/requests/rack_logger_monkey_patch_spec.rb
@@ -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

0 comments on commit fae89aa

Please sign in to comment.