class NewRelic::Agent::HealthCheck
Constants
- AGENT_DISABLED
- APP_NAME_EXCEEDED
- FAILED_TO_CONNECT
- FAILED_TO_PARSE_CONFIG
- FORCED_DISCONNECT
- HEALTHY
- HTTP_ERROR
- INVALID_LICENSE_KEY
- MISSING_APP_NAME
- MISSING_LICENSE_KEY
- PROXY_CONFIG_ERROR
- SHUTDOWN
Public Class Methods
Source
# File lib/new_relic/agent/health_check.rb, line 8 def initialize @start_time = nano_time @continue = true @status = HEALTHY # the following assignments may set @continue = false if they are invalid set_enabled set_delivery_location set_frequency end
Public Instance Methods
Source
# File lib/new_relic/agent/health_check.rb, line 31 def create_and_run_health_check_loop return unless health_checks_enabled? && @continue NewRelic::Agent.logger.debug('Agent Control health check conditions met. Starting health checks.') NewRelic::Agent.record_metric('Supportability/AgentControl/Health/enabled', 1) Thread.new do while @continue begin sleep @frequency write_file @continue = false if @status == SHUTDOWN rescue StandardError => e NewRelic::Agent.logger.error("Aborting Agent Control health check. Error raised: #{e}") @continue = false end end end end
Source
# File lib/new_relic/agent/health_check.rb, line 58 def healthy? @status == HEALTHY end
Source
# File lib/new_relic/agent/health_check.rb, line 51 def update_status(status, options = []) return unless @continue @status = status.dup update_message(options) unless options.empty? end
Private Instance Methods
Source
# File lib/new_relic/agent/health_check.rb, line 94 def contents <<~CONTENTS healthy: #{@status[:healthy]} status: #{@status[:message]}#{last_error} start_time_unix_nano: #{@start_time} status_time_unix_nano: #{nano_time} CONTENTS end
Source
# File lib/new_relic/agent/health_check.rb, line 111 def file_name "health-#{NewRelic::Agent::GuidGenerator.generate_guid(32)}.yml" end
Source
# File lib/new_relic/agent/health_check.rb, line 124 def health_checks_enabled? @enabled && @delivery_location && @frequency > 0 end
Source
# File lib/new_relic/agent/health_check.rb, line 103 def last_error @status[:healthy] ? '' : "\nlast_error: #{@status[:last_error]}" end
Source
# File lib/new_relic/agent/health_check.rb, line 107 def nano_time Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond) end
Source
# File lib/new_relic/agent/health_check.rb, line 74 def set_delivery_location @delivery_location = if ENV['NEW_RELIC_AGENT_CONTROL_HEALTH_DELIVERY_LOCATION'] # The spec states file paths for the delivery location will begin with file:// # This does not create a valid path in Ruby, so remove the prefix when present ENV['NEW_RELIC_AGENT_CONTROL_HEALTH_DELIVERY_LOCATION']&.gsub('file://', '') else # The spec default is 'file:///newrelic/apm/health', but since we're just going to remove it anyway... '/newrelic/apm/health' end end
Source
# File lib/new_relic/agent/health_check.rb, line 64 def set_enabled @enabled = if ENV['NEW_RELIC_AGENT_CONTROL_ENABLED'] == 'true' true else NewRelic::Agent.logger.debug('NEW_RELIC_AGENT_CONTROL_ENABLED not true, disabling health checks') @continue = false false end end
Source
# File lib/new_relic/agent/health_check.rb, line 85 def set_frequency @frequency = ENV['NEW_RELIC_AGENT_CONTROL_HEALTH_FREQUENCY'] ? ENV['NEW_RELIC_AGENT_CONTROL_HEALTH_FREQUENCY'].to_i : 5 if @frequency <= 0 NewRelic::Agent.logger.debug('NEW_RELIC_AGENT_CONTROL_HEALTH_FREQUENCY zero or less, disabling health checks') @continue = false end end
Source
# File lib/new_relic/agent/health_check.rb, line 128 def update_message(options) @status[:message] = sprintf(@status[:message], *options) rescue StandardError => e NewRelic::Agent.logger.debug("Error raised while updating Agent Control health check message: #{e}." \ "options = #{options}, @status[:message] = #{@status[:message]}") end
Source
# File lib/new_relic/agent/health_check.rb, line 115 def write_file @file ||= "#{@delivery_location}/#{file_name}" File.write(@file, contents) rescue StandardError => e NewRelic::Agent.logger.error("Agent Control health check raised an error while writing a file: #{e}") @continue = false end