Module: NewRelic::Agent::Hostname

Defined in:
lib/new_relic/agent/hostname.rb

Constant Summary collapse

LOCALHOST =
%w[
  localhost
  0.0.0.0
  127.0.0.1
  0:0:0:0:0:0:0:1
  0:0:0:0:0:0:0:0
  ::1
  ::
].freeze

Class Method Summary collapse

Class Method Details

.getObject

[View source]

11
12
13
14
15
16
17
18
19
20
# 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_prefixesObject

[View source]

46
47
48
# 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) ⇒ Object

[View source]

64
65
66
# 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_fqdnObject

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.

[View source]

29
30
31
32
33
34
35
36
37
38
# 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) ⇒ Object

[View source]

40
41
42
43
44
# 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) ⇒ Boolean

Returns:

  • (Boolean)
[View source]

60
61
62
# File 'lib/new_relic/agent/hostname.rb', line 60

def self.local?(host_or_ip)
  LOCALHOST.include?(host_or_ip)
end