Permalink
Browse files

The statsd handler attempts to convert the value for any event of an …

…unknown type to an integer to send as a statsd count. If it cannot be converted, then a 1 will be sent as a count.
  • Loading branch information...
1 parent ebc646d commit 1b56ff2f84bf86a7204fddb1295821c9bb2337e7 Hayes Davis committed Nov 18, 2011
Showing with 25 additions and 2 deletions.
  1. +3 −1 README.md
  2. +7 −1 lib/camayoc/handlers/statsd.rb
  3. +15 −0 test/handlers/statsd_test.rb
View
@@ -137,7 +137,9 @@ graphite and statsd going, then you'll get pretty graphs.
This handler does not support logging details about events since this isn't
really what statsd and graphite are for. Any calls to the event method will be
-treated as count stats when sent to statsd.
+treated as counts in statsd. If the event value can be converted to an Integer
+using Ruby's Integer method, then it will be sent as the count. Otherwise, a
+value of 1 will be sent.
Memory
------
@@ -21,11 +21,17 @@ def initialize(options={})
def event(ev)
case ev.type
when :timing then timing(ev)
- else count(ev)
+ when :count then count(ev)
+ else other(ev)
end
end
private
+ def other(ev)
+ value = Integer(ev.value) rescue 1
+ send(ev.ns_stat,value,'c',ev.options[:sample_rate]||1)
+ end
+
def count(ev)
send(ev.ns_stat,ev.value,'c',ev.options[:sample_rate]||1)
end
@@ -19,6 +19,21 @@ def test_generic_sends_count_statsd_message
@statsd.event(Camayoc::StatEvent.new(:count,"foo:bar","beep",1,{}))
end
+ def test_any_other_event_type_is_treated_as_a_count_with_value_if_value_is_integer
+ expect_message("foo.bar.beep:25|c")
+ @statsd.event(Camayoc::StatEvent.new(:foo,"foo:bar","beep",25,{}))
+ end
+
+ def test_any_other_event_type_is_treated_as_a_count_with_value_if_value_is_string_integer_representation
+ expect_message("foo.bar.beep:31|c")
+ @statsd.event(Camayoc::StatEvent.new(:foo,"foo:bar","beep","31",{}))
+ end
+
+ def test_any_other_event_type_is_treated_as_a_count_of_1_if_value_is_not_convertable_to_integer
+ expect_message("foo.bar.beep:1|c")
+ @statsd.event(Camayoc::StatEvent.new(:foo,"foo:bar","beep",{:a=>50},{}))
+ end
+
private
def expect_message(message)
@statsd.instance_variable_get("@socket").expects(:send).with(message,0,"localhost",1234)

0 comments on commit 1b56ff2

Please sign in to comment.