Permalink
Browse files

Merge branch 'master' into thread_safe_batching

* master:
  Fix typo in README example server config
  Fixed Statsd.gauge parameter name in documentation.
  Removed an unused variable from the statsd spec
  Removed an unused variable from spec/helper
  Add basic usage to docs
  adding postfix

Conflicts:
	lib/statsd.rb
  • Loading branch information...
2 parents 3d4d86c + 6da51fd commit 0274a41496ef9f2c5a710ac54251f292fb5af739 @raggi raggi committed Dec 5, 2012
Showing with 55 additions and 5 deletions.
  1. +17 −0 README.rdoc
  2. +12 −3 lib/statsd.rb
  3. +1 −1 spec/helper.rb
  4. +25 −1 spec/statsd_spec.rb
View
@@ -7,6 +7,23 @@ A Ruby client for {StatsD}[https://github.com/etsy/statsd]
Bundler:
gem "statsd-ruby", :require => "statsd"
+= Basic Usage
+
+ # Set up a global Statsd client for a server on localhost:9125
+ $statsd = Statsd.new 'localhost', 9125
+
+ # Send some stats
+ $statsd.increment 'garets'
+ $statsd.timing 'glork', 320
+ $statsd.gauge 'bork', 100
+
+ # Use {#time} to time the execution of a block
+ $statsd.time('account.activate') { @account.activate! }
+
+ # Create a namespaced statsd client and increment 'account.activate'
+ statsd = Statsd.new('localhost').tap{|sd| sd.namespace = 'account'}
+ statsd.increment 'activate'
+
= Testing
Run the specs with <tt>rake spec</tt>
View
@@ -42,7 +42,8 @@ class Statsd
class Batch < Statsd
extend Forwardable
- def_delegators :@statsd, :namespace, :namespace=, :host, :port, :prefix
+ def_delegators :@statsd,
+ :namespace, :namespace=, :host, :port, :prefix, :postfix
attr_accessor :batch_size
@@ -98,6 +99,9 @@ def send_to_socket(message)
# The default batch size for new batches (default: 10)
attr_accessor :batch_size
+ # 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
@@ -109,6 +113,7 @@ def initialize(host = '127.0.0.1', port = 8125)
self.host, self.port = host, port
@prefix = nil
@batch_size = 10
+ @postfix = nil
end
# @attribute [w] namespace
@@ -118,6 +123,10 @@ def namespace=(namespace)
@prefix = "#{namespace}."
end
+ def postfix=(pf)
+ @postfix = ".#{pf}"
+ end
+
# @attribute [w] host
# Writes are not thread safe.
def host=(host)
@@ -164,7 +173,7 @@ def count(stat, count, sample_rate=1)
# counters.
#
# @param [String] stat stat name.
- # @param [Numeric] gauge value.
+ # @param [Numeric] value gauge value.
# @param [Numeric] sample_rate sample rate, 1 for always
# @example Report the current user count:
# $statsd.gauge('user.count', User.count)
@@ -230,7 +239,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
@@ -17,7 +17,7 @@ def send(message, *rest)
end
def recv
- res = @buffer.shift
+ @buffer.shift
end
def clear
View
@@ -126,7 +126,7 @@ class Statsd
before { class << @statsd; def rand; 0; end; end } # ensure delivery
it "should format the message according to the statsd spec" do
- result = @statsd.time('foobar', 0.5) { 'test' }
+ @statsd.time('foobar', 0.5) { 'test' }
@socket.recv.must_equal ['foobar:0|ms|@0.5']
end
end
@@ -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 0274a41

Please sign in to comment.