Permalink
Browse files

Merging metric-name-lambdas into master

  • Loading branch information...
2 parents aca09cc + 125526a commit 075af8596ae46b151009c79ea9cd15cab0c8cc2c @freeatnet freeatnet committed Jan 21, 2013
Showing with 22 additions and 4 deletions.
  1. +8 −0 README.md
  2. +4 −4 lib/statsd/instrument.rb
  3. +10 −0 test/statsd-instrument_test.rb
View
8 README.md
@@ -136,6 +136,14 @@ AWS::S3::Base.singleton_class.extend StatsD::Instrument
AWS::S3::Base.singleton_class.statsd_measure :request, 'S3.request'
```
+### Lambda As Metric Name
+
+Send a lambda instead of a string as the metric name to dynamically set the name of the metric. Useful when measuring sub classes.
+
+``` ruby
+GoogleBase.statsd_count :insert, lamdba{|object| object.class.to_s.downcase + ".insert" }
+```
+
## Reliance on DNS
Out of the box StatsD is set up to be unidirectional fire-and-forget over UDP. Configuring the StatsD host to be a non-ip will trigger a DNS lookup (ie synchronous round trip network call) for each metric sent. This can be particularly problematic in clouds that have a shared DNS infrastructure such as AWS.
View
8 lib/statsd/instrument.rb
@@ -30,7 +30,7 @@ module Instrument
def statsd_measure(method, name, sample_rate = StatsD.default_sample_rate)
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, nil, sample_rate) { send(old_method, *args, &block) }
+ StatsD.measure(metric_name.respond_to?(:call) ? metric_name.call(self) : metric_name, nil, sample_rate) { send(old_method, *args, &block) }
end
end
end
@@ -47,7 +47,7 @@ def statsd_count_success(method, name, sample_rate = StatsD.default_sample_rate)
truthiness = (yield(result) rescue false) if block_given?
result
ensure
- StatsD.increment("#{metric_name}." + (truthiness == false ? 'failure' : 'success'), sample_rate)
+ StatsD.increment("#{metric_name.respond_to?(:call) ? metric_name.call(self) : metric_name}." + (truthiness == false ? 'failure' : 'success'), sample_rate)
end
end
end
@@ -65,7 +65,7 @@ def statsd_count_if(method, name, sample_rate = StatsD.default_sample_rate)
truthiness = (yield(result) rescue false) if block_given?
result
ensure
- StatsD.increment(metric_name, sample_rate) if truthiness
+ StatsD.increment(metric_name.respond_to?(:call) ? metric_name.call(self) : metric_name, sample_rate) if truthiness
end
end
end
@@ -74,7 +74,7 @@ def statsd_count_if(method, name, sample_rate = StatsD.default_sample_rate)
def statsd_count(method, name, sample_rate = StatsD.default_sample_rate)
add_to_method(method, name, :count) do |old_method, new_method, metric_name|
define_method(new_method) do |*args, &block|
- StatsD.increment(metric_name, sample_rate)
+ StatsD.increment(metric_name.respond_to?(:call) ? metric_name.call(self) : metric_name, sample_rate)
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 075af85

Please sign in to comment.