Permalink
Browse files

Merge pull request #25 from freeatnet/metric-name-lambdas

Metric name lambdas
  • Loading branch information...
2 parents aca09cc + 172643e commit 264c69f382cdb513abb7d4b855e5793f10e05043 @jstorimer jstorimer committed Jan 25, 2013
Showing with 25 additions and 4 deletions.
  1. +11 −0 README.md
  2. +4 −4 lib/statsd/instrument.rb
  3. +10 −0 test/statsd-instrument_test.rb
View
@@ -136,6 +136,17 @@ AWS::S3::Base.singleton_class.extend StatsD::Instrument
AWS::S3::Base.singleton_class.statsd_measure :request, 'S3.request'
```
+### Dynamic Metric Names
+
+You can use a lambda function instead of a string dynamically set
+the name of the metric. The lambda function must accept two arguments:
+the object the function is being called on and the array of arguments
+passed.
+
+```ruby
+GoogleBase.statsd_count :insert, lamdba{|object, args| object.class.to_s.downcase + "." + args.first.to_s + ".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
@@ -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, args) : 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, args) : 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, args) : 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, args) : metric_name, sample_rate)
send(old_method, *args, &block)
end
end
@@ -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, args| object.class.to_s.downcase + ".insert." + args.first.to_s})
+
+ StatsD.expects(:increment).with('gatewaysubclass.insert.true', 1)
+ 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 264c69f

Please sign in to comment.