Sha256: 31ae51be5f2255065cdb352f83e535d04c4fe747d05379f70234dd41b00b2370

Contents?: true

Size: 897 Bytes

Versions: 9

Compression:

Stored size: 897 Bytes

Contents

require 'statsd-ruby'
require 'resolv'

module Vitals::Reporters
  class DnsResolvingStatsdReporter < StatsdReporter
    def initialize(host: 'localhost', port: 8125, format: nil)
      @host = host
      @port = port
      @format = format
      setup_statsd
    end

    private
    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

    def query_dns
      ress = Resolv::DNS.open { |dns| dns.getresource(@host, Resolv::DNS::Resource::IN::A) }
      [ress.address.to_s, ress.ttl]
    end
  end
end


Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
vitals-0.12.0 lib/vitals/reporters/dns_resolving_statsd_reporter.rb
vitals-0.11.1 lib/vitals/reporters/dns_resolving_statsd_reporter.rb
vitals-0.11.0 lib/vitals/reporters/dns_resolving_statsd_reporter.rb
vitals-0.10.1 lib/vitals/reporters/dns_resolving_statsd_reporter.rb
vitals-0.10.0 lib/vitals/reporters/dns_resolving_statsd_reporter.rb
vitals-0.9.1 lib/vitals/reporters/dns_resolving_statsd_reporter.rb
vitals-0.9.0 lib/vitals/reporters/dns_resolving_statsd_reporter.rb
vitals-0.8.1 lib/vitals/reporters/dns_resolving_statsd_reporter.rb
vitals-0.8.0 lib/vitals/reporters/dns_resolving_statsd_reporter.rb