Class: Statsd::Server::Daemon

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

Instance Method Summary collapse

Instance Method Details

#run(options) ⇒ Object



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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/statsd/server.rb', line 47

def run(options)
  config = YAML::load(ERB.new(IO.read(options[:config])).result)

  if options[:mongo]
    require 'statsd/mongo'
    # Setup retention store
    db = ::Mongo::Connection.new(config['mongo_host']).db(config['mongo_database'])
    config['retentions'].each do |retention|
      collection_name = retention['name']
      unless db.collection_names.include?(collection_name)
        db.create_collection(collection_name, :capped => retention['capped'], :size => retention['cap_bytes']) 
      end
      db.collection(collection_name).ensure_index([['ts', ::Mongo::ASCENDING]])
    end        
    Statsd::Mongo.hostname = config['mongo_host']
    Statsd::Mongo.database = config['mongo_database']
    Statsd::Mongo.retentions = config['retentions']
    Statsd::Mongo.flush_interval = config['flush_interval']
  end

  if options[:graphite]
    require 'statsd/graphite' 
  end

  # Start the server
  EventMachine::run do
    EventMachine::open_datagram_socket(config['bind'], config['port'], Statsd::Server)  

    # Periodically Flush
    EventMachine::add_periodic_timer(config['flush_interval']) do
      counters,timers = Statsd::Server.get_and_clear_stats!

       # Flush Adapters
      if options[:mongo]
        EM.defer { Statsd::Mongo.flush_stats(counters,timers) } 
      end

      if options[:graphite]
        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
    
    end

  end

end