Skip to content

Commit

Permalink
Don't start again in Hanami integration
Browse files Browse the repository at this point in the history
When AppSignal is already active, do not start AppSignal again.
This is a good precaution as it prevents boot loops, when AppSignal is
started twice with different configurations.

This will improve support for nested Hanami applications, when they're
mounted in another frameworks we support, like Rails or Sinatra.

This is similar to PR #1105

Part of #329
  • Loading branch information
tombruijn committed Jun 27, 2024
1 parent 7b4e583 commit 0da421f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
7 changes: 7 additions & 0 deletions .changesets/don-t-start-for-hanami-if-already-started.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
bump: patch
type: fix
---

Fix issue with AppSignal getting stuck in a boot loop when loading the Hanami integration with: `require "appsignal/integrations/hanami"`
This could happen in nested applications, like a Hanami app in a Rails app. It will now use the first config AppSignal starts with.
12 changes: 7 additions & 5 deletions lib/appsignal/integrations/hanami.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ def self.init
Appsignal.internal_logger.debug("Loading Hanami integration")

hanami_app_config = ::Hanami.app.config
Appsignal.config = Appsignal::Config.new(
hanami_app_config.root || Dir.pwd,
hanami_app_config.env
)

Appsignal.start
unless Appsignal.active?
Appsignal.config = Appsignal::Config.new(
hanami_app_config.root || Dir.pwd,
hanami_app_config.env
)
Appsignal.start
end

return unless Appsignal.active?

Expand Down
29 changes: 29 additions & 0 deletions spec/lib/appsignal/integrations/hanami_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,35 @@ def uninstall_hanami_middleware
end
end

context "when AppSignal is already active" do
before do
expect(Appsignal).to receive(:active?).at_least(1).and_return(true)
end

it "does not initialize AppSignal again" do
expect(Appsignal).to_not receive(:start)

Appsignal::Integrations::HanamiPlugin.init
end

it "prepends the integration to Hanami::Action" do
Appsignal::Integrations::HanamiPlugin.init

expect(::Hanami::Action)
.to have_received(:prepend).with(Appsignal::Integrations::HanamiIntegration)
end

it "adds middleware to the Hanami app" do
Appsignal::Integrations::HanamiPlugin.init

expect(::Hanami.app.config.middleware.stack[::Hanami::Router::DEFAULT_PREFIX])
.to include(
[Rack::Events, [[kind_of(Appsignal::Rack::EventHandler)]], *hanami_middleware_options],
[Appsignal::Rack::HanamiMiddleware, [], *hanami_middleware_options]
)
end
end

context "when APPSIGNAL_APP_ENV ENV var is provided" do
it "uses this as the environment" do
ENV["APPSIGNAL_APP_ENV"] = "custom"
Expand Down

0 comments on commit 0da421f

Please sign in to comment.