Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Support Lambdas as metric names

  • Loading branch information...
commit cb824858c2677c594cf846ae6c5b020cb6216caf 1 parent 03adcbe
Jonas Forsberg himynameisjonas authored

Showing 2 changed files with 14 additions and 4 deletions. Show diff stats Hide diff stats

  1. +4 4 lib/statsd/instrument.rb
  2. +10 0 test/statsd-instrument_test.rb
8 lib/statsd/instrument.rb
@@ -30,7 +30,7 @@ module Instrument
30 30 def statsd_measure(method, name)
31 31 add_to_method(method, name, :measure) do |old_method, new_method, metric_name, *args|
32 32 define_method(new_method) do |*args, &block|
33   - StatsD.measure(metric_name) { send(old_method, *args, &block) }
  33 + StatsD.measure(metric_name.respond_to?(:call) ? metric_name.call(self) : metric_name) { send(old_method, *args, &block) }
34 34 end
35 35 end
36 36 end
@@ -47,7 +47,7 @@ def statsd_count_success(method, name)
47 47 truthiness = (yield(result) rescue false) if block_given?
48 48 result
49 49 ensure
50   - StatsD.increment("#{metric_name}." + (truthiness == false ? 'failure' : 'success'))
  50 + StatsD.increment("#{metric_name.respond_to?(:call) ? metric_name.call(self) : metric_name}." + (truthiness == false ? 'failure' : 'success'))
51 51 end
52 52 end
53 53 end
@@ -65,7 +65,7 @@ def statsd_count_if(method, name)
65 65 truthiness = (yield(result) rescue false) if block_given?
66 66 result
67 67 ensure
68   - StatsD.increment(metric_name) if truthiness
  68 + StatsD.increment(metric_name.respond_to?(:call) ? metric_name.call(self) : metric_name) if truthiness
69 69 end
70 70 end
71 71 end
@@ -74,7 +74,7 @@ def statsd_count_if(method, name)
74 74 def statsd_count(method, name)
75 75 add_to_method(method, name, :count) do |old_method, new_method, metric_name|
76 76 define_method(new_method) do |*args, &block|
77   - StatsD.increment(metric_name)
  77 + StatsD.increment(metric_name.respond_to?(:call) ? metric_name.call(self) : metric_name)
78 78 send(old_method, *args, &block)
79 79 end
80 80 end
10 test/statsd-instrument_test.rb
@@ -53,6 +53,9 @@ def purchase(arg)
53 53 end
54 54 end
55 55
  56 +class GatewaySubClass < ActiveMerchant::Gateway
  57 +end
  58 +
56 59 ActiveMerchant::Base.extend StatsD::Instrument
57 60
58 61 class StatsDTest < Test::Unit::TestCase
@@ -128,6 +131,13 @@ def test_statsd_count
128 131 ActiveMerchant::Gateway.new.purchase(true)
129 132 end
130 133
  134 + def test_statsd_count_with_name_as_lambda
  135 + ActiveMerchant::Gateway.statsd_count(:ssl_post, lambda {|object| object.class.to_s.downcase + ".insert"})
  136 +
  137 + StatsD.expects(:increment).with('gatewaysubclass.insert')
  138 + GatewaySubClass.new.purchase(true)
  139 + end
  140 +
131 141 def test_statsd_count_with_method_receiving_block
132 142 ActiveMerchant::Base.statsd_count :post_with_block, 'ActiveMerchant.Base.post_with_block'
133 143

0 comments on commit cb82485

Please sign in to comment.
Something went wrong with that request. Please try again.