Permalink
Browse files

Merge pull request #17 from langalex/master

Adds passing blocks to instrumented methods
  • Loading branch information...
2 parents 7423cc2 + fc50f5d commit e42bbea34cac4a297667bf3d014ba47af9d8d935 @jstorimer jstorimer committed Jul 9, 2012
Showing with 50 additions and 10 deletions.
  1. +9 −9 lib/statsd/instrument.rb
  2. +41 −1 test/statsd-instrument_test.rb
View
@@ -29,17 +29,17 @@ def self.server=(conn)
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|
- StatsD.measure(metric_name) { send(old_method, *args) }
+ define_method(new_method) do |*args, &block|
+ StatsD.measure(metric_name) { send(old_method, *args, &block) }
end
end
end
def statsd_count_success(method, name)
add_to_method(method, name, :count_success) do |old_method, new_method, metric_name|
- define_method(new_method) do |*args|
+ define_method(new_method) do |*args, &block|
begin
- truthiness = result = send(old_method, *args)
+ truthiness = result = send(old_method, *args, &block)
rescue
truthiness = false
raise
@@ -55,9 +55,9 @@ def statsd_count_success(method, name)
def statsd_count_if(method, name)
add_to_method(method, name, :count_if) do |old_method, new_method, metric_name|
- define_method(new_method) do |*args|
+ define_method(new_method) do |*args, &block|
begin
- truthiness = result = send(old_method, *args)
+ truthiness = result = send(old_method, *args, &block)
rescue
truthiness = false
raise
@@ -73,9 +73,9 @@ 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|
+ define_method(new_method) do |*args, &block|
StatsD.increment(metric_name)
- send(old_method, *args)
+ send(old_method, *args, &block)
end
end
end
@@ -103,7 +103,7 @@ def add_to_method(method, name, action, &block)
def self.measure(key, milli = nil)
result = nil
ms = milli || Benchmark.ms do
- result = yield
+ result = yield
end
write(key, ms, :ms)
@@ -20,6 +20,10 @@ def ssl_post(arg)
raise 'Not OK'
end
end
+
+ def post_with_block(&block)
+ yield if block_given?
+ end
end
class ActiveMerchant::Gateway < ActiveMerchant::Base
@@ -32,7 +36,7 @@ def purchase(arg)
def self.sync
true
- end
+ end
def self.singleton_class
class << self; self; end
@@ -65,6 +69,16 @@ def test_statsd_count_if
ActiveMerchant::Gateway.new.purchase(false)
end
+ def test_statsd_count_if_with_method_receiving_block
+ ActiveMerchant::Base.statsd_count_if :post_with_block, 'ActiveMerchant.Base.post_with_block' do |result|
+ result[:success]
+ end
+
+ return_value = ActiveMerchant::Base.new.post_with_block {'block called'}
+
+ assert_equal 'block called', return_value
+ end
+
def test_statsd_count_if_with_block
ActiveMerchant::UniqueGateway.statsd_count_if :ssl_post, 'ActiveMerchant.Gateway.block' do |result|
result[:success]
@@ -85,6 +99,16 @@ def test_statsd_count_success
ActiveMerchant::Gateway.new.purchase(false)
end
+ def test_statsd_count_success_with_method_receiving_block
+ ActiveMerchant::Base.statsd_count_success :post_with_block, 'ActiveMerchant.Base.post_with_block' do |result|
+ result[:success]
+ end
+
+ return_value = ActiveMerchant::Base.new.post_with_block {'block called'}
+
+ assert_equal 'block called', return_value
+ end
+
def test_statsd_count_success_with_block
ActiveMerchant::UniqueGateway.statsd_count_success :ssl_post, 'ActiveMerchant.Gateway' do |result|
result[:success]
@@ -104,13 +128,29 @@ def test_statsd_count
ActiveMerchant::Gateway.new.purchase(true)
end
+ def test_statsd_count_with_method_receiving_block
+ ActiveMerchant::Base.statsd_count :post_with_block, 'ActiveMerchant.Base.post_with_block'
+
+ return_value = ActiveMerchant::Base.new.post_with_block {'block called'}
+
+ assert_equal 'block called', return_value
+ end
+
def test_statsd_measure
ActiveMerchant::UniqueGateway.statsd_measure :ssl_post, 'ActiveMerchant.Gateway.ssl_post'
StatsD.expects(:write).with('ActiveMerchant.Gateway.ssl_post', is_a(Float), :ms).returns({:success => true})
ActiveMerchant::UniqueGateway.new.purchase(true)
end
+ def test_statsd_measure_with_method_receiving_block
+ ActiveMerchant::Base.statsd_measure :post_with_block, 'ActiveMerchant.Base.post_with_block'
+
+ return_value = ActiveMerchant::Base.new.post_with_block {'block called'}
+
+ assert_equal 'block called', return_value
+ end
+
def test_instrumenting_class_method
ActiveMerchant::Gateway.singleton_class.extend StatsD::Instrument
ActiveMerchant::Gateway.singleton_class.statsd_count :sync, 'ActiveMerchant.Gateway.sync'

0 comments on commit e42bbea

Please sign in to comment.