From 4bfbf78d8d6dfcd288a41d02107707cf9c6edab0 Mon Sep 17 00:00:00 2001 From: Noemi <45180344+unflxw@users.noreply.github.com> Date: Tue, 2 Apr 2024 17:25:34 +0200 Subject: [PATCH] Do not attempt to transmit if not active If AppSignal is not active, it's likely that there is no valid configuration. Do not attempt to transmit heartbeats if AppSignal is not active. Log a debug message about it. --- lib/appsignal/heartbeat.rb | 7 ++++++- spec/lib/appsignal/heartbeat_spec.rb | 21 ++++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/lib/appsignal/heartbeat.rb b/lib/appsignal/heartbeat.rb index ef8016ba..01e95722 100644 --- a/lib/appsignal/heartbeat.rb +++ b/lib/appsignal/heartbeat.rb @@ -37,10 +37,15 @@ def event(kind) end def transmit_event(kind) + unless Appsignal.active? + Appsignal.internal_logger.debug("AppSignal not active, not transmitting heartbeat event") + return + end + response = self.class.transmitter.transmit(event(kind)) if response.code.to_i >= 200 && response.code.to_i < 300 - Appsignal.internal_logger.trace("Transmitted heartbeat `#{name}` #{kind} event") + Appsignal.internal_logger.trace("Transmitted heartbeat `#{name}` (#{id}) #{kind} event") else Appsignal.internal_logger.error( "Failed to transmit heartbeat event: status code was #{response.code}" diff --git a/spec/lib/appsignal/heartbeat_spec.rb b/spec/lib/appsignal/heartbeat_spec.rb index 4ad2eb2c..48f20c0d 100644 --- a/spec/lib/appsignal/heartbeat_spec.rb +++ b/spec/lib/appsignal/heartbeat_spec.rb @@ -4,15 +4,26 @@ let(:transmitter) { Appsignal::Transmitter.new("http://heartbeats/", config) } before(:each) do + allow(Appsignal).to receive(:active?).and_return(true) config.logger = Logger.new(StringIO.new) allow(Appsignal::Heartbeat).to receive(:transmitter).and_return(transmitter) end + describe "when Appsignal is not active" do + it "should not transmit any events" do + allow(Appsignal).to receive(:active?).and_return(false) + expect(transmitter).not_to receive(:transmit) + + heartbeat.start + heartbeat.finish + end + end + describe "#start" do it "should send a heartbeat start" do expect(transmitter).to receive(:transmit).with(hash_including( :name => "heartbeat-name", - :kind => "Start" + :kind => "start" )).and_return(nil) heartbeat.start @@ -23,7 +34,7 @@ it "should send a heartbeat finish" do expect(transmitter).to receive(:transmit).with(hash_including( :name => "heartbeat-name", - :kind => "Finish" + :kind => "finish" )).and_return(nil) heartbeat.finish @@ -34,12 +45,12 @@ describe "when a block is given" do it "should send a heartbeat start and finish and return the block output" do expect(transmitter).to receive(:transmit).with(hash_including( - :kind => "Start", + :kind => "start", :name => "heartbeat-with-block" )).and_return(nil) expect(transmitter).to receive(:transmit).with(hash_including( - :kind => "Finish", + :kind => "finish", :name => "heartbeat-with-block" )).and_return(nil) @@ -67,7 +78,7 @@ describe "when no block is given" do it "should only send a heartbeat finish event" do expect(transmitter).to receive(:transmit).with(hash_including( - :kind => "Finish", + :kind => "finish", :name => "heartbeat-without-block" )).and_return(nil)