class Vitals::Reporters::DnsResolvingStatsdReporter

Public Class Methods

new(host: 'localhost', port: 8125, format: nil) click to toggle source
# File lib/vitals/reporters/dns_resolving_statsd_reporter.rb, line 6
def initialize(host: 'localhost', port: 8125, format: nil)
  @host = host
  @port = port
  @format = format
  setup_statsd
end

Private Instance Methods

query_dns() click to toggle source
# File lib/vitals/reporters/dns_resolving_statsd_reporter.rb, line 32
def query_dns
  ress = Resolv::DNS.open { |dns| dns.getresource(@host, Resolv::DNS::Resource::IN::A) }
  [ress.address.to_s, ress.ttl]
end
setup_statsd() click to toggle source
# File lib/vitals/reporters/dns_resolving_statsd_reporter.rb, line 14
def setup_statsd
  ip = @host
  unless (@host =~ Resolv::AddressRegex || @host == 'localhost'.freeze)
    ip, ttl = query_dns
    Thread.new do
      while true do
        sleep ttl
        previous_ip = ip
        ip, ttl = query_dns
        if ip != previous_ip
          @statsd = Statsd.new(ip, @port)
        end
      end
    end
  end
  @statsd = Statsd.new(ip, @port)
end