Skip to content

Commit

Permalink
Do not attempt to transmit if not active
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
unflxw committed Apr 5, 2024
1 parent 9c7ce2b commit 4bfbf78
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
7 changes: 6 additions & 1 deletion lib/appsignal/heartbeat.rb
Expand Up @@ -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}"
Expand Down
21 changes: 16 additions & 5 deletions spec/lib/appsignal/heartbeat_spec.rb
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 4bfbf78

Please sign in to comment.