Skip to content

Commit

Permalink
Merge pull request #6 from gingerlime/master
Browse files Browse the repository at this point in the history
adding gauge support
  • Loading branch information
quasor committed Jan 18, 2013
2 parents ca10606 + 7ef8295 commit efe4cba
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ Sampling

Tells StatsD that this counter is being sent sampled ever 1/10th of the time.

Gauges
------

gaugor:333|g

StatsD now also supports gauges, arbitrary values, which can be recorded.

Guts
----
Expand Down
10 changes: 10 additions & 0 deletions lib/statsd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ def update_counter(stats, delta = 1, sample_rate = 1)
send_stats(stats.map { |s| "#{s}:#{delta}|c" }, sample_rate)
end

# +stats+ is a hash
def gauge(stats)
send_stats(stats.map { |s,val|
if @prefix
s = "#{@prefix}.#{s}"
end
"#{s}:#{val}|g"
})
end

private

def send_stats(data, sample_rate = 1)
Expand Down
12 changes: 10 additions & 2 deletions lib/statsd/graphite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'eventmachine'
module Statsd
class Graphite < EM::Connection
attr_accessor :counters, :timers, :flush_interval
attr_accessor :counters, :timers, :gauges, :flush_interval

def initialize(*args)
puts args
Expand All @@ -27,12 +27,20 @@ def receive_data(data)
# end

def flush_stats
print "#{Time.now} Flushing #{counters.count} counters and #{timers.count} timers to Graphite."
print "#{Time.now} Flushing #{gauges.count} gauges, #{counters.count} counters and #{timers.count} timers to Graphite."
stat_string = ''
time = ::Benchmark.realtime do
ts = Time.now.to_i
num_stats = 0

# store gauges
gauges.each_pair do |key,value|
message = "stats.gauges.#{key} #{value} #{ts}\n"
stat_string += message

num_stats += 1
end

# store counters
counters.each_pair do |key,value|
message = "stats.#{key} #{value} #{ts}\n"
Expand Down
16 changes: 13 additions & 3 deletions lib/statsd/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Server #< EM::Connection
FLUSH_INTERVAL = 10
COUNTERS = {}
TIMERS = {}
GAUGES = {}

def post_init
puts "statsd server started!"
Expand All @@ -16,9 +17,10 @@ def post_init
def self.get_and_clear_stats!
counters = COUNTERS.dup
timers = TIMERS.dup
gauges = GAUGES.dup
COUNTERS.clear
TIMERS.clear
[counters,timers]
[counters,timers,gauges]
end

def receive_data(msg)
Expand All @@ -29,15 +31,22 @@ def receive_data(msg)
bits.each do |record|
sample_rate = 1
fields = record.split("|")
if fields.nil? || fields.count < 2
next
end
if (fields[1].strip == "ms")
TIMERS[key] ||= []
TIMERS[key].push(fields[0].to_i)
else
elsif (fields[1].strip == "c")
if (fields[2] && fields[2].match(/^@([\d\.]+)/))
sample_rate = fields[2].match(/^@([\d\.]+)/)[1]
end
COUNTERS[key] ||= 0
COUNTERS[key] += (fields[0].to_i || 1) * (1.0 / sample_rate.to_f)
elsif (fields[1].strip == "g")
GAUGES[key] = (fields[0].to_i || 0)
else
print "Invalid statistic #{fields.inspect} received; ignoring"
end
end
end
Expand Down Expand Up @@ -74,7 +83,7 @@ def run(options)

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

# Flush Adapters
if options[:mongo]
Expand All @@ -85,6 +94,7 @@ def run(options)
EventMachine.connect config['graphite_host'], config['graphite_port'], Statsd::Graphite do |conn|
conn.counters = counters
conn.timers = timers
conn.gauges = gauges
conn.flush_interval = config['flush_interval']
conn.flush_stats
end
Expand Down

0 comments on commit efe4cba

Please sign in to comment.