Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Namespace #3

Merged
merged 2 commits into from

2 participants

@jnunemaker

Added the ability to easily namespace an instance of Statsd. Allows for easy tracking of different services/apps.

@reinh reinh merged commit 43e0f8e into from
@reinh
Owner

Merged (finally). Thanks!

@reinh
Owner

Merged (finally). Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 20, 2011
  1. @jnunemaker
  2. @jnunemaker

    Added concept of namespace.

    jnunemaker authored
This page is out of date. Refresh to see the latest.
Showing with 24 additions and 2 deletions.
  1. +4 −1 lib/statsd.rb
  2. +20 −1 spec/statsd_spec.rb
View
5 lib/statsd.rb
@@ -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
21 spec/statsd_spec.rb
@@ -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
Something went wrong with that request. Please try again.