Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Namespace #3

Merged
merged 2 commits into from Apr 18, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/statsd.rb
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
21 changes: 20 additions & 1 deletion spec/statsd_spec.rb
@@ -1,4 +1,4 @@
require 'spec/helper'
require 'helper'

describe Statsd do
before do
Expand Down 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