class NewRelic::Agent::Utilization::AWS

Constants

IMDS_BASE_URL
IMDS_KEYS
IMDS_TOKEN_TTL_SECS
TOKEN_OPEN_TIMEOUT_SECS
TOKEN_READ_TIMEOUT_SECS

Public Class Methods

imds_token() click to toggle source
# File lib/new_relic/agent/utilization/aws.rb, line 18
def imds_token
  uri = URI.parse("#{IMDS_BASE_URL}/api/token")
  http = Net::HTTP.new(uri.hostname)
  http.open_timeout = TOKEN_OPEN_TIMEOUT_SECS
  http.read_timeout = TOKEN_READ_TIMEOUT_SECS
  response = http.send_request('PUT',
    uri.path,
    '',
    {'X-aws-ec2-metadata-token-ttl-seconds' => IMDS_TOKEN_TTL_SECS})
  unless response.code == Vendor::SUCCESS
    NewRelic::Agent.logger.debug('Failed to obtain an AWS token for use with IMDS - encountered ' \
                                 "#{response.class} with HTTP response code #{response.code} - " \
                                 'assuming non AWS')
    return
  end

  response.body
rescue Net::OpenTimeout
  NewRelic::Agent.logger.debug('Timed out waiting for AWS IMDS - assuming non AWS')
end