Skip to content

Commit

Permalink
Fix prefix handling to make update_counter work correctly
Browse files Browse the repository at this point in the history
 - also fix increment/decrement of array values
  • Loading branch information
Ariel Salomon committed May 14, 2013
1 parent 09f4c9e commit 1c47422
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
9 changes: 2 additions & 7 deletions lib/statsd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,19 @@ def timing(stat, time = nil, sample_rate = 1)

# +stats+ can be a string or an array of strings
def increment(stats, sample_rate = 1)
if @prefix
stats = "#{@prefix}.#{stats}"
end
update_counter stats, 1, sample_rate
end

# +stats+ can be a string or an array of strings
def decrement(stats, sample_rate = 1)
if @prefix
stats = "#{@prefix}.#{stats}"
end
update_counter stats, -1, sample_rate
end

# +stats+ can be a string or array of strings
def update_counter(stats, delta = 1, sample_rate = 1)
stats = Array(stats)
send_stats(stats.map { |s| "#{s}:#{delta}|c" }, sample_rate)
p = @prefix ? "#{@prefix}." : '' # apply prefix to each
send_stats(stats.map { |s| "#{p}#{s}:#{delta}|c" }, sample_rate)
end

# +stats+ is a hash
Expand Down
24 changes: 19 additions & 5 deletions spec/statsd_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,34 @@
describe '#increment' do
let(:c) { Statsd::Client.new }

it 'should prepend the prefix if it has one' do
c.prefix = 'dev'
c.should_receive(:update_counter).with('dev.foo', anything(), anything())
it 'should update the counter by 1' do
c.should_receive(:update_counter).with('foo', 1, anything())
c.increment('foo')
end
end

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

it 'should update the counter by -1' do
c.should_receive(:update_counter).with('foo', -1, anything())
c.decrement('foo')
end
end

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

it 'should prepend the prefix if it has one' do
c.prefix = 'dev'
c.should_receive(:update_counter).with('dev.foo', anything(), anything())
c.decrement('foo')
c.should_receive(:send_stats).with(['dev.foo:123|c'], anything())
c.update_counter('foo', 123)
end

it 'should prepend multiple prefixes if it has one' do
c.prefix = 'dev'
c.should_receive(:send_stats).with(['dev.foo:123|c', 'dev.bar:123|c'], anything())
c.update_counter(['foo', 'bar'], 123)
end
end

Expand Down

1 comment on commit 1c47422

@rwygand
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Please sign in to comment.