Permalink
Browse files

Merge remote-tracking branches 'jnunemaker/namespace' and 'jnunemaker…

…/time-method'

* jnunemaker/namespace:
  Added concept of namespace.
  rake spec failed. Since spec is added to load path in test task just requiring helper fixed it.

* jnunemaker/time-method:
  Added #time method that tracks timing for block.
  rake spec failed. Since spec is added to load path in test task just requiring helper fixed it.
  • Loading branch information...
3 parents fba12c2 + 43e0f8e + 58fe06d commit 7937cfb41c3aa2febf27829478885137d99849c4 Rein Henrichs committed Apr 18, 2011
Showing with 52 additions and 3 deletions.
  1. +11 −1 lib/statsd.rb
  2. +41 −2 spec/statsd_spec.rb
View
@@ -9,6 +9,8 @@
# statsd.increment 'garets'
# statsd.timing 'glork', 320
class Statsd
+ attr_accessor :namespace
+
# @param [String] host your statsd host
# @param [Integer] port your statsd port
def initialize(host, port=8125)
@@ -33,14 +35,22 @@ def count(stat, count, sample_rate=1); send stat, count, 'c', sample_rate end
# @param [Integer] sample_rate sample rate, 1 for always
def timing(stat, ms, sample_rate=1); send stat, ms, 'ms', sample_rate end
+ def time(stat, sample_rate=1)
+ start = Time.now
+ result = yield
+ timing(stat, ((Time.now - start) * 1000).round, sample_rate)
+ result
+ end
+
private
def sampled(sample_rate)
yield unless sample_rate < 1 and rand > sample_rate
end
def send(stat, delta, type, sample_rate)
- sampled(sample_rate) { socket.send("#{stat}:#{delta}|#{type}#{'|@' << sample_rate.to_s if sample_rate < 1}", 0, @host, @port) }
+ prefix = "#{@namespace}." unless @namespace.nil?
+ sampled(sample_rate) { socket.send("#{prefix}#{stat}:#{delta}|#{type}#{'|@' << sample_rate.to_s if sample_rate < 1}", 0, @host, @port) }
end
def socket; @socket ||= UDPSocket.new end
View
@@ -1,4 +1,4 @@
-require 'spec/helper'
+require 'helper'
describe Statsd do
before do
@@ -23,7 +23,6 @@ def socket; @socket ||= FakeUDPSocket.new end
end
end
-
describe "#increment" do
it "should format the message according to the statsd spec" do
@statsd.increment('foobar')
@@ -69,6 +68,27 @@ def socket; @socket ||= FakeUDPSocket.new end
end
end
+ describe "#time" do
+ it "should format the message according to the statsd spec" do
+ @statsd.time('foobar') { sleep(0.001); 'test' }
+ @statsd.socket.recv.must_equal ['foobar:1|ms']
+ end
+
+ it "should return the result of the block" do
+ result = @statsd.time('foobar') { sleep(0.001); 'test' }
+ result.must_equal 'test'
+ end
+
+ describe "with a sample rate" do
+ before { class << @statsd; def rand; 0; end; end } # ensure delivery
+
+ it "should format the message according to the statsd spec" do
+ result = @statsd.time('foobar', 0.5) { sleep(0.001); 'test' }
+ @statsd.socket.recv.must_equal ['foobar:1|ms|@0.5']
+ end
+ end
+ end
+
describe "#sampled" do
describe "when the sample rate is 1" do
it "should yield" do
@@ -97,6 +117,25 @@ def socket; @socket ||= FakeUDPSocket.new end
end
end
end
+
+ describe "with namespace" do
+ before { @statsd.namespace = 'service' }
+
+ it "should add namespace to increment" do
+ @statsd.increment('foobar')
+ @statsd.socket.recv.must_equal ['service.foobar:1|c']
+ end
+
+ it "should add namespace to decrement" do
+ @statsd.decrement('foobar')
+ @statsd.socket.recv.must_equal ['service.foobar:-1|c']
+ end
+
+ it "should add namespace to timing" do
+ @statsd.timing('foobar', 500)
+ @statsd.socket.recv.must_equal ['service.foobar:500|ms']
+ end
+ end
end
describe Statsd do

0 comments on commit 7937cfb

Please sign in to comment.