Namespace #3

Merged
merged 2 commits into from Apr 18, 2011
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)
@@ -40,7 +42,8 @@ def sampled(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
@@ -97,6 +97,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