Permalink
Browse files

Merge pull request #29 from justeat/sets

Add support for the "set" stat type.
  • Loading branch information...
2 parents 356d965 + dd6594c commit b9893d18577234144de9765bc8af20ec705ce6fc @raggi raggi committed Dec 11, 2012
Showing with 31 additions and 0 deletions.
  1. +16 −0 lib/statsd.rb
  2. +15 −0 spec/statsd_spec.rb
View
@@ -181,6 +181,22 @@ def gauge(stat, value, sample_rate=1)
send_stats stat, value, :g, sample_rate
end
+ # Sends an arbitary set value for the given stat to the statsd server.
+ #
+ # This is for recording counts of unique events, which are useful to
+ # see on graphs to correlate to other values. For example, a deployment
+ # might get recorded as a set, and be drawn as annotations on a CPU history
+ # graph.
+ #
+ # @param [String] stat stat name.
+ # @param [Numeric] value event value.
+ # @param [Numeric] sample_rate sample rate, 1 for always
+ # @example Report a deployment happening:
+ # $statsd.set('deployment', DEPLOYMENT_EVENT_CODE)
+ def set(stat, value, sample_rate=1)
+ send_stats stat, value, :s, 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
View
@@ -111,6 +111,21 @@ class Statsd
end
end
+ describe "#set" do
+ it "should format the message according to the statsd spec" do
+ @statsd.set('foobar', 765)
+ @socket.recv.must_equal ['foobar:765|s']
+ 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.set('foobar', 500, 0.5)
+ @socket.recv.must_equal ['foobar:500|s|@0.5']
+ end
+ end
+ end
+
describe "#time" do
it "should format the message according to the statsd spec" do
@statsd.time('foobar') { 'test' }

0 comments on commit b9893d1

Please sign in to comment.