Skip to content

Commit

Permalink
Use new notifications handlers on Rails edge
Browse files Browse the repository at this point in the history
Since
rails/rails@dbf2edb,
ActiveSupport::Notifications adds
ActiveSupport::Notifications::Fanout::Handle, which is now used to route
instrumentation calls through.

This patch switches to the new module when it's available, while keeping
backward compatibility for older versions.

Closes appsignal/support#280.
  • Loading branch information
jeffkreeftmeijer authored and tombruijn committed Oct 9, 2023
1 parent 399989f commit 2765674
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .changesets/handle-upcoming-rails-notification-handler.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: "patch"
type: "add"
---

Support Rails 7.1 ActiveSupport Notifications handler.
27 changes: 18 additions & 9 deletions lib/appsignal/hooks/active_support_notifications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,30 @@ def self.instrument(name, payload = {})
end

require "appsignal/integrations/active_support_notifications"
instrumenter = ::ActiveSupport::Notifications::Instrumenter
parent_integration_module = Appsignal::Integrations::ActiveSupportNotificationsIntegration
if instrumenter.method_defined?(:start) && instrumenter.method_defined?(:finish)
install_module(parent_integration_module::StartFinishIntegration)

if defined?(::ActiveSupport::Notifications::Fanout::Handle)
install_module(
parent_integration_module::StartFinishHandlerIntegration,
::ActiveSupport::Notifications::Fanout::Handle
)
else
install_module(parent_integration_module::InstrumentIntegration)
end
instrumenter = ::ActiveSupport::Notifications::Instrumenter

return unless instrumenter.method_defined?(:finish_with_state)
if instrumenter.method_defined?(:start) && instrumenter.method_defined?(:finish)
install_module(parent_integration_module::StartFinishIntegration, instrumenter)
else
install_module(parent_integration_module::InstrumentIntegration, instrumenter)
end

install_module(parent_integration_module::FinishStateIntegration)
return unless instrumenter.method_defined?(:finish_with_state)

install_module(parent_integration_module::FinishStateIntegration, instrumenter)
end
end

def install_module(mod)
::ActiveSupport::Notifications::Instrumenter.send(:prepend, mod)
def install_module(mod, instrumenter)
instrumenter.send(:prepend, mod)
end
end
end
Expand Down
26 changes: 26 additions & 0 deletions lib/appsignal/integrations/active_support_notifications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,32 @@ def finish(name, payload = {})
end
end

module StartFinishHandlerIntegration
def start
instrument_this = @name[0] != ActiveSupportNotificationsIntegration::BANG

Appsignal::Transaction.current.start_event if instrument_this
super
end

def finish_with_values(name, id, payload = {})
# Events that start with a bang are internal to Rails
instrument_this = name[0] != ActiveSupportNotificationsIntegration::BANG

if instrument_this
title, body, body_format = Appsignal::EventFormatter.format(name, payload)
Appsignal::Transaction.current.finish_event(
name.to_s,
title,
body,
body_format
)
end

super
end
end

module FinishStateIntegration
def finish_with_state(listeners_state, name, payload = {})
# Events that start with a bang are internal to Rails
Expand Down
6 changes: 6 additions & 0 deletions spec/lib/appsignal/hooks/active_support_notifications_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@

it_behaves_like "activesupport instrument override"

if defined?(::ActiveSupport::Notifications::Fanout::Handle)
require_relative "./active_support_notifications/start_finish_shared_examples"

it_behaves_like "activesupport start finish override"
end

if ::ActiveSupport::Notifications::Instrumenter.method_defined?(:start)
require_relative "./active_support_notifications/start_finish_shared_examples"

Expand Down

0 comments on commit 2765674

Please sign in to comment.