diff --git a/source/code/plugin/health/aggregate_monitor.rb b/source/code/plugin/health/aggregate_monitor.rb index 794f716ce..10dbdc705 100644 --- a/source/code/plugin/health/aggregate_monitor.rb +++ b/source/code/plugin/health/aggregate_monitor.rb @@ -3,6 +3,12 @@ require_relative 'health_model_constants' require 'json' +# Require only when running inside container. +# otherwise unit tests will fail due to ApplicationInsightsUtility dependency on base omsagent ruby files. If you have your dev machine starting with omsagent-rs, then GOOD LUCK! +if Socket.gethostname.start_with?('omsagent-rs') + require_relative '../ApplicationInsightsUtility' +end + module HealthModel class AggregateMonitor attr_accessor :monitor_id, :monitor_instance_id, :state, :transition_date_time, :aggregation_algorithm, :aggregation_algorithm_params, :labels, :is_aggregate_monitor, :details @@ -16,6 +22,8 @@ class AggregateMonitor MonitorState::NONE => 5 } + @@telemetry_sent_hash = {} + # constructor def initialize( monitor_id, @@ -127,17 +135,43 @@ def calculate_percentage_state(monitor_set) #sort #TODO: What if sorted_filtered is empty? is that even possible? + log = HealthMonitorHelpers.get_log_handle sorted_filtered = sort_filter_member_monitors(monitor_set) state_threshold = @aggregation_algorithm_params['state_threshold'].to_f - size = sorted_filtered.size + if sorted_filtered.nil? + size = 0 + else + size = sorted_filtered.size + end + if size == 1 @state = sorted_filtered[0].state else count = ((state_threshold*size)/100).ceil index = size - count - @state = sorted_filtered[index].state + if sorted_filtered.nil? || sorted_filtered[index].nil? + @state = HealthMonitorStates::UNKNOWN + if !@@telemetry_sent_hash.key?(@monitor_instance_id) + log.debug "Adding to telemetry sent hash #{@monitor_instance_id}" + @@telemetry_sent_hash[@monitor_instance_id] = true + log.info "Index: #{index} size: #{size} Count: #{count}" + custom_error_event_map = {} + custom_error_event_map["count"] = count + custom_error_event_map["index"] = index + custom_error_event_map["size"] = size + if !sorted_filtered.nil? + sorted_filtered.each_index{|i| + custom_error_event_map[i] = sorted_filtered[i].state + } + end + ApplicationInsightsUtility.sendCustomEvent("PercentageStateCalculationErrorEvent", custom_error_event_map) + end + else + @state = sorted_filtered[index].state + end + @state end end