Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 5 files changed
  • 0 commit comments
  • 1 contributor
Commits on Jun 26, 2012
Ariel Salomon Remove useless version constant 833875e
Ariel Salomon Add gauges; clean up stat parsing 40411de
View
1  .gitignore
@@ -1,2 +1,3 @@
/pkg
.rvmrc
+Gemfile.lock
View
12 lib/statsd.rb
@@ -2,8 +2,6 @@
require 'resolv'
module Statsd
- Version = '0.0.6'
-
# initialize singleton instance in an initializer
def self.create_instance(opts={})
raise "Already initialized Statsd" if defined? @@instance
@@ -74,6 +72,16 @@ def update_counter(stats, delta = 1, sample_rate = 1)
send_stats(stats.map { |s| "#{s}:#{delta}|c" }, sample_rate)
end
+ # +stats+ is a hash
+ def gauge(stats)
+ send_stats(stats.map { |s,val|
+ if @prefix
+ s = "#{@prefix}.#{s}"
+ end
+ "#{s}:#{val}|g"
+ })
+ end
+
private
def send_stats(data, sample_rate = 1)
View
11 lib/statsd/server.rb
@@ -11,6 +11,7 @@ module Server
FLUSH_INTERVAL = 10
COUNTERS = {}
TIMERS = {}
+ GAUGES = {}
def post_init
puts "statsd server started!"
@@ -19,9 +20,11 @@ def post_init
def self.get_and_clear_stats!
counters = COUNTERS.dup
timers = TIMERS.dup
+ gauges = GAUGES.dup
COUNTERS.clear
TIMERS.clear
- [counters,timers]
+ GAUGES.clear
+ [counters,timers,gauges]
end
def receive_data(msg)
@@ -37,12 +40,16 @@ def receive_data(msg)
if (fields[1].strip == "ms")
TIMERS[key] ||= []
TIMERS[key].push(fields[0].to_i)
- else
+ elsif (fields[1].strip == "c")
if (fields[2] && fields[2].match(/^@([\d\.]+)/))
sample_rate = fields[2].match(/^@([\d\.]+)/)[1]
end
COUNTERS[key] ||= 0
COUNTERS[key] += (fields[0].to_i || 1) * (1.0 / sample_rate.to_f)
+ elsif (fields[1].strip == "g")
+ GAUGES[key] ||= (fields[0].to_i || 0)
+ else
+ puts "Invalid statistic #{fields.inspect} received; ignoring"
end
end
end
View
18 spec/statsd_spec.rb
@@ -109,4 +109,22 @@
c.decrement('foo')
end
end
+
+ describe '#gauge' do
+ let(:c) { Statsd::Client.new }
+
+ it 'should encode the values correctly' do
+ c.should_receive(:send_stats).with do |array|
+ array.should include('foo:1|g')
+ array.should include('bar:2|g')
+ end
+ c.gauge('foo' => 1, 'bar' => 2)
+ end
+
+ it 'should prepend the prefix if it has one' do
+ c.prefix = 'dev'
+ c.should_receive(:send_stats).with(['dev.foo:1|g'])
+ c.gauge('foo' => 1)
+ end
+ end
end
View
2  statsd.gemspec
@@ -2,7 +2,7 @@
Gem::Specification.new do |s|
s.name = "lookout-statsd"
- s.version = "0.5.#{ENV['BUILD_NUMBER'] || 'dev'}"
+ s.version = "0.6.#{ENV['BUILD_NUMBER'] || 'dev'}"
s.platform = Gem::Platform::RUBY
s.authors = ['R. Tyler Croy', 'Andrew Coldham', 'Ben VandenBos']

No commit comments for this range

Something went wrong with that request. Please try again.