Skip to content

Commit

Permalink
Added concept of namespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnunemaker committed Mar 20, 2011
1 parent 80e154f commit 43e0f8e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/statsd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions spec/statsd_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 43e0f8e

Please sign in to comment.