Skip to content

Commit

Permalink
Merge pull request lookout#2 from Oscil8/master
Browse files Browse the repository at this point in the history
Fix Gemfile, prefix handling
  • Loading branch information
Oscil8 committed May 14, 2013
2 parents 40411de + 1c47422 commit c5518a3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source :gemcutter
source "https://rubygems.org"

gem "rake"

Expand Down
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

0 comments on commit c5518a3

Please sign in to comment.