Class: Statsd::Graphite

Inherits:
EM::Connection
  • Object
show all
Defined in:
lib/statsd/graphite.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#countersObject

Returns the value of attribute counters.



7
8
9
# File 'lib/statsd/graphite.rb', line 7

def counters
  @counters
end

#flush_intervalObject

Returns the value of attribute flush_interval.



7
8
9
# File 'lib/statsd/graphite.rb', line 7

def flush_interval
  @flush_interval
end

#timersObject

Returns the value of attribute timers.



7
8
9
# File 'lib/statsd/graphite.rb', line 7

def timers
  @timers
end

Instance Method Details

#flush_statsObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
# File 'lib/statsd/graphite.rb', line 9

def flush_stats
  puts "#{Time.now} Flushing #{counters.count} counters and #{timers.count} timers to Graphite."

  stat_string = ''

  ts = Time.now.to_i
  num_stats = 0

  # store counters
  counters.each_pair do |key,value|
    message = "#{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)
        # average all the timing data
        sum = values.inject( 0 ) { |s,x| s+x }
        mean = sum / values.count

        # 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
      end

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

      timers[key] = []

      num_stats += 1
    end
  end

  stat_string += "statsd.numStats #{num_stats} #{ts}\n"

  # send to graphite
  send_data stat_string
  close_connection_after_writing
end