Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

integrate the statsd-ruby client into the statsd repository #2

Merged
merged 47 commits into from

5 participants

Felix Gilcher Rein Henrichs John Nunemaker Ray Krueger Mahesh Murthy
Felix Gilcher
Owner

This way we can keep compatible versions in the same repo

Rein Henrichs and others added some commits
Rein Henrichs Initial commit to statsd. d74e7b0
Rein Henrichs Convert to minitest/spec 4cbb9de
Rein Henrichs Implement Statsd#increment b7c7b0c
Rein Henrichs Implement Statsd#decrement 6992f7d
Rein Henrichs Refactor Statsd#increment and #decrement to use #count 9700de4
Rein Henrichs Implement Statsd#timing 8ef437e
Rein Henrichs Refactor Statsd#timing and #count to use #send a480d98
Rein Henrichs Statsd supports sample rate 674ae6c
Rein Henrichs Refactored Statsd sampling 0219a8f
Rein Henrichs Add explaining comments to spec c191808
Rein Henrichs Make the client really work with a real UDP socket 6ec767a
Rein Henrichs Updated README eaec68b
Rein Henrichs Adding yard documentation 0bc7293
Rein Henrichs Make things private that should be private 5c670f3
Rein Henrichs Don't use the FakeUDPSocket for live tests so they work 8514326
Rein Henrichs Remove errant debug statement 296f268
Rein Henrichs Remove bundler
Seriously. Go away.
1a2acbd
Rein Henrichs reinh Fix some whitespace d85787d
Rein Henrichs reinh Refactor Statsd#sampled 334f665
Rein Henrichs reinh Fix spec names to match intended behavior 3f9d9c3
Rein Henrichs reinh Default port to statsd's default port, 8125 7c73c8d
Rein Henrichs Updating summary and description 50833ee
Rein Henrichs Version bump to 0.0.0 4b79780
Rein Henrichs Version bump to 0.1.0 bf8b589
Rein Henrichs Adding gemspec for bundler :( fba12c2
John Nunemaker jnunemaker rake spec failed. Since spec is added to load path in test task just …
…requiring helper fixed it.
80e154f
John Nunemaker jnunemaker Added #time method that tracks timing for block. 58fe06d
John Nunemaker jnunemaker Added concept of namespace. 43e0f8e
Rein Henrichs Merge remote-tracking branches 'jnunemaker/namespace' and 'jnunemaker…
…/time-method'

* jnunemaker/namespace:
  Added concept of namespace.
  rake spec failed. Since spec is added to load path in test task just requiring helper fixed it.

* jnunemaker/time-method:
  Added #time method that tracks timing for block.
  rake spec failed. Since spec is added to load path in test task just requiring helper fixed it.
7937cfb
Ray Krueger raykrueger Add debug logging abilities
Setting Statsd.logger to any std logger instance will log all stats using a log.debug block.
55ce9b7
Rein Henrichs reinh Merge pull request #9 from signal/logging
Add debug logging abilities
5a34ed3
Rein Henrichs reinh Add Ray Krueger as contributor for Statsd.logger (thanks!) 9060938
Rein Henrichs Rename gem to statsd-ruby cfc955e
Rein Henrichs Version bump to 0.2.0 d42d0e4
Rein Henrichs Regenerate gemspec for version 0.2.0 1a9e53b
Rein Henrichs Better documentation! ZOMG 6bf6ad9
Rein Henrichs Version bump to 0.2.1 bcac191
Rein Henrichs Regenerate gemspec for version 0.2.1 e6ab90c
Ray Krueger raykrueger Specs for logging changes
Better late than never
58033b6
Ray Krueger raykrueger Replace :: with . as a delimiter, also replace reserved chars [:|@] w…
…ith _

Cleaning up stat names to remove statsd reserved characters. Sending improperly formatted stat names will crash statsd.
Also replacing :: with . allows for ruby class names to be used as stat names.
cfbc556
Mahesh Murthy maheshmurthy Adding an 'enabled' flag.
This allows Statd to be configured, but disabled. For example...

:config/initializers/statsd.rb
enabled = (Rails.env.staging? || Rails.env.production?)
Statsd.logger = Rails.logger
StatsdMonitor = Statsd.new("10.178.0.20", 8125, enabled)

This is extremely useful for local debugging. You can see the stats, without sending any data.
1682317
Ray Krueger raykrueger Removing the enabled flag as it isn't very useful f8a090f
Rein Henrichs reinh Merge pull request #11 from signal/statsd
---

Here weve added an enabled flag that of course defaults to true.
Also the stat name changes that prevent crashing of the statsd server, and allowing for ruby class names as stat names.

The enabled flag works really well with the logging addition from earlier...
    
    :config/initializers/statsd.rb
    enabled = (Rails.env.staging? || Rails.env.production?)
    Statsd.logger = Rails.logger
    StatsdMonitor = Statsd.new("10.178.0.20", 8125, enabled)
    
This is extremely useful for local debugging. You can see the stats, without sending any data.

Also, there are specs for everything :) and I rebased it all on top of your 0.2.1 release.
c34edb1
Rein Henrichs reinh Don't test coverage of gems, specs rcov. That's silly. 0fda6d4
Rein Henrichs reinh Version bump to 0.3.0 0f52511
Rein Henrichs reinh Regenerate gemspec for version 0.3.0 7f1c757
Felix Gilcher Xylakant Merge remote-tracking branch 'statsd-ruby/master' into feature/integr…
…ate-statsd-ruby
e84ae5d
Felix Gilcher Xylakant merged commit 573caad into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 18, 2011
  1. Initial commit to statsd.

    Rein Henrichs authored
  2. Convert to minitest/spec

    Rein Henrichs authored
Commits on Feb 19, 2011
  1. Implement Statsd#increment

    Rein Henrichs authored
  2. Implement Statsd#decrement

    Rein Henrichs authored
  3. Refactor Statsd#increment and #decrement to use #count

    Rein Henrichs authored
  4. Implement Statsd#timing

    Rein Henrichs authored
  5. Refactor Statsd#timing and #count to use #send

    Rein Henrichs authored
  6. Statsd supports sample rate

    Rein Henrichs authored
  7. Refactored Statsd sampling

    Rein Henrichs authored
  8. Add explaining comments to spec

    Rein Henrichs authored
  9. Make the client really work with a real UDP socket

    Rein Henrichs authored
  10. Updated README

    Rein Henrichs authored
  11. Adding yard documentation

    Rein Henrichs authored
  12. Make things private that should be private

    Rein Henrichs authored
  13. Don't use the FakeUDPSocket for live tests so they work

    Rein Henrichs authored
  14. Remove errant debug statement

    Rein Henrichs authored
  15. Remove bundler

    Rein Henrichs authored
    Seriously. Go away.
Commits on Feb 20, 2011
  1. Rein Henrichs

    Fix some whitespace

    reinh authored
  2. Rein Henrichs

    Refactor Statsd#sampled

    reinh authored
  3. Rein Henrichs
  4. Rein Henrichs
Commits on Mar 7, 2011
  1. Updating summary and description

    Rein Henrichs authored
  2. Version bump to 0.0.0

    Rein Henrichs authored
  3. Version bump to 0.1.0

    Rein Henrichs authored
  4. Adding gemspec for bundler :(

    Rein Henrichs authored
Commits on Mar 20, 2011
  1. John Nunemaker
  2. John Nunemaker
  3. John Nunemaker

    Added concept of namespace.

    jnunemaker authored
Commits on Apr 18, 2011
  1. Merge remote-tracking branches 'jnunemaker/namespace' and 'jnunemaker…

    Rein Henrichs authored
    …/time-method'
    
    * jnunemaker/namespace:
      Added concept of namespace.
      rake spec failed. Since spec is added to load path in test task just requiring helper fixed it.
    
    * jnunemaker/time-method:
      Added #time method that tracks timing for block.
      rake spec failed. Since spec is added to load path in test task just requiring helper fixed it.
Commits on Jun 23, 2011
  1. Ray Krueger

    Add debug logging abilities

    raykrueger authored
    Setting Statsd.logger to any std logger instance will log all stats using a log.debug block.
  2. Rein Henrichs

    Merge pull request #9 from signal/logging

    reinh authored
    Add debug logging abilities
  3. Rein Henrichs
  4. Rename gem to statsd-ruby

    Rein Henrichs authored
  5. Version bump to 0.2.0

    Rein Henrichs authored
  6. Regenerate gemspec for version 0.2.0

    Rein Henrichs authored
  7. Better documentation! ZOMG

    Rein Henrichs authored
  8. Version bump to 0.2.1

    Rein Henrichs authored
  9. Regenerate gemspec for version 0.2.1

    Rein Henrichs authored
  10. Ray Krueger

    Specs for logging changes

    raykrueger authored
    Better late than never
  11. Ray Krueger

    Replace :: with . as a delimiter, also replace reserved chars [:|@] w…

    raykrueger authored
    …ith _
    
    Cleaning up stat names to remove statsd reserved characters. Sending improperly formatted stat names will crash statsd.
    Also replacing :: with . allows for ruby class names to be used as stat names.
Commits on Jun 24, 2011
  1. Mahesh Murthy Ray Krueger

    Adding an 'enabled' flag.

    maheshmurthy authored raykrueger committed
    This allows Statd to be configured, but disabled. For example...
    
    :config/initializers/statsd.rb
    enabled = (Rails.env.staging? || Rails.env.production?)
    Statsd.logger = Rails.logger
    StatsdMonitor = Statsd.new("10.178.0.20", 8125, enabled)
    
    This is extremely useful for local debugging. You can see the stats, without sending any data.
  2. Ray Krueger
  3. Rein Henrichs

    Merge pull request #11 from signal/statsd

    reinh authored
    ---
    
    Here weve added an enabled flag that of course defaults to true.
    Also the stat name changes that prevent crashing of the statsd server, and allowing for ruby class names as stat names.
    
    The enabled flag works really well with the logging addition from earlier...
        
        :config/initializers/statsd.rb
        enabled = (Rails.env.staging? || Rails.env.production?)
        Statsd.logger = Rails.logger
        StatsdMonitor = Statsd.new("10.178.0.20", 8125, enabled)
        
    This is extremely useful for local debugging. You can see the stats, without sending any data.
    
    Also, there are specs for everything :) and I rebased it all on top of your 0.2.1 release.
  4. Rein Henrichs
  5. Rein Henrichs

    Version bump to 0.3.0

    reinh authored
  6. Rein Henrichs
Commits on Jan 27, 2012
  1. Felix Gilcher
This page is out of date. Refresh to see the latest.
5 .document
View
@@ -0,0 +1,5 @@
+lib/**/*.rb
+bin/*
+-
+features/**/*.feature
+LICENSE.txt
42 .gitignore
View
@@ -0,0 +1,42 @@
+# rcov generated
+coverage
+
+# rdoc generated
+rdoc
+
+# yard generated
+doc
+.yardoc
+
+# bundler
+.bundle
+
+# jeweler generated
+pkg
+
+# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
+#
+# * Create a file at ~/.gitignore
+# * Include files you want ignored
+# * Run: git config --global core.excludesfile ~/.gitignore
+#
+# After doing this, these files will be ignored in all your git projects,
+# saving you from having to 'pollute' every project you touch with them
+#
+# Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
+#
+# For MacOS:
+#
+#.DS_Store
+#
+# For TextMate
+#*.tmproj
+#tmtags
+#
+# For emacs:
+#*~
+#\#*
+#.\#*
+#
+# For vim:
+#*.swp
20 LICENSE.txt
View
@@ -0,0 +1,20 @@
+Copyright (c) 2011 Rein Henrichs
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 README.rdoc
View
@@ -0,0 +1,29 @@
+= statsd
+
+A Ruby statsd client (https://github.com/etsy/statsd)
+
+= Testing
+
+Run the specs with <tt>rake spec</tt>
+
+Run the specs and include live integration specs with <tt>LIVE=true rake spec</tt>. Note: This will test over a real UDP socket.
+
+== Contributing to statsd
+
+* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
+* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
+* Fork the project
+* Start a feature/bugfix branch
+* Commit and push until you are happy with your contribution
+* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
+* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
+
+== Contributors
+
+* Rein Henrichs
+* Ray Krueger
+
+== Copyright
+
+Copyright (c) 2011 Rein Henrichs. See LICENSE.txt for
+further details.
39 Rakefile
View
@@ -0,0 +1,39 @@
+require 'rubygems'
+require 'rake'
+
+require 'jeweler'
+Jeweler::Tasks.new do |gem|
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
+ gem.name = "statsd-ruby"
+ gem.homepage = "http://github.com/reinh/statsd"
+ gem.license = "MIT"
+ gem.summary = %Q{A Statsd client in Ruby}
+ gem.description = %Q{A Statsd client in Ruby}
+ gem.email = "rein@phpfog.com"
+ gem.authors = ["Rein Henrichs"]
+ gem.add_development_dependency "minitest", ">= 0"
+ gem.add_development_dependency "yard", "~> 0.6.0"
+ gem.add_development_dependency "jeweler", "~> 1.5.2"
+ gem.add_development_dependency "rcov", ">= 0"
+end
+Jeweler::RubygemsDotOrgTasks.new
+
+require 'rake/testtask'
+Rake::TestTask.new(:spec) do |spec|
+ spec.libs << 'lib' << 'spec'
+ spec.pattern = 'spec/**/*_spec.rb'
+ spec.verbose = true
+end
+
+require 'rcov/rcovtask'
+Rcov::RcovTask.new do |spec|
+ spec.libs << 'lib' << 'spec'
+ spec.pattern = 'spec/**/*_spec.rb'
+ spec.verbose = true
+ spec.rcov_opts << "--exclude spec,gems"
+end
+
+task :default => :spec
+
+require 'yard'
+YARD::Rake::YardocTask.new
1  VERSION
View
@@ -0,0 +1 @@
+0.3.0
98 lib/statsd.rb
View
@@ -0,0 +1,98 @@
+require 'socket'
+
+# = Statsd: A Statsd client (https://github.com/etsy/statsd)
+#
+# @example Set up a global Statsd client for a server on localhost:9125
+# $statsd = Statsd.new 'localhost', 8125
+# @example Send some stats
+# $statsd.increment 'garets'
+# $statsd.timing 'glork', 320
+# @example Use {#time} to time the execution of a block
+# $statsd.time('account.activate') { @account.activate! }
+# @example Create a namespaced statsd client and increment 'account.activate'
+# statsd = Statsd.new('localhost').tap{|sd| sd.namespace = 'account'}
+# statsd.increment 'activate'
+class Statsd
+ # A namespace to prepend to all statsd calls.
+ attr_accessor :namespace
+
+ #characters that will be replaced with _ in stat names
+ RESERVED_CHARS_REGEX = /[\:\|\@]/
+
+ class << self
+ # Set to any standard logger instance (including stdlib's Logger) to enable
+ # stat logging using logger.debug
+ attr_accessor :logger
+ end
+
+ # @param [String] host your statsd host
+ # @param [Integer] port your statsd port
+ def initialize(host, port=8125)
+ @host, @port = host, port
+ end
+
+ # Sends an increment (count = 1) for the given stat to the statsd server.
+ #
+ # @param stat (see #count)
+ # @param sample_rate (see #count)
+ # @see #count
+ def increment(stat, sample_rate=1); count stat, 1, sample_rate end
+
+ # Sends a decrement (count = -1) for the given stat to the statsd server.
+ #
+ # @param stat (see #count)
+ # @param sample_rate (see #count)
+ # @see #count
+ def decrement(stat, sample_rate=1); count stat, -1, sample_rate end
+
+ # Sends an arbitrary count for the given stat to the statsd server.
+ #
+ # @param [String] stat stat name
+ # @param [Integer] count count
+ # @param [Integer] sample_rate sample rate, 1 for always
+ def count(stat, count, sample_rate=1); send stat, count, 'c', sample_rate end
+
+ # Sends a timing (in ms) for the given stat to the statsd server. The
+ # sample_rate determines what percentage of the time this report is sent. The
+ # statsd server then uses the sample_rate to correctly track the average
+ # timing for the stat.
+ #
+ # @param stat stat name
+ # @param [Integer] ms timing in milliseconds
+ # @param [Integer] sample_rate sample rate, 1 for always
+ def timing(stat, ms, sample_rate=1); send stat, ms, 'ms', sample_rate end
+
+ # Reports execution time of the provided block using {#timing}.
+ #
+ # @param stat (see #timing)
+ # @param sample_rate (see #timing)
+ # @yield The operation to be timed
+ # @see #timing
+ # @example Report the time (in ms) taken to activate an account
+ # $statsd.time('account.activate') { @account.activate! }
+ def time(stat, sample_rate=1)
+ start = Time.now
+ result = yield
+ timing(stat, ((Time.now - start) * 1000).round, sample_rate)
+ result
+ end
+
+ private
+
+ def sampled(sample_rate)
+ yield unless sample_rate < 1 and rand > sample_rate
+ end
+
+ def send(stat, delta, type, sample_rate)
+ prefix = "#{@namespace}." unless @namespace.nil?
+ stat = stat.to_s.gsub('::', '.').gsub(RESERVED_CHARS_REGEX, '_')
+ sampled(sample_rate) { send_to_socket("#{prefix}#{stat}:#{delta}|#{type}#{'|@' << sample_rate.to_s if sample_rate < 1}") }
+ end
+
+ def send_to_socket(message)
+ self.class.logger.debug {"Statsd: #{message}"} if self.class.logger
+ socket.send(message, 0, @host, @port)
+ end
+
+ def socket; @socket ||= UDPSocket.new end
+end
33 spec/helper.rb
View
@@ -0,0 +1,33 @@
+require 'rubygems'
+require 'minitest/autorun'
+
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+require 'statsd'
+require 'logger'
+
+class FakeUDPSocket
+ def initialize
+ @buffer = []
+ end
+
+ def send(message, *rest)
+ @buffer.push [message]
+ end
+
+ def recv
+ res = @buffer.shift
+ end
+
+ def clear
+ @buffer = []
+ end
+
+ def to_s
+ inspect
+ end
+
+ def inspect
+ "<FakeUDPSocket: #{@buffer.inspect}>"
+ end
+end
197 spec/statsd_spec.rb
View
@@ -0,0 +1,197 @@
+require 'helper'
+
+describe Statsd do
+ before do
+ @statsd = Statsd.new('localhost', 1234)
+ class << @statsd
+ 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
+
+ after { @statsd.socket.clear }
+
+ describe "#initialize" do
+ it "should set the host and port" do
+ @statsd.host.must_equal 'localhost'
+ @statsd.port.must_equal 1234
+ end
+
+ it "should default the port to 8125" do
+ Statsd.new('localhost').instance_variable_get('@port').must_equal 8125
+ end
+ end
+
+ describe "#increment" do
+ it "should format the message according to the statsd spec" do
+ @statsd.increment('foobar')
+ @statsd.socket.recv.must_equal ['foobar:1|c']
+ end
+
+ describe "with a sample rate" do
+ before { class << @statsd; def rand; 0; end; end } # ensure delivery
+ it "should format the message according to the statsd spec" do
+ @statsd.increment('foobar', 0.5)
+ @statsd.socket.recv.must_equal ['foobar:1|c|@0.5']
+ end
+ end
+ end
+
+ describe "#decrement" do
+ it "should format the message according to the statsd spec" do
+ @statsd.decrement('foobar')
+ @statsd.socket.recv.must_equal ['foobar:-1|c']
+ end
+
+ describe "with a sample rate" do
+ before { class << @statsd; def rand; 0; end; end } # ensure delivery
+ it "should format the message according to the statsd spec" do
+ @statsd.decrement('foobar', 0.5)
+ @statsd.socket.recv.must_equal ['foobar:-1|c|@0.5']
+ end
+ end
+ end
+
+ describe "#timing" do
+ it "should format the message according to the statsd spec" do
+ @statsd.timing('foobar', 500)
+ @statsd.socket.recv.must_equal ['foobar:500|ms']
+ end
+
+ describe "with a sample rate" do
+ before { class << @statsd; def rand; 0; end; end } # ensure delivery
+ it "should format the message according to the statsd spec" do
+ @statsd.timing('foobar', 500, 0.5)
+ @statsd.socket.recv.must_equal ['foobar:500|ms|@0.5']
+ end
+ end
+ end
+
+ describe "#time" do
+ it "should format the message according to the statsd spec" do
+ @statsd.time('foobar') { sleep(0.001); 'test' }
+ @statsd.socket.recv.must_equal ['foobar:1|ms']
+ end
+
+ it "should return the result of the block" do
+ result = @statsd.time('foobar') { sleep(0.001); 'test' }
+ result.must_equal 'test'
+ end
+
+ describe "with a sample rate" do
+ 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) { sleep(0.001); 'test' }
+ @statsd.socket.recv.must_equal ['foobar:1|ms|@0.5']
+ end
+ end
+ end
+
+ describe "#sampled" do
+ describe "when the sample rate is 1" do
+ it "should yield" do
+ @statsd.sampled(1) { :yielded }.must_equal :yielded
+ end
+ end
+
+ describe "when the sample rate is greater than a random value [0,1]" do
+ before { class << @statsd; def rand; 0; end; end } # ensure delivery
+ it "should yield" do
+ @statsd.sampled(0.5) { :yielded }.must_equal :yielded
+ end
+ end
+
+ describe "when the sample rate is less than a random value [0,1]" do
+ before { class << @statsd; def rand; 1; end; end } # ensure no delivery
+ it "should not yield" do
+ @statsd.sampled(0.5) { :yielded }.must_equal nil
+ end
+ end
+
+ describe "when the sample rate is equal to a random value [0,1]" do
+ before { class << @statsd; def rand; 0.5; end; end } # ensure delivery
+ it "should yield" do
+ @statsd.sampled(0.5) { :yielded }.must_equal :yielded
+ 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
+
+ describe "with logging" do
+ require 'stringio'
+ before { Statsd.logger = Logger.new(@log = StringIO.new)}
+
+ it "should write to the log in debug" do
+ Statsd.logger.level = Logger::DEBUG
+
+ @statsd.increment('foobar')
+
+ @log.string.must_match "Statsd: foobar:1|c"
+ end
+
+ it "should not write to the log unless debug" do
+ Statsd.logger.level = Logger::INFO
+
+ @statsd.increment('foobar')
+
+ @log.string.must_be_empty
+ end
+
+ end
+
+ describe "stat names" do
+
+ it "should accept anything as stat" do
+ @statsd.increment(Object, 1)
+ end
+
+ it "should replace ruby constant delimeter with graphite package name" do
+ class Statsd::SomeClass; end
+ @statsd.increment(Statsd::SomeClass, 1)
+
+ @statsd.socket.recv.must_equal ['Statsd.SomeClass:1|c']
+ end
+
+ it "should replace statsd reserved chars in the stat name" do
+ @statsd.increment('ray@hostname.blah|blah.blah:blah', 1)
+ @statsd.socket.recv.must_equal ['ray_hostname.blah_blah.blah_blah:1|c']
+ end
+
+ end
+
+end
+
+describe Statsd do
+ describe "with a real UDP socket" do
+ it "should actually send stuff over the socket" do
+ socket = UDPSocket.new
+ host, port = 'localhost', 12345
+ socket.bind(host, port)
+
+ statsd = Statsd.new(host, port)
+ statsd.increment('foobar')
+ message = socket.recvfrom(16).first
+ message.must_equal 'foobar:1|c'
+ end
+ end
+end if ENV['LIVE']
57 statsd-ruby.gemspec
View
@@ -0,0 +1,57 @@
+# Generated by jeweler
+# DO NOT EDIT THIS FILE DIRECTLY
+# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{statsd-ruby}
+ s.version = "0.3.0"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Rein Henrichs"]
+ s.date = %q{2011-06-24}
+ s.description = %q{A Statsd client in Ruby}
+ s.email = %q{rein@phpfog.com}
+ s.extra_rdoc_files = [
+ "LICENSE.txt",
+ "README.rdoc"
+ ]
+ s.files = [
+ ".document",
+ "LICENSE.txt",
+ "README.rdoc",
+ "Rakefile",
+ "VERSION",
+ "lib/statsd.rb",
+ "spec/helper.rb",
+ "spec/statsd_spec.rb",
+ "statsd-ruby.gemspec"
+ ]
+ s.homepage = %q{http://github.com/reinh/statsd}
+ s.licenses = ["MIT"]
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.9.1}
+ s.summary = %q{A Statsd client in Ruby}
+
+ if s.respond_to? :specification_version then
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
+ s.add_development_dependency(%q<minitest>, [">= 0"])
+ s.add_development_dependency(%q<yard>, ["~> 0.6.0"])
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
+ s.add_development_dependency(%q<rcov>, [">= 0"])
+ else
+ s.add_dependency(%q<minitest>, [">= 0"])
+ s.add_dependency(%q<yard>, ["~> 0.6.0"])
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
+ s.add_dependency(%q<rcov>, [">= 0"])
+ end
+ else
+ s.add_dependency(%q<minitest>, [">= 0"])
+ s.add_dependency(%q<yard>, ["~> 0.6.0"])
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
+ s.add_dependency(%q<rcov>, [">= 0"])
+ end
+end
+
Something went wrong with that request. Please try again.