Permalink
Browse files

Support Lambdas as metric names

  • Loading branch information...
1 parent 03adcbe commit cb824858c2677c594cf846ae6c5b020cb6216caf @himynameisjonas himynameisjonas committed Oct 6, 2012
Showing with 14 additions and 4 deletions.
  1. +4 −4 lib/statsd/instrument.rb
  2. +10 −0 test/statsd-instrument_test.rb
View
8 lib/statsd/instrument.rb
@@ -30,7 +30,7 @@ module Instrument
def statsd_measure(method, name)
add_to_method(method, name, :measure) do |old_method, new_method, metric_name, *args|
define_method(new_method) do |*args, &block|
- StatsD.measure(metric_name) { send(old_method, *args, &block) }
+ StatsD.measure(metric_name.respond_to?(:call) ? metric_name.call(self) : metric_name) { send(old_method, *args, &block) }
end
end
end
@@ -47,7 +47,7 @@ def statsd_count_success(method, name)
truthiness = (yield(result) rescue false) if block_given?
result
ensure
- StatsD.increment("#{metric_name}." + (truthiness == false ? 'failure' : 'success'))
+ StatsD.increment("#{metric_name.respond_to?(:call) ? metric_name.call(self) : metric_name}." + (truthiness == false ? 'failure' : 'success'))
end
end
end
@@ -65,7 +65,7 @@ def statsd_count_if(method, name)
truthiness = (yield(result) rescue false) if block_given?
result
ensure
- StatsD.increment(metric_name) if truthiness
+ StatsD.increment(metric_name.respond_to?(:call) ? metric_name.call(self) : metric_name) if truthiness
end
end
end
@@ -74,7 +74,7 @@ def statsd_count_if(method, name)
def statsd_count(method, name)
add_to_method(method, name, :count) do |old_method, new_method, metric_name|
define_method(new_method) do |*args, &block|
- StatsD.increment(metric_name)
+ StatsD.increment(metric_name.respond_to?(:call) ? metric_name.call(self) : metric_name)
send(old_method, *args, &block)
end
end
View
10 test/statsd-instrument_test.rb
@@ -53,6 +53,9 @@ def purchase(arg)
end
end
+class GatewaySubClass < ActiveMerchant::Gateway
+end
+
ActiveMerchant::Base.extend StatsD::Instrument
class StatsDTest < Test::Unit::TestCase
@@ -128,6 +131,13 @@ def test_statsd_count
ActiveMerchant::Gateway.new.purchase(true)
end
+ def test_statsd_count_with_name_as_lambda
+ ActiveMerchant::Gateway.statsd_count(:ssl_post, lambda {|object| object.class.to_s.downcase + ".insert"})
+
+ StatsD.expects(:increment).with('gatewaysubclass.insert')
+ GatewaySubClass.new.purchase(true)
+ end
+
def test_statsd_count_with_method_receiving_block
ActiveMerchant::Base.statsd_count :post_with_block, 'ActiveMerchant.Base.post_with_block'

0 comments on commit cb82485

Please sign in to comment.