Permalink
Browse files

Added concept of namespace.

  • Loading branch information...
1 parent 80e154f commit 43e0f8ec672576efeda6bfc57249b8eb8dc82cbf @jnunemaker jnunemaker committed Mar 20, 2011
Showing with 23 additions and 1 deletion.
  1. +4 −1 lib/statsd.rb
  2. +19 −0 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)
@@ -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
@@ -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

0 comments on commit 43e0f8e

Please sign in to comment.