diff --git a/lib/statsd/graphite.rb b/lib/statsd/graphite.rb index 8ca1a66..ff9b122 100644 --- a/lib/statsd/graphite.rb +++ b/lib/statsd/graphite.rb @@ -3,13 +3,13 @@ module Statsd class Graphite < EM::Connection attr_accessor :counters, :timers, :flush_interval - + def initialize(*args) puts args super # stuff here... end - + def post_init # puts counters.size # send_data 'Hello' @@ -18,21 +18,21 @@ def post_init end def receive_data(data) - p data + p data end # def unbind # p ' connection totally closed' # EventMachine::stop_event_loop # end - + def flush_stats print "#{Time.now} Flushing #{counters.count} counters and #{timers.count} timers to Graphite." stat_string = '' time = ::Benchmark.realtime do ts = Time.now.to_i - num_stats = 0 - + num_stats = 0 + # store counters counters.each_pair do |key,value| message = "stats.#{key} #{value} #{ts}\n" @@ -41,10 +41,10 @@ def flush_stats num_stats += 1 end - + # store timers timers.each_pair do |key, values| - if (values.length > 0) + if (values.length > 0) pct_threshold = 90 values.sort! count = values.count @@ -74,16 +74,16 @@ def flush_stats stat_string += message timers[key] = [] - + num_stats += 1 end end stat_string += "statsd.numStats #{num_stats} #{ts}\n" - - end + + end # send to graphite send_data stat_string - puts "complete. (#{time.round(3)}s)" + puts "complete. (#{(time*100).round()/100.0}s)" close_connection_after_writing end end diff --git a/stats.rb b/stats.rb index e13c817..2dfb566 100644 --- a/stats.rb +++ b/stats.rb @@ -1,3 +1,4 @@ +require 'rubygems' require 'eventmachine' require 'statsd' require 'statsd/server' @@ -14,7 +15,7 @@ APP_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']) + db.create_collection(collection_name, :capped => retention['capped'], :size => retention['cap_bytes']) end db.collection(collection_name).ensure_index([['ts', Mongo::ASCENDING]]) end @@ -25,27 +26,27 @@ Statsd::Mongo.retentions = APP_CONFIG['retentions'] Statsd::Mongo.flush_interval = APP_CONFIG['flush_interval'] EventMachine::run do - EventMachine::open_datagram_socket('127.0.0.1', 8125, Statsd::Server) + EventMachine::open_datagram_socket('127.0.0.1', 8125, Statsd::Server) EventMachine::add_periodic_timer(APP_CONFIG['flush_interval']) do counters,timers = Statsd::Server.get_and_clear_stats! - + # # Flush Adapters # # Mongo - # EM.defer do + # EM.defer do # Statsd::Mongo.flush_stats(counters,timers) # end # - + # Graphite EventMachine.connect APP_CONFIG['graphite_host'], APP_CONFIG['graphite_port'], Statsd::Graphite do |conn| conn.counters = counters conn.timers = timers conn.flush_interval = 10 conn.flush_stats - end + end end - - -end \ No newline at end of file + + +end