Skip to content

Commit

Permalink
Disable Hanami monkeypatch on Hanami 2.2
Browse files Browse the repository at this point in the history
In the previous commit we started reading the action name from the
Hanami Action instance on the request env.

Disable the monkeypatch on Hanami 2.2+ so it's not doing the same thing
twice. We'll leave the monkeypatch for now, but eventually we should be
able to remove it. Like on the next major Hanami version or after some
years.

Part of #911
  • Loading branch information
tombruijn committed Nov 5, 2024
1 parent af4e1eb commit 5661807
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
3 changes: 3 additions & 0 deletions lib/appsignal/loaders/hanami.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ def on_start
)
hanami_app_config.middleware.use(Appsignal::Rack::HanamiMiddleware)

return unless Gem::Version.new(Hanami::VERSION) < Gem::Version.new("2.2.0")

::Hanami::Action.prepend Appsignal::Loaders::HanamiLoader::HanamiIntegration
end

# Legacy instrumentation to set the action name in Hanami apps older than Hanami 2.2
module HanamiIntegration
def call(env)
super
Expand Down
28 changes: 22 additions & 6 deletions spec/lib/appsignal/loaders/hanami_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,16 @@ def uninstall_hanami_middleware
)
end

it "prepends the integration to Hanami::Action" do
expect(::Hanami::Action)
.to have_received(:prepend).with(Appsignal::Loaders::HanamiLoader::HanamiIntegration)
if DependencyHelper.hanami2_2_present?
it "does not prepend a monkeypatch integration to Hanami::Action" do
expect(::Hanami::Action).to_not have_received(:prepend)
.with(Appsignal::Loaders::HanamiLoader::HanamiIntegration)
end
else
it "prepends the integration to Hanami::Action" do
expect(::Hanami::Action).to have_received(:prepend)
.with(Appsignal::Loaders::HanamiLoader::HanamiIntegration)
end
end

def hanami_middleware_options
Expand Down Expand Up @@ -80,10 +87,19 @@ def make_request(env)
context "with an active transaction" do
let(:env) { { Appsignal::Rack::APPSIGNAL_TRANSACTION => transaction } }

it "sets action name on the transaction" do
make_request(env)
if DependencyHelper.hanami2_2_present?
it "does not set an action name on the transaction" do
# This is done by the middleware instead
make_request(env)

expect(transaction).to_not have_action
end
else
it "sets action name on the transaction" do
make_request(env)

expect(transaction).to have_action("HanamiApp::Actions::Books::Index")
expect(transaction).to have_action("HanamiApp::Actions::Books::Index")
end
end
end
end
Expand Down

0 comments on commit 5661807

Please sign in to comment.