From 09f4c9ea3838ffc0854a39d31f03706e0aae3dd5 Mon Sep 17 00:00:00 2001 From: Ariel Salomon Date: Tue, 14 May 2013 09:20:40 -0700 Subject: [PATCH 1/2] Fix Gemfile source --- Gemfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 40aa6e5..a8441b9 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,4 @@ -source :gemcutter +source "https://rubygems.org" gem "rake" From 1c474224354a3449f9f3d35da3d79bfd188b83e4 Mon Sep 17 00:00:00 2001 From: Ariel Salomon Date: Tue, 14 May 2013 09:28:02 -0700 Subject: [PATCH 2/2] Fix prefix handling to make update_counter work correctly - also fix increment/decrement of array values --- lib/statsd.rb | 9 ++------- spec/statsd_spec.rb | 24 +++++++++++++++++++----- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/lib/statsd.rb b/lib/statsd.rb index 3e1c178..090cd4c 100644 --- a/lib/statsd.rb +++ b/lib/statsd.rb @@ -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 diff --git a/spec/statsd_spec.rb b/spec/statsd_spec.rb index d3321f3..dff47c0 100644 --- a/spec/statsd_spec.rb +++ b/spec/statsd_spec.rb @@ -93,9 +93,8 @@ 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 @@ -103,10 +102,25 @@ 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