diff --git a/.changesets/don-t-start-for-hanami-if-already-started.md b/.changesets/don-t-start-for-hanami-if-already-started.md new file mode 100644 index 000000000..b935222ae --- /dev/null +++ b/.changesets/don-t-start-for-hanami-if-already-started.md @@ -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. diff --git a/lib/appsignal/integrations/hanami.rb b/lib/appsignal/integrations/hanami.rb index 97e99f8e8..31b3d468c 100644 --- a/lib/appsignal/integrations/hanami.rb +++ b/lib/appsignal/integrations/hanami.rb @@ -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? diff --git a/spec/lib/appsignal/integrations/hanami_spec.rb b/spec/lib/appsignal/integrations/hanami_spec.rb index 1f64c4403..a988ccf1d 100644 --- a/spec/lib/appsignal/integrations/hanami_spec.rb +++ b/spec/lib/appsignal/integrations/hanami_spec.rb @@ -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"