class NewRelic::Agent::AWSInfo

Constants

API_VERSION
INSTANCE_HOST
REMOTE_DATA_VALID_CHARS

Attributes

availability_zone[R]
instance_id[R]
instance_type[R]

Public Class Methods

new() click to toggle source
# File lib/new_relic/agent/aws_info.rb, line 12
def initialize
  handle_remote_calls do
    @instance_type = remote_fetch('instance-type')
    @instance_id = remote_fetch('instance-id')
    @availability_zone = remote_fetch('placement/availability-zone')
  end
end

Public Instance Methods

loaded?() click to toggle source
# File lib/new_relic/agent/aws_info.rb, line 20
def loaded?
  instance_type && instance_id && availability_zone
end
to_collector_hash() click to toggle source
# File lib/new_relic/agent/aws_info.rb, line 24
def to_collector_hash
  {
    :id => instance_id,
    :type => instance_type,
    :zone => availability_zone
  }
end

Private Instance Methods

filter_remote_data(data_str) click to toggle source
# File lib/new_relic/agent/aws_info.rb, line 73
def filter_remote_data(data_str)
  return nil unless data_str.kind_of?(String)
  return nil unless data_str.bytesize <= 255

  data_str.each_char do |ch|
    next if ch =~ REMOTE_DATA_VALID_CHARS
    code_point = ch[0].ord # this works in Ruby 1.8.7 - 2.1.2
    next if code_point >= 0x80

    return nil # it's in neither set of valid characters
  end

  data_str
end
handle_error(*messages) click to toggle source
# File lib/new_relic/agent/aws_info.rb, line 68
def handle_error(*messages)
  NewRelic::Agent.logger.debug(*messages)
  reset
end
handle_remote_calls() { || ... } click to toggle source
# File lib/new_relic/agent/aws_info.rb, line 56
def handle_remote_calls
  begin
    Timeout::timeout(1) do
      yield
    end
  rescue Timeout::Error
    handle_error "UtilizationData timed out fetching remote keys."
  rescue StandardError => e
    handle_error "UtilizationData encountered error fetching remote keys: #{e.message}"
  end
end
remote_fetch(remote_key) click to toggle source
# File lib/new_relic/agent/aws_info.rb, line 42
def remote_fetch(remote_key)
  uri = URI("http://#{INSTANCE_HOST}/#{API_VERSION}/meta-data/#{remote_key}")
  response = Net::HTTP::get(uri)

  data = filter_remote_data(response)

  unless data
    NewRelic::Agent.increment_metric('Supportability/utilization/aws/error')
    raise ResponseError, "Fetching instance metadata for #{remote_key.inspect} returned invalid data: #{response.inspect}"
  end

  data
end
reset() click to toggle source
# File lib/new_relic/agent/aws_info.rb, line 34
def reset
  @instance_type = @instance_id = @availability_zone = nil
end