Class: Statsd::Daemon

Inherits:
Object
  • Object
show all
Defined in:
lib/statsd/daemon.rb

Instance Method Summary collapse

Instance Method Details

#run(options) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/statsd/daemon.rb', line 38

def run(options)
  config = if options[:config] and options[:config].is_a?(Hash)
             options[:config]
           elsif options[:config_file] and options[:config_file].is_a?(String)
             YAML::load(ERB.new(IO.read(options[:config_file])).result)
           end

  EventMachine::run do
    ## statsd->graphite aggregation
    if config['graphite_host']
      MessageDispatchDaemon.register_receiver(Statsd::Aggregator)
      EventMachine::add_periodic_timer(config['flush_interval']) do
        counters,timers = Statsd::Aggregator.get_and_clear_stats!
        EventMachine.connect config['graphite_host'], config['graphite_port'], Statsd::Graphite do |conn|
          conn.counters = counters
          conn.timers = timers
          conn.flush_interval = config['flush_interval']
          conn.flush_stats
        end
      end
      ##

      ## statsd->statsd data relay
      if config['forwarding']
        Statsd::Forwarder.set_destinations(config['forwarding_destinations'])
        MessageDispatchDaemon.register_receiver(Statsd::Forwarder)

        Statsd::Forwarder.build_fresh_sockets
        EventMachine::add_periodic_timer(config['forwarding_socket_lifetime']) do
          Statsd::Forwarder.build_fresh_sockets
        end
      end
      ##

      puts "Going to listen on #{config['bind']}:#{config['port']}"
      EventMachine::open_datagram_socket(config['bind'], config['port'], MessageDispatchDaemon)
    end
  end
end