Permalink
Browse files

adding postfix

  • Loading branch information...
1 parent 4a028fa commit 4827db56e9ca3cdfdeec0011ad5311c802a17a45 @jondot jondot committed Jul 31, 2012
Showing with 33 additions and 1 deletion.
  1. +9 −1 lib/statsd.rb
  2. +24 −0 spec/statsd_spec.rb
View
@@ -30,6 +30,9 @@ class Statsd
# StatsD port. Defaults to 8125.
attr_reader :port
+ # a postfix to append to all metrics
+ attr_reader :postfix
+
class << self
# Set to a standard logger instance to enable debug logging.
attr_accessor :logger
@@ -40,6 +43,7 @@ class << self
def initialize(host = '127.0.0.1', port = 8125)
self.host, self.port = host, port
@prefix = nil
+ @postfix = nil
end
# @attribute [w] namespace
@@ -49,6 +53,10 @@ def namespace=(namespace)
@prefix = "#{namespace}."
end
+ def postfix=(pf)
+ @postfix = ".#{pf}"
+ end
+
# @attribute [w] host
# Writes are not thread safe.
def host=(host)
@@ -137,7 +145,7 @@ def send_stats(stat, delta, type, sample_rate=1)
# Replace Ruby module scoping with '.' and reserved chars (: | @) with underscores.
stat = stat.to_s.gsub('::', '.').tr(':|@', '_')
rate = "|@#{sample_rate}" unless sample_rate == 1
- send_to_socket "#{@prefix}#{stat}:#{delta}|#{type}#{rate}"
+ send_to_socket "#{@prefix}#{stat}#{@postfix}:#{delta}|#{type}#{rate}"
end
end
View
@@ -189,6 +189,30 @@ class Statsd
end
end
+ describe "with postfix" do
+ before { @statsd.postfix = 'ip-23-45-56-78' }
+
+ it "should add postfix to increment" do
+ @statsd.increment('foobar')
+ @socket.recv.must_equal ['foobar.ip-23-45-56-78:1|c']
+ end
+
+ it "should add postfix to decrement" do
+ @statsd.decrement('foobar')
+ @socket.recv.must_equal ['foobar.ip-23-45-56-78:-1|c']
+ end
+
+ it "should add namespace to timing" do
+ @statsd.timing('foobar', 500)
+ @socket.recv.must_equal ['foobar.ip-23-45-56-78:500|ms']
+ end
+
+ it "should add namespace to gauge" do
+ @statsd.gauge('foobar', 500)
+ @socket.recv.must_equal ['foobar.ip-23-45-56-78:500|g']
+ end
+ end
+
describe "with logging" do
require 'stringio'
before { Statsd.logger = Logger.new(@log = StringIO.new)}

0 comments on commit 4827db5

Please sign in to comment.