Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions config/initializers/health_check_event_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class FilteredEventSubscriber
def initialize(subscriber, filter:)
@subscriber = subscriber
@filter = filter
end

def emit(event)
@subscriber.emit(event) if @filter.call(event)
end
end

class HealthCheckFilteredEventSubscriber < FilteredEventSubscriber
def initialize(subscriber)
filter = ->(event) { event.dig(:payload, :controller) != "Rails::HealthController" }

super(subscriber, filter:)
end
end
Comment thread
northeastprince marked this conversation as resolved.

Rails.application.config.to_prepare do
next unless defined?(Appsignal::Integrations::ActiveSupportEventReporter::Subscriber)

subscribers = Rails.event.instance_variable_get(:@subscribers)
original = subscribers.find { it[:subscriber].is_a? Appsignal::Integrations::ActiveSupportEventReporter::Subscriber }

next unless original

subscribers.delete original

filtered = HealthCheckFilteredEventSubscriber.new(original[:subscriber])
Rails.event.subscribe(filtered, &original[:filter])
Comment thread
northeastprince marked this conversation as resolved.
end
34 changes: 34 additions & 0 deletions test/lib/health_check_event_filtering_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require "test_helper"

class HealthCheckEventFilteringTest < ActionDispatch::IntegrationTest
test "filters out health check request events" do
with_filtered_event_reporter do
assert_no_event_reported("action_controller.request_started") do
get health_check_path
assert_response :ok
end
end
end

test "passes through non-health check request events" do
assert_event_reported("action_controller.request_completed") do
get root_path
end
Comment thread
northeastprince marked this conversation as resolved.
end

private

def with_filtered_event_reporter
original = ActiveSupport.event_reporter
ActiveSupport.event_reporter = FilteredEventReporter.new(original)
yield
ensure
ActiveSupport.event_reporter = original
end
Comment on lines +21 to +27
Copy link

Copilot AI Apr 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test mutates the global ActiveSupport.event_reporter. Since the test suite is configured to run in parallel (parallelize in test/test_helper.rb), this can cause cross-test interference and flaky failures when other tests emit events concurrently. Consider making this test non-parallel-safe (disable parallelization for this class) or avoid changing the global reporter by using a local subscriber/spies that only observes events within the block.

Copilot uses AI. Check for mistakes.

class FilteredEventReporter < SimpleDelegator
def subscribe(subscriber, &filter)
super(HealthCheckFilteredEventSubscriber.new(subscriber), &filter)
end
Comment thread
northeastprince marked this conversation as resolved.
end
end
Loading