Browse files

Merge pull request #15 from titanous/statsite

Statsite support
  • Loading branch information...
2 parents dd3da61 + 754a0dc commit 688c503c8ed2bfe94770423f475f6e3f12a735de @jstorimer jstorimer committed Jun 19, 2012
Showing with 34 additions and 7 deletions.
  1. +10 −7 lib/statsd/instrument.rb
  2. +24 −0 test/statsd-instrument_test.rb
View
17 lib/statsd/instrument.rb
@@ -12,11 +12,12 @@ def ms
module StatsD
class << self
attr_accessor :host, :port, :mode, :logger, :enabled, :default_sample_rate,
- :prefix
+ :prefix, :implementation
end
self.enabled = true
self.default_sample_rate = 1
-
+ self.implementation = :statsd
+
TimeoutClass = defined?(::SystemTimer) ? ::SystemTimer : ::Timeout
# StatsD.server = 'localhost:1234'
@@ -114,9 +115,10 @@ def self.increment(key, delta = 1, sample_rate = default_sample_rate)
write(key, delta, :incr, sample_rate)
end
- #gaugor:333|g
- def self.gauge(key, value, sample_rate = default_sample_rate)
- write(key, value, :g, sample_rate)
+ # gaugor:333|g
+ # guagor:1234|kv|@1339864935 (statsite)
+ def self.gauge(key, value, sample_rate_or_epoch = default_sample_rate)
+ write(key, value, :g, sample_rate_or_epoch)
end
private
@@ -136,10 +138,11 @@ def self.write(k,v,op, sample_rate = default_sample_rate)
when :ms
command << '|ms'
when :g
- command << '|g'
+ command << (self.implementation == :statsite ? '|kv' : '|g')
end
- command << "|@#{sample_rate}" if sample_rate < 1
+ command << "|@#{sample_rate}" if sample_rate < 1 || (self.implementation == :statsite && sample_rate > 1)
+ command << "\n" if self.implementation == :statsite
if mode.to_s == 'production'
socket_wrapper { socket.send(command, 0, host, port) }
View
24 test/statsd-instrument_test.rb
@@ -159,6 +159,30 @@ def test_write_supports_gauge_syntax
StatsD.gauge('fooy', 42)
end
+ def test_write_supports_statsite_gauge_syntax
+ StatsD.unstub(:gauge)
+
+ StatsD.mode = :production
+ StatsD.server = 'localhost:123'
+ StatsD.implementation = :statsite
+
+ UDPSocket.any_instance.expects(:send).with('fooy:42|kv', 0, 'localhost', 123)
+
+ StatsD.gauge('fooy', 42)
+ end
+
+ def test_write_supports_statsite_gauge_timestamp
+ StatsD.unstub(:gauge)
+
+ StatsD.mode = :production
+ StatsD.server = 'localhost:123'
+ StatsD.implementation = :statsite
+
+ UDPSocket.any_instance.expects(:send).with('fooy:42|kv|@123456', 0, 'localhost', 123)
+
+ StatsD.gauge('fooy', 42, 123456)
+ end
+
def test_should_not_write_when_disabled
StatsD.enabled = false
StatsD.expects(:logger).never

0 comments on commit 688c503

Please sign in to comment.