Sha256: bf0e855baf32c109214b167a30d2d10fd529abc899e022c0b32363b9226b9ae8

Contents?: true

Size: 1.39 KB

Versions: 7

Compression:

Stored size: 1.39 KB

Contents

# encoding: utf-8
# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
require 'socket'

module NewRelic
  module Agent
    module Hostname
      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

      def self.get_fqdn
        %x[hostname -f].chomp!
      rescue => e
        NewRelic::Agent.logger.debug "Unable to determine fqdn #{e}"
        nil
      end

      def self.heroku_dyno_name_prefix(dyno_name)
        get_dyno_prefixes.find do |dyno_prefix|
          dyno_name.start_with?(dyno_prefix + ".")
        end
      end

      def self.get_dyno_prefixes
        ::NewRelic::Agent.config[:'heroku.dyno_name_prefixes_to_shorten']
      end

      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

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

      def self.get_external host_or_ip
        local?(host_or_ip) ? get : host_or_ip
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
newrelic_rpm-6.9.0.363 lib/new_relic/agent/hostname.rb
newrelic_rpm-6.8.0.360 lib/new_relic/agent/hostname.rb
newrelic_rpm-6.7.0.359 lib/new_relic/agent/hostname.rb
newrelic_rpm-6.6.0.358 lib/new_relic/agent/hostname.rb
newrelic_rpm-6.5.0.357 lib/new_relic/agent/hostname.rb
newrelic_rpm-6.4.0.356 lib/new_relic/agent/hostname.rb
newrelic_rpm-6.3.0.355 lib/new_relic/agent/hostname.rb