Skip to content
This repository

Namespace #3

Merged
merged 2 commits into from about 3 years ago

2 participants

John Nunemaker Rein Henrichs
John Nunemaker

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

Rein Henrichs reinh merged commit 43e0f8e into from April 18, 2011
Rein Henrichs reinh closed this April 18, 2011
Rein Henrichs
Owner
reinh commented April 18, 2011

Merged (finally). Thanks!

Rein Henrichs
Owner
reinh commented April 18, 2011

Merged (finally). Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Mar 19, 2011
John Nunemaker rake spec failed. Since spec is added to load path in test task just …
…requiring helper fixed it.
80e154f
Mar 20, 2011
John Nunemaker Added concept of namespace. 43e0f8e
This page is out of date. Refresh to see the latest.
5  lib/statsd.rb
@@ -9,6 +9,8 @@
9 9
 #     statsd.increment 'garets'
10 10
 #     statsd.timing 'glork', 320
11 11
 class Statsd
  12
+  attr_accessor :namespace
  13
+  
12 14
   # @param [String] host your statsd host
13 15
   # @param [Integer] port your statsd port
14 16
   def initialize(host, port=8125)
@@ -40,7 +42,8 @@ def sampled(sample_rate)
40 42
   end
41 43
 
42 44
   def send(stat, delta, type, sample_rate)
43  
-    sampled(sample_rate) { socket.send("#{stat}:#{delta}|#{type}#{'|@' << sample_rate.to_s if sample_rate < 1}", 0, @host, @port) }
  45
+    prefix = "#{@namespace}." unless @namespace.nil?
  46
+    sampled(sample_rate) { socket.send("#{prefix}#{stat}:#{delta}|#{type}#{'|@' << sample_rate.to_s if sample_rate < 1}", 0, @host, @port) }
44 47
   end
45 48
 
46 49
   def socket; @socket ||= UDPSocket.new end
21  spec/statsd_spec.rb
... ...
@@ -1,4 +1,4 @@
1  
-require 'spec/helper'
  1
+require 'helper'
2 2
 
3 3
 describe Statsd do
4 4
   before do
@@ -97,6 +97,25 @@ def socket; @socket ||= FakeUDPSocket.new end
97 97
       end
98 98
     end
99 99
   end
  100
+
  101
+  describe "with namespace" do
  102
+    before { @statsd.namespace = 'service' }
  103
+
  104
+    it "should add namespace to increment" do
  105
+      @statsd.increment('foobar')
  106
+      @statsd.socket.recv.must_equal ['service.foobar:1|c']
  107
+    end
  108
+
  109
+    it "should add namespace to decrement" do
  110
+      @statsd.decrement('foobar')
  111
+      @statsd.socket.recv.must_equal ['service.foobar:-1|c']
  112
+    end
  113
+
  114
+    it "should add namespace to timing" do
  115
+      @statsd.timing('foobar', 500)
  116
+      @statsd.socket.recv.must_equal ['service.foobar:500|ms']
  117
+    end
  118
+  end
100 119
 end
101 120
 
102 121
 describe Statsd do
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.