module NewRelic::Agent::Hostname

Constants

LOCALHOST

Public Class Methods

get() click to toggle source
# File lib/new_relic/agent/hostname.rb, line 11
def self.get
  dyno_name = ENV['DYNO']
  @hostname ||= if dyno_name && ::NewRelic::Agent.config[:'heroku.use_dyno_names']
    matching_prefix = heroku_dyno_name_prefix(dyno_name)
    dyno_name = "#{matching_prefix}.*" if matching_prefix
    dyno_name
  else
    Socket.gethostname.force_encoding(Encoding::UTF_8)
  end
end
get_dyno_prefixes() click to toggle source
# File lib/new_relic/agent/hostname.rb, line 46
def self.get_dyno_prefixes
  ::NewRelic::Agent.config[:'heroku.dyno_name_prefixes_to_shorten']
end
get_external(host_or_ip) click to toggle source
# File lib/new_relic/agent/hostname.rb, line 64
def self.get_external(host_or_ip)
  local?(host_or_ip) ? get : host_or_ip
end
get_fqdn() click to toggle source

Pass ‘-f’ to the external executable ‘hostname’ to request the fully qualified domain name (fqdn). For implementations of ‘hostname’ that do not support ‘-f’ (such as the one OpenBSD ships with), fall back to calling ‘hostname’ without the ‘-f’. If both ways of calling ‘hostname’ fail, or in a context where ‘hostname’ is not even available (within an AWS Lambda function, for example), call the ‘get’ method which uses Socket instead of an external executable.

# File lib/new_relic/agent/hostname.rb, line 29
def self.get_fqdn
  begin
    NewRelic::Helper.run_command('hostname -f')
  rescue NewRelic::CommandRunFailedError
    NewRelic::Helper.run_command('hostname')
  end
rescue NewRelic::CommandExecutableNotFoundError, NewRelic::CommandRunFailedError => e
  NewRelic::Agent.logger.debug("#{e.class} - #{e.message}")
  get
end
heroku_dyno_name_prefix(dyno_name) click to toggle source
# File lib/new_relic/agent/hostname.rb, line 40
def self.heroku_dyno_name_prefix(dyno_name)
  get_dyno_prefixes.find do |dyno_prefix|
    dyno_name.start_with?(dyno_prefix + '.')
  end
end
local?(host_or_ip) click to toggle source
# File lib/new_relic/agent/hostname.rb, line 60
def self.local?(host_or_ip)
  LOCALHOST.include?(host_or_ip)
end