Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metric name lambdas #19

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -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.

Expand Down
8 changes: 4 additions & 4 deletions lib/statsd/instrument.rb
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions test/statsd-instrument_test.rb
Expand Up @@ -53,6 +53,9 @@ def purchase(arg)
end
end

class GatewaySubClass < ActiveMerchant::Gateway
end

ActiveMerchant::Base.extend StatsD::Instrument

class StatsDTest < Test::Unit::TestCase
Expand Down Expand Up @@ -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', StatsD.default_sample_rate)
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'

Expand Down