Skip to content

Commit

Permalink
Merge pull request #850 from flippercloud/telemetry-shutdown
Browse files Browse the repository at this point in the history
Add telemetry shutdown header
  • Loading branch information
jnunemaker authored Feb 22, 2024
2 parents cb59439 + 97fbdb1 commit 0a1b5e1
Show file tree
Hide file tree
Showing 2 changed files with 62 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
52 changes: 52 additions & 0 deletions spec/flipper/cloud/telemetry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
require 'flipper/cloud/configuration'

RSpec.describe Flipper::Cloud::Telemetry do
before do
# Stub polling for features.
stub_request(:get, "https://www.flippercloud.io/adapter/features?exclude_gate_names=true").
to_return(status: 200, body: "{}")
end

it "phones home and does not update telemetry interval if missing" do
stub = stub_request(:post, "https://www.flippercloud.io/adapter/telemetry").
to_return(status: 200, body: "{}")
Expand Down Expand Up @@ -42,6 +48,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 0a1b5e1

Please sign in to comment.