Skip to content

Commit

Permalink
Add gauges; clean up stat parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ariel Salomon authored and R. Tyler Croy committed Jun 26, 2012
1 parent 833875e commit 40411de
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
10 changes: 10 additions & 0 deletions lib/statsd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,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
11 changes: 9 additions & 2 deletions lib/statsd/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Server
FLUSH_INTERVAL = 10
COUNTERS = {}
TIMERS = {}
GAUGES = {}

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

def receive_data(msg)
Expand All @@ -37,12 +40,16 @@ def receive_data(msg)
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
puts "Invalid statistic #{fields.inspect} received; ignoring"
end
end
end
Expand Down
18 changes: 18 additions & 0 deletions spec/statsd_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,22 @@
c.decrement('foo')
end
end

describe '#gauge' do
let(:c) { Statsd::Client.new }

it 'should encode the values correctly' do
c.should_receive(:send_stats).with do |array|
array.should include('foo:1|g')
array.should include('bar:2|g')
end
c.gauge('foo' => 1, 'bar' => 2)
end

it 'should prepend the prefix if it has one' do
c.prefix = 'dev'
c.should_receive(:send_stats).with(['dev.foo:1|g'])
c.gauge('foo' => 1)
end
end
end
2 changes: 1 addition & 1 deletion statsd.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Gem::Specification.new do |s|
s.name = "lookout-statsd"
s.version = "0.5.#{ENV['BUILD_NUMBER'] || 'dev'}"
s.version = "0.6.#{ENV['BUILD_NUMBER'] || 'dev'}"
s.platform = Gem::Platform::RUBY

s.authors = ['R. Tyler Croy', 'Andrew Coldham', 'Ben VandenBos']
Expand Down

0 comments on commit 40411de

Please sign in to comment.