class Statsd::Graphite

Attributes

counters[RW]
flush_interval[RW]
timers[RW]

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/statsd/graphite.rb, line 7
def initialize(*args)
  puts args
  super
  # stuff here...
end

Public Instance Methods

flush_stats() click to toggle source

def unbind

p ' connection totally closed'
EventMachine::stop_event_loop

end

# File lib/statsd/graphite.rb, line 29
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        
    
    # store counters
    counters.each_pair do |key,value|
      message = "stats.#{key} #{value} #{ts}\n"
      stat_string += message
      counters[key] = 0

      num_stats += 1
    end

    # store timers
    timers.each_pair do |key, values|
      if (values.length > 0) 
        pct_threshold = 90
        values.sort!
        count = values.count
        min = values.first
        max = values.last

        mean = min
        max_at_threshold = max

        if (count > 1)
          # strip off the top 100-threshold
          threshold_index = (((100 - pct_threshold) / 100.0) * count).round
          values = values[0..-threshold_index]
          max_at_threshold = values.last

          # average the remaining timings
          sum = values.inject( 0 ) { |s,x| s+x }
          mean = sum / values.count
        end

        message = ""
        message += "stats.timers.#{key}.mean #{mean} #{ts}\n"
        message += "stats.timers.#{key}.upper #{max} #{ts}\n"
        message += "stats.timers.#{key}.upper_#{pct_threshold} #{max_at_threshold} #{ts}\n"
        message += "stats.timers.#{key}.lower #{min} #{ts}\n"
        message += "stats.timers.#{key}.count #{count} #{ts}\n"
        stat_string += message

        timers[key] = []
              
        num_stats += 1
      end
    end
    stat_string += "statsd.numStats #{num_stats} #{ts}\n"
    
  end 
  # send to graphite
  send_data stat_string
  puts "complete. (#{time.round(3)}s)"
  close_connection_after_writing
end
post_init() click to toggle source
# File lib/statsd/graphite.rb, line 13
def post_init
  # puts counters.size
  # send_data 'Hello'
  # puts 'hello'
  # close_connection_after_writing
end
receive_data(data) click to toggle source
# File lib/statsd/graphite.rb, line 20
def receive_data(data)
  p data      
end