Skip to content

Commit

Permalink
Show all instance port mappings in instance stats
Browse files Browse the repository at this point in the history
[#109832334]

Signed-off-by: Dan Wendorf <dwendorf@pivotal.io>
  • Loading branch information
rizwanreza authored and wendorf committed Apr 22, 2016
1 parent cc1e640 commit b90e782
Show file tree
Hide file tree
Showing 7 changed files with 316 additions and 214 deletions.
46 changes: 31 additions & 15 deletions app/presenters/v3/process_stats_presenter.rb
Expand Up @@ -9,7 +9,7 @@ def present_stats_hash(type, process_stats)
private

def instance_stats_hash(type, index, stats)
case stats['state']
case stats[:state]
when 'DOWN'
down_instance_stats_hash(type, index, stats)
else
Expand All @@ -21,29 +21,45 @@ def found_instance_stats_hash(type, index, stats)
{
type: type,
index: index,
state: stats['state'],
state: stats[:state],
usage: {
time: stats['stats']['usage']['time'],
cpu: stats['stats']['usage']['cpu'],
mem: stats['stats']['usage']['mem'],
disk: stats['stats']['usage']['disk'],
time: stats[:stats][:usage][:time],
cpu: stats[:stats][:usage][:cpu],
mem: stats[:stats][:usage][:mem],
disk: stats[:stats][:usage][:disk],
},
host: stats['stats']['host'],
port: stats['stats']['port'],
uptime: stats['stats']['uptime'],
mem_quota: stats['stats']['mem_quota'],
disk_quota: stats['stats']['disk_quota'],
fds_quota: stats['stats']['fds_quota']
}
host: stats[:stats][:host],
uptime: stats[:stats][:uptime],
mem_quota: stats[:stats][:mem_quota],
disk_quota: stats[:stats][:disk_quota],
fds_quota: stats[:stats][:fds_quota]
}.tap { |presented_stats| add_port_info(presented_stats, stats) }
end

def down_instance_stats_hash(type, index, stats)
{
type: type,
index: index,
state: stats['state'],
uptime: stats['uptime']
state: stats[:state],
uptime: stats[:uptime]
}
end

def add_port_info(presented_stats, stats)
if stats[:stats][:net_info]
presented_stats[:instance_ports] = net_info_to_instance_ports(stats[:stats][:net_info])
else
presented_stats[:port] = stats[:stats][:port]
end
end

def net_info_to_instance_ports(net_info)
net_info[:ports].map do |ports|
{
external: ports[:host_port],
internal: ports[:container_port],
}
end
end
end
end
37 changes: 19 additions & 18 deletions lib/cloud_controller/diego/instances_reporter.rb
Expand Up @@ -101,25 +101,26 @@ def stats_for_app(app)
for_each_desired_instance(instances, app) do |instance|
usage = instance[:stats] || {}
info = {
'state' => instance[:state],
'stats' => {
'name' => app.name,
'uris' => app.uris,
'host' => instance[:host],
'port' => instance[:port],
'uptime' => instance[:uptime],
'mem_quota' => app[:memory] * 1024 * 1024,
'disk_quota' => app[:disk_quota] * 1024 * 1024,
'fds_quota' => app.file_descriptors,
'usage' => {
'time' => usage[:time] || Time.now.utc.to_s,
'cpu' => usage[:cpu] || 0,
'mem' => usage[:mem] || 0,
'disk' => usage[:disk] || 0,
state: instance[:state],
stats: {
name: app.name,
uris: app.uris,
host: instance[:host],
port: instance[:port],
net_info: instance[:net_info],
uptime: instance[:uptime],
mem_quota: app[:memory] * 1024 * 1024,
disk_quota: app[:disk_quota] * 1024 * 1024,
fds_quota: app.file_descriptors,
usage: {
time: usage[:time] || Time.now.utc.to_s,
cpu: usage[:cpu] || 0,
mem: usage[:mem] || 0,
disk: usage[:disk] || 0,
}
}
}
info['details'] = instance[:details] if instance[:details]
info[:details] = instance[:details] if instance[:details]
result[instance[:index]] = info
end

Expand Down Expand Up @@ -147,8 +148,8 @@ def fill_unreported_instances_with_down_instances(reported_instances, app)
app.instances.times do |i|
unless reported_instances[i]
reported_instances[i] = {
'state' => 'DOWN',
'uptime' => 0,
state: 'DOWN',
uptime: 0,
}
end
end
Expand Down
98 changes: 92 additions & 6 deletions spec/request/processes_spec.rb
Expand Up @@ -147,6 +147,34 @@
end

describe 'GET /v3/processes/:guid/stats' do
it 'succeeds when TPS is an older version without net_info' do
process = VCAP::CloudController::ProcessModel.make(:process, type: 'worker', app: app_model, space: space, diego: true)

usage_time = Time.now.utc.to_s
tps_response = [{
process_guid: process.guid,
instance_guid: 'instance-A',
index: 0,
state: 'RUNNING',
details: 'some-details',
uptime: 1,
since: 101,
host: 'toast',
port: 8080,
stats: { time: usage_time, cpu: 80, mem: 128, disk: 1024 }
}].to_json

process_guid = VCAP::CloudController::Diego::ProcessGuid.from_app(process)
stub_request(:get, "http://tps.service.cf.internal:1518/v1/actual_lrps/#{process_guid}/stats").to_return(status: 200, body: tps_response)

get "/v3/apps/#{app_model.guid}/processes/worker/stats", nil, developer_headers

parsed_response = MultiJson.load(last_response.body)

expect(last_response.status).to eq(200)
expect(parsed_response['resources'][0]['port']).to eq(8080)
end

it 'retrieves the stats for a process' do
process = VCAP::CloudController::ProcessModel.make(:process, type: 'worker', space: space, diego: true)

Expand All @@ -160,7 +188,13 @@
uptime: 1,
since: 101,
host: 'toast',
port: 8080,
net_info: {
address: 'host',
ports: [
{ container_port: 7890, host_port: 5432 },
{ container_port: 8080, host_port: 1234 }
]
},
stats: { time: usage_time, cpu: 80, mem: 128, disk: 1024 }
}].to_json

Expand All @@ -180,8 +214,17 @@
'mem' => 128,
'disk' => 1024,
},
'host' => 'toast',
'port' => 8080,
'host' => 'toast',
'instance_ports' => [
{
'external' => 5432,
'internal' => 7890
},
{
'external' => 1234,
'internal' => 8080
}
],
'uptime' => 1,
'mem_quota' => 1073741824,
'disk_quota' => 1073741824,
Expand Down Expand Up @@ -543,7 +586,7 @@
end

describe 'GET /v3/apps/:guid/processes/:type/stats' do
it 'retrieves the stats for a process belonging to an app' do
it 'succeeds when TPS is an older version without net_info' do
process = VCAP::CloudController::ProcessModel.make(:process, type: 'worker', app: app_model, space: space, diego: true)

usage_time = Time.now.utc.to_s
Expand All @@ -565,6 +608,40 @@

get "/v3/apps/#{app_model.guid}/processes/worker/stats", nil, developer_headers

parsed_response = MultiJson.load(last_response.body)

expect(last_response.status).to eq(200)
expect(parsed_response['resources'][0]['port']).to eq(8080)
end

it 'retrieves the stats for a process belonging to an app' do
process = VCAP::CloudController::ProcessModel.make(:process, type: 'worker', app: app_model, space: space, diego: true)

usage_time = Time.now.utc.to_s
tps_response = [{
process_guid: process.guid,
instance_guid: 'instance-A',
index: 0,
state: 'RUNNING',
details: 'some-details',
uptime: 1,
since: 101,
host: 'toast',
net_info: {
address: 'host',
ports: [
{ container_port: 7890, host_port: 5432 },
{ container_port: 8080, host_port: 1234 }
]
},
stats: { time: usage_time, cpu: 80, mem: 128, disk: 1024 }
}].to_json

process_guid = VCAP::CloudController::Diego::ProcessGuid.from_app(process)
stub_request(:get, "http://tps.service.cf.internal:1518/v1/actual_lrps/#{process_guid}/stats").to_return(status: 200, body: tps_response)

get "/v3/apps/#{app_model.guid}/processes/worker/stats", nil, developer_headers

expected_response = {
'resources' => [{
'type' => 'worker',
Expand All @@ -576,8 +653,17 @@
'mem' => 128,
'disk' => 1024,
},
'host' => 'toast',
'port' => 8080,
'host' => 'toast',
'instance_ports' => [
{
'external' => 5432,
'internal' => 7890
},
{
'external' => 1234,
'internal' => 8080
}
],
'uptime' => 1,
'mem_quota' => 1073741824,
'disk_quota' => 1073741824,
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/controllers/v3/processes_controller_spec.rb
Expand Up @@ -790,7 +790,7 @@
describe '#stats' do
let(:process_type) { VCAP::CloudController::AppFactory.make(diego: true, type: 'potato') }
let(:space) { process_type.space }
let(:stats) { { 0 => { 'stats' => { 'usage' => {} } } } }
let(:stats) { { 0 => { stats: { usage: {}, net_info: { ports: [] } } } } }
let(:instances_reporters) { double(:instances_reporters) }

before do
Expand Down

0 comments on commit b90e782

Please sign in to comment.