Skip to content

Commit

Permalink
Set warning header on /v2/apps/:guid/stats for stats server errors
Browse files Browse the repository at this point in the history
Authored-by: Tim Downey <[email protected]>
  • Loading branch information
tcdowney committed Apr 30, 2021
1 parent ed75cf6 commit 932e8ec
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
7 changes: 6 additions & 1 deletion app/controllers/runtime/stats_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ def stats(guid, opts={})
end

begin
stats, _ = instances_reporters.stats_for_app(process)
stats, warnings = instances_reporters.stats_for_app(process)

warnings.each do |warning_message|
add_warning(warning_message)
end

stats.each_value do |stats_hash|
if stats_hash[:stats]
stats_hash[:stats].delete_if { |key, _| key == :net_info }
Expand Down
38 changes: 37 additions & 1 deletion spec/unit/controllers/runtime/stats_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ module VCAP::CloudController
}
}
end
let(:warnings) { [] }
let(:instances_reporters) { double(:instances_reporters) }

before do
CloudController::DependencyLocator.instance.register(:instances_reporters, instances_reporters)
allow(instances_reporters).to receive(:stats_for_app).and_return([stats, []])
allow(instances_reporters).to receive(:stats_for_app).and_return([stats, warnings])
end

context 'because they are a developer' do
Expand Down Expand Up @@ -55,6 +56,7 @@ module VCAP::CloudController

expect(last_response.status).to eq(200)
expect(MultiJson.load(last_response.body)).to eq(expected)
expect(last_response.headers['X-Cf-Warnings']).to be_nil
expect(instances_reporters).to have_received(:stats_for_app).with(
satisfy { |requested_app| requested_app.guid == process.app.guid })
end
Expand Down Expand Up @@ -91,6 +93,40 @@ module VCAP::CloudController
end
end

context 'when the instances reporter returns warnings' do
let(:warnings) { ['s0mjgnbha', 'full_moon_with_s0mjgnbha'] }

it 'should return the stats with an X-Cf-Warnings header' do
set_current_user(developer)

process.state = 'STARTED'
process.instances = 1
process.save

process.refresh

expected = {
'0' => {
'state' => 'RUNNING',
'stats' => {},
},
'1' => {
'state' => 'DOWN',
'details' => 'start-me',
'since' => 1,
}
}

get "/v2/apps/#{process.app.guid}/stats"

expect(last_response.status).to eq(200)
expect(MultiJson.load(last_response.body)).to eq(expected)
expect(last_response.headers['X-Cf-Warnings']).to eq('s0mjgnbha,full_moon_with_s0mjgnbha')
expect(instances_reporters).to have_received(:stats_for_app).with(
satisfy { |requested_app| requested_app.guid == process.app.guid })
end
end

context 'when instance reporter raises an ApiError' do
before do
@error = CloudController::Errors::ApiError.new_from_details('ServerError')
Expand Down

0 comments on commit 932e8ec

Please sign in to comment.