Skip to content

Commit

Permalink
Add telemetry shutdown header
Browse files Browse the repository at this point in the history
So we can turn off however we like regardless of status code from the server side.
  • Loading branch information
jnunemaker committed Feb 22, 2024
1 parent cb59439 commit f7ff6ba
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/flipper/cloud/telemetry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,16 @@ def post_to_cloud(drained)
# thus may have a telemetry-interval header for us to respect.
response ||= error.response if error && error.respond_to?(:response)

if response && interval = response["telemetry-interval"]
self.interval = interval.to_f
if response
if Flipper::Typecast.to_boolean(response["telemetry-shutdown"])
debug "action=telemetry_shutdown message=The server has requested that telemetry be shut down."
stop
return
end

if interval = response["telemetry-interval"]
self.interval = interval.to_f
end
end
rescue => error
error "action=post_to_cloud error=#{error.inspect}"
Expand Down
46 changes: 46 additions & 0 deletions spec/flipper/cloud/telemetry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,52 @@
expect(stub).to have_been_requested
end

it "phones home and requests shutdown if telemetry-shutdown header is true" do
stub = stub_request(:post, "https://www.flippercloud.io/adapter/telemetry").
to_return(status: 404, body: "{}", headers: {"telemetry-shutdown" => "true"})

output = StringIO.new
cloud_configuration = Flipper::Cloud::Configuration.new(
token: "test",
logger: Logger.new(output),
logging_enabled: true,
)

# Record some telemetry and stop the threads so we submit a response.
telemetry = described_class.new(cloud_configuration)
telemetry.record(Flipper::Feature::InstrumentationName, {
operation: :enabled?,
feature_name: :foo,
result: true,
})
telemetry.stop
expect(stub).to have_been_requested
expect(output.string).to match(/action=telemetry_shutdown message=The server has requested that telemetry be shut down./)
end

it "phones home and does not shutdown if telemetry shutdown header is missing" do
stub = stub_request(:post, "https://www.flippercloud.io/adapter/telemetry").
to_return(status: 404, body: "{}", headers: {})

output = StringIO.new
cloud_configuration = Flipper::Cloud::Configuration.new(
token: "test",
logger: Logger.new(output),
logging_enabled: true,
)

# Record some telemetry and stop the threads so we submit a response.
telemetry = described_class.new(cloud_configuration)
telemetry.record(Flipper::Feature::InstrumentationName, {
operation: :enabled?,
feature_name: :foo,
result: true,
})
telemetry.stop
expect(stub).to have_been_requested
expect(output.string).not_to match(/action=telemetry_shutdown message=The server has requested that telemetry be shut down./)
end

it "can update telemetry interval from error" do
stub = stub_request(:post, "https://www.flippercloud.io/adapter/telemetry").
to_return(status: 500, body: "{}", headers: {"telemetry-interval" => "120"})
Expand Down

0 comments on commit f7ff6ba

Please sign in to comment.