Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactored Statsd sampling

  • Loading branch information...
commit 0219a8fc6dae4fa6b92a9a9cc5538dcef91ef136 1 parent 674ae6c
Rein Henrichs authored
Showing with 11 additions and 7 deletions.
  1. +3 −6 lib/statsd.rb
  2. +1 −0  spec/helper.rb
  3. +7 −1 spec/statsd_spec.rb
9 lib/statsd.rb
View
@@ -21,17 +21,14 @@ def timing(stat, ms, sample_rate=1)
end
def sampled(sample_rate)
- if sample_rate < 1
- yield if rand <= sample_rate
- else
- yield
- end
+ return if sample_rate < 1 and rand > sample_rate
+ yield
end
private
def send(stat, delta, type, sample_rate)
- socket.send("#{stat}:#{delta}|#{type}#{'|@' << sample_rate.to_s if sample_rate < 1}")
+ sampled(sample_rate) { socket.send("#{stat}:#{delta}|#{type}#{'|@' << sample_rate.to_s if sample_rate < 1}") }
end
def socket
1  spec/helper.rb
View
@@ -7,6 +7,7 @@
$stderr.puts "Run `bundle install` to install missing gems"
exit e.status_code
end
+
require 'minitest/autorun'
$LOAD_PATH.unshift(File.dirname(__FILE__))
8 spec/statsd_spec.rb
View
@@ -7,7 +7,9 @@ def socket
end
end
- before { @statsd = Statsd.new('localhost', 1234) }
+ before do
+ @statsd = Statsd.new('localhost', 1234)
+ end
after { @statsd.socket.clear }
describe "#initialize" do
@@ -17,6 +19,7 @@ def socket
end
end
+
describe "#increment" do
it "should format the message according to the statsd spec" do
@statsd.increment('foobar')
@@ -24,6 +27,7 @@ def socket
end
describe "with a sample rate" do
+ before { class << @statsd; def rand; 0; end; end}
it "should format the message according to the statsd spec" do
@statsd.increment('foobar', 0.5)
@statsd.socket.recv.must_equal ['foobar:1|c|@0.5']
@@ -38,6 +42,7 @@ def socket
end
describe "with a sample rate" do
+ before { class << @statsd; def rand; 0; end; end}
it "should format the message according to the statsd spec" do
@statsd.decrement('foobar', 0.5)
@statsd.socket.recv.must_equal ['foobar:-1|c|@0.5']
@@ -52,6 +57,7 @@ def socket
end
describe "with a sample rate" do
+ before { class << @statsd; def rand; 0; end; end}
it "should format the message according to the statsd spec" do
@statsd.timing('foobar', 500, 0.5)
@statsd.socket.recv.must_equal ['foobar:500|ms|@0.5']
Please sign in to comment.
Something went wrong with that request. Please try again.