From c7f521c833d0358c8062610bef242ba5a9f1a412 Mon Sep 17 00:00:00 2001 From: Manoj Mathai Date: Wed, 13 Jun 2012 17:14:47 +0530 Subject: [PATCH 1/3] All the stats methods can now be directly called on Statsd - Statsd.setup(:host => [, :port => ]) before any call --- lib/statsd.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/statsd.rb b/lib/statsd.rb index 5af9196..1f890cb 100644 --- a/lib/statsd.rb +++ b/lib/statsd.rb @@ -109,4 +109,19 @@ def send_to_socket(message) end def socket; @socket ||= UDPSocket.new end + + def self.setup(options) + params = [ + options[:host] || "localhost", + options[:port] + ].compact + @instance = self.new(*params) + @setup = true + end + + def self.method_missing(m, *args, &block) + raise Exception.new("Call 'setup' and let me know where statsd is!") unless @setup + @instance.__send__(m, *args, &block) + end end + From f09f0c714185bd85dd26f18c780b493a8782b28a Mon Sep 17 00:00:00 2001 From: Manoj Mathai Date: Wed, 13 Jun 2012 17:22:38 +0530 Subject: [PATCH 2/3] No need of @setup --- lib/statsd.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/statsd.rb b/lib/statsd.rb index 1f890cb..e443fab 100644 --- a/lib/statsd.rb +++ b/lib/statsd.rb @@ -116,11 +116,10 @@ def self.setup(options) options[:port] ].compact @instance = self.new(*params) - @setup = true end def self.method_missing(m, *args, &block) - raise Exception.new("Call 'setup' and let me know where statsd is!") unless @setup + raise Exception.new("Call 'setup' and let me know where statsd is!") unless @instance @instance.__send__(m, *args, &block) end end From d180f297a8c5cfd47e8664374572f09dd217727f Mon Sep 17 00:00:00 2001 From: Manoj Mathai Date: Wed, 13 Jun 2012 19:11:44 +0530 Subject: [PATCH 3/3] Added tests --- lib/statsd.rb | 10 +++++++-- spec/statsd_spec.rb | 50 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/lib/statsd.rb b/lib/statsd.rb index e443fab..f8a53aa 100644 --- a/lib/statsd.rb +++ b/lib/statsd.rb @@ -13,6 +13,8 @@ # statsd = Statsd.new('localhost').tap{|sd| sd.namespace = 'account'} # statsd.increment 'activate' class Statsd + class ConfigError < StandardError; end; + # A namespace to prepend to all statsd calls. attr_accessor :namespace @@ -110,16 +112,20 @@ def send_to_socket(message) def socket; @socket ||= UDPSocket.new end - def self.setup(options) + def self.setup(options={}) params = [ options[:host] || "localhost", options[:port] ].compact @instance = self.new(*params) end + + def self.clear_setup + @instance = nil + end def self.method_missing(m, *args, &block) - raise Exception.new("Call 'setup' and let me know where statsd is!") unless @instance + raise ConfigError.new("I'm not setup()") unless @instance @instance.__send__(m, *args, &block) end end diff --git a/spec/statsd_spec.rb b/spec/statsd_spec.rb index 6c6523c..7e69c7b 100644 --- a/spec/statsd_spec.rb +++ b/spec/statsd_spec.rb @@ -160,7 +160,6 @@ def socket; @socket ||= FakeUDPSocket.new end end describe "stat names" do - it "should accept anything as stat" do @statsd.increment(Object, 1) end @@ -176,7 +175,56 @@ class Statsd::SomeClass; end @statsd.increment('ray@hostname.blah|blah.blah:blah', 1) @statsd.socket.recv.must_equal ['ray_hostname.blah_blah.blah_blah:1|c'] end + end + describe "should support direct calls" do + before do + Statsd.clear_setup + Statsd.setup(:host => "localhost", :port => 1234) + @instance = Statsd.instance_variable_get('@instance') + class << @instance + public :sampled # we need to test this + attr_reader :host, :port # we also need to test this + def socket; @socket ||= FakeUDPSocket.new end + end + end + + it 'should complain if setup has not been called' do + Statsd.clear_setup + assert_raises(Statsd::ConfigError) do + Statsd.timing("dummy", rand) + end + end + + it 'should setup instance correctly' do + @instance.host.must_equal 'localhost' + @instance.port.must_equal 1234 + end + + it "should support increment" do + Statsd.increment('foobar') + @instance.socket.recv.must_equal ['foobar:1|c'] + end + + it "should support decrement" do + Statsd.decrement('foobar') + @instance.socket.recv.must_equal ['foobar:-1|c'] + end + + it "should support timing" do + t = 100 + Statsd.timing('foobar', t) + @instance.socket.recv.must_equal ["foobar:#{t}|ms"] + end + + it "should support time" do + Statsd.time('foobar') { sleep(0.001); 'test' } + @instance.socket.recv.must_equal ['foobar:1|ms'] + end + + it "should support sampled" do + Statsd.sampled(1) { :yielded }.must_equal :yielded + end end end