Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kathap committed Jun 14, 2024
1 parent be5bd32 commit ae44b42
Showing 1 changed file with 62 additions and 63 deletions.
125 changes: 62 additions & 63 deletions lib/cloud_controller/diego/reporters/instances_stats_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,95 +12,94 @@ def initialize(bbs_instances_client, logstats_client)
end

def stats_for_app(process)
result = {}
current_time = Time.now.to_f
formatted_current_time = Time.now.to_datetime.rfc3339
begin
logger.debug('stats_for_app.fetching_container_metrics', process_guid: process.guid)
desired_lrp = bbs_instances_client.desired_lrp_instance(process)

log_cache_data, log_cache_errors = envelopes(desired_lrp, process)
stats = formatted_process_stats(log_cache_data, formatted_current_time)
quota_stats = formatted_quota_stats(log_cache_data)
isolation_segment = desired_lrp.PlacementTags.first

bbs_instances_client.lrp_instances(process).each do |actual_lrp|
index = actual_lrp.actual_lrp_key.index
next unless index < process.instances

state = LrpStateTranslator.translate_lrp_state(actual_lrp)
info = build_info(state, actual_lrp, process, stats, quota_stats, log_cache_errors, current_time, formatted_current_time, index)
info[:isolation_segment] = isolation_segment if isolation_segment
info[:details] = actual_lrp.placement_error if actual_lrp.placement_error.present?

info[:routable] = (actual_lrp.routable if actual_lrp.optional_routable)
result[actual_lrp.actual_lrp_key.index] = info
end

fill_unreported_instances_with_down_instances(result, process, flat: false)

warnings = [log_cache_errors].compact
[result, warnings]
rescue CloudController::Errors::NoRunningInstances
# case when no desired_lrp exists but an actual_lrp
handle_no_running_instances(result, process, log_cache_errors, current_time, formatted_current_time)
rescue StandardError => e
logger.error('stats_for_app.error', error: e.to_s)
raise e if e.is_a?(CloudController::Errors::ApiError) && e.name == 'ServiceUnavailable'

exception = CloudController::Errors::InstancesUnavailable.new(e.message)
exception.set_backtrace(e.backtrace)
raise exception
logger.debug('stats_for_app.fetching_container_metrics', process_guid: process.guid)
desired_lrp = bbs_instances_client.desired_lrp_instance(process)

log_cache_errors, stats, quota_stats, isolation_segment = get_stats(desired_lrp, process)

actual_lrp_info(process, stats, quota_stats, log_cache_errors, isolation_segment, true)
rescue CloudController::Errors::NoRunningInstances
# case when no desired_lrp exists but an actual_lrp
handle_no_running_instances(process)
rescue StandardError => e
logger.error('stats_for_app.error', error: e.to_s)
raise e if e.is_a?(CloudController::Errors::ApiError) && e.name == 'ServiceUnavailable'

exception = CloudController::Errors::InstancesUnavailable.new(e.message)
exception.set_backtrace(e.backtrace)
raise exception
end

private

attr_reader :bbs_instances_client

def get_stats(desired_lrp, process)
log_cache_data, log_cache_errors = envelopes(desired_lrp, process)
stats = formatted_process_stats(log_cache_data, Time.now.to_datetime.rfc3339)
quota_stats = formatted_quota_stats(log_cache_data)
isolation_segment = desired_lrp.PlacementTags.first
[log_cache_errors, stats, quota_stats, isolation_segment]
end

def actual_lrp_info(process, stats, quota_stats, log_cache_errors, isolation_segment, state=true)
result = {}
bbs_instances_client.lrp_instances(process).each do |actual_lrp|
next unless actual_lrp.actual_lrp_key.index < process.instances

state = state ? LrpStateTranslator.translate_lrp_state(actual_lrp) : VCAP::CloudController::Diego::LRP_STOPPING

info = build_info(state, actual_lrp, process, stats, quota_stats, log_cache_errors)
info[:isolation_segment] = isolation_segment unless isolation_segment.nil?
result[actual_lrp.actual_lrp_key.index] = info
end

fill_unreported_instances_with_down_instances(result, process, flat: false)

warnings = [log_cache_errors].compact
[result, warnings]
end

def build_info(state, actual_lrp, process, stats, quota_stats, log_cache_errors, current_time, formatted_current_time, index)
{
def actual_lrp_info_when_no_desired_lrp(process)
isolation_segment = nil
state = false
actual_lrp_info(process, nil, nil, nil, isolation_segment, state)
end

def build_info(state, actual_lrp, process, stats, quota_stats, log_cache_errors)
info = {
state: state,
stats: {
name: process.name,
uris: process.uris,
host: actual_lrp.actual_lrp_net_info.address,
port: get_default_port(actual_lrp.actual_lrp_net_info),
net_info: actual_lrp.actual_lrp_net_info.to_h,
uptime: nanoseconds_to_seconds((current_time * 1e9) - actual_lrp.since),
uptime: nanoseconds_to_seconds((Time.now.to_f * 1e9) - actual_lrp.since),
fds_quota: process.file_descriptors
}.merge(metrics_data_for_instance(stats, quota_stats, log_cache_errors, formatted_current_time, index))
}.merge(metrics_data_for_instance(stats, quota_stats, log_cache_errors, Time.now.to_datetime.rfc3339, actual_lrp.actual_lrp_key.index))
}
info[:details] = actual_lrp.placement_error if actual_lrp.placement_error.present?

info[:routable] = (actual_lrp.routable if actual_lrp.optional_routable)
info
end

def handle_no_running_instances(result, process, log_cache_errors, current_time, formatted_current_time)
def handle_no_running_instances(process)
if bbs_instances_client.lrp_instances(process).empty?
logger.info('stats_for_app.error', error: e.to_s)
[fill_unreported_instances_with_down_instances({}, process, flat: false), []]
else
state = VCAP::CloudController::Diego::LRP_STOPPING
logger.debug('Actual LRP found, setting state to STOPPING', process_guid: process.guid)
bbs_instances_client.lrp_instances(process).each do |actual_lrp|
index = actual_lrp.actual_lrp_key.index
next unless index < process.instances

info = build_info(state, actual_lrp, process, true, true, true, current_time, formatted_current_time, index)
info[:details] = actual_lrp.placement_error if actual_lrp.placement_error.present?

info[:routable] = (actual_lrp.routable if actual_lrp.optional_routable)
result[actual_lrp.actual_lrp_key.index] = info
end

warnings = [log_cache_errors].compact
[result, warnings]
actual_lrp_info_when_no_desired_lrp(process)
end
rescue CloudController::Errors::NoRunningInstances => e
logger.info('stats_for_app.error', error: e.to_s)
[fill_unreported_instances_with_down_instances({}, process, flat: false), []]
end

private

attr_reader :bbs_instances_client

def metrics_data_for_instance(stats, quota_stats, log_cache_errors, formatted_current_time, index)
if log_cache_errors.blank?
if !stats.nil? && log_cache_errors.blank?
{
mem_quota: quota_stats[index]&.memory_bytes_quota,
disk_quota: quota_stats[index]&.disk_bytes_quota,
Expand Down

0 comments on commit ae44b42

Please sign in to comment.