Skip to content

Commit

Permalink
Merge remote-tracking branches 'jnunemaker/namespace' and 'jnunemaker…
Browse files Browse the repository at this point in the history
…/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
Rein Henrichs committed Apr 18, 2011
3 parents fba12c2 + 43e0f8e + 58fe06d commit 7937cfb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
12 changes: 11 additions & 1 deletion lib/statsd.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
# statsd.increment 'garets' # statsd.increment 'garets'
# statsd.timing 'glork', 320 # statsd.timing 'glork', 320
class Statsd class Statsd
attr_accessor :namespace

# @param [String] host your statsd host # @param [String] host your statsd host
# @param [Integer] port your statsd port # @param [Integer] port your statsd port
def initialize(host, port=8125) def initialize(host, port=8125)
Expand All @@ -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 # @param [Integer] sample_rate sample rate, 1 for always
def timing(stat, ms, sample_rate=1); send stat, ms, 'ms', sample_rate end 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 private


def sampled(sample_rate) def sampled(sample_rate)
yield unless sample_rate < 1 and rand > sample_rate yield unless sample_rate < 1 and rand > sample_rate
end end


def send(stat, delta, type, sample_rate) 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 end


def socket; @socket ||= UDPSocket.new end def socket; @socket ||= UDPSocket.new end
Expand Down
43 changes: 41 additions & 2 deletions spec/statsd_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'spec/helper' require 'helper'


describe Statsd do describe Statsd do
before do before do
Expand All @@ -23,7 +23,6 @@ def socket; @socket ||= FakeUDPSocket.new end
end end
end end



describe "#increment" do describe "#increment" do
it "should format the message according to the statsd spec" do it "should format the message according to the statsd spec" do
@statsd.increment('foobar') @statsd.increment('foobar')
Expand Down Expand Up @@ -69,6 +68,27 @@ def socket; @socket ||= FakeUDPSocket.new end
end 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 "#sampled" do
describe "when the sample rate is 1" do describe "when the sample rate is 1" do
it "should yield" do it "should yield" do
Expand Down Expand Up @@ -97,6 +117,25 @@ def socket; @socket ||= FakeUDPSocket.new end
end end
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 end


describe Statsd do describe Statsd do
Expand Down

0 comments on commit 7937cfb

Please sign in to comment.