Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #21 from camilo/sampling_rate_for_meta_programming

Sampling rate for meta-progamming instumentation
  • Loading branch information...
commit 8d76578854520ccf46e3f53c6862594a88132ac4 2 parents 03adcbe + ef91399
@jstorimer jstorimer authored
Showing with 31 additions and 19 deletions.
  1. +10 −8 lib/statsd/instrument.rb
  2. +21 −11 test/statsd-instrument_test.rb
View
18 lib/statsd/instrument.rb
@@ -27,15 +27,15 @@ def self.server=(conn)
end
module Instrument
- def statsd_measure(method, name)
+ 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) { send(old_method, *args, &block) }
+ StatsD.measure(metric_name, nil, sample_rate) { send(old_method, *args, &block) }
end
end
end
- def statsd_count_success(method, name)
+ def statsd_count_success(method, name, sample_rate = StatsD.default_sample_rate)
add_to_method(method, name, :count_success) do |old_method, new_method, metric_name|
define_method(new_method) do |*args, &block|
begin
@@ -47,13 +47,13 @@ def statsd_count_success(method, name)
truthiness = (yield(result) rescue false) if block_given?
result
ensure
- StatsD.increment("#{metric_name}." + (truthiness == false ? 'failure' : 'success'))
+ StatsD.increment("#{metric_name}." + (truthiness == false ? 'failure' : 'success'), sample_rate)
end
end
end
end
- def statsd_count_if(method, name)
+ def statsd_count_if(method, name, sample_rate = StatsD.default_sample_rate)
add_to_method(method, name, :count_if) do |old_method, new_method, metric_name|
define_method(new_method) do |*args, &block|
begin
@@ -65,16 +65,16 @@ def statsd_count_if(method, name)
truthiness = (yield(result) rescue false) if block_given?
result
ensure
- StatsD.increment(metric_name) if truthiness
+ StatsD.increment(metric_name, sample_rate) if truthiness
end
end
end
end
- def statsd_count(method, name)
+ 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)
+ StatsD.increment(metric_name, sample_rate)
send(old_method, *args, &block)
end
end
@@ -131,6 +131,8 @@ def self.write(k,v,op, sample_rate = default_sample_rate)
return unless enabled
return if sample_rate < 1 && rand > sample_rate
+ k = k.gsub('::', '.')
+
command = "#{self.prefix + '.' if self.prefix}#{k}:#{v}"
case op
when :incr
View
32 test/statsd-instrument_test.rb
@@ -64,7 +64,7 @@ def setup
def test_statsd_count_if
ActiveMerchant::Gateway.statsd_count_if :ssl_post, 'ActiveMerchant.Gateway.if'
- StatsD.expects(:increment).with(includes('if')).once
+ StatsD.expects(:increment).with(includes('if'), 1).once
ActiveMerchant::Gateway.new.purchase(true)
ActiveMerchant::Gateway.new.purchase(false)
end
@@ -84,18 +84,18 @@ def test_statsd_count_if_with_block
result[:success]
end
- StatsD.expects(:increment).with(includes('block')).once
+ StatsD.expects(:increment).with(includes('block'), 1).once
ActiveMerchant::UniqueGateway.new.purchase(true)
ActiveMerchant::UniqueGateway.new.purchase(false)
end
def test_statsd_count_success
- ActiveMerchant::Gateway.statsd_count_success :ssl_post, 'ActiveMerchant.Gateway'
+ ActiveMerchant::Gateway.statsd_count_success :ssl_post, 'ActiveMerchant.Gateway', 0.5
- StatsD.expects(:increment).with(includes('success'))
+ StatsD.expects(:increment).with(includes('success'), 0.5)
ActiveMerchant::Gateway.new.purchase(true)
- StatsD.expects(:increment).with(includes('failure'))
+ StatsD.expects(:increment).with(includes('failure'), 0.5)
ActiveMerchant::Gateway.new.purchase(false)
end
@@ -114,17 +114,17 @@ def test_statsd_count_success_with_block
result[:success]
end
- StatsD.expects(:increment).with(includes('success'))
+ StatsD.expects(:increment).with(includes('success'), StatsD.default_sample_rate)
ActiveMerchant::UniqueGateway.new.purchase(true)
- StatsD.expects(:increment).with(includes('failure'))
+ StatsD.expects(:increment).with(includes('failure'), StatsD.default_sample_rate)
ActiveMerchant::UniqueGateway.new.purchase(false)
end
def test_statsd_count
ActiveMerchant::Gateway.statsd_count :ssl_post, 'ActiveMerchant.Gateway.ssl_post'
- StatsD.expects(:increment).with(includes('ssl_post'))
+ StatsD.expects(:increment).with(includes('ssl_post'), 1)
ActiveMerchant::Gateway.new.purchase(true)
end
@@ -136,13 +136,23 @@ def test_statsd_count_with_method_receiving_block
assert_equal 'block called', return_value
end
+ def test_statsd_measure_with_nested_modules
+ ActiveMerchant::UniqueGateway.statsd_measure :ssl_post, 'ActiveMerchant::Gateway.ssl_post'
+
+ StatsD.stubs(:mode).returns(:production)
+ UDPSocket.any_instance.expects(:send).with(regexp_matches(/ActiveMerchant\.Gateway\.ssl_post:\d\.\d{2,}\|ms/), 0, 'localhost', 123).at_least(1)
+
+ ActiveMerchant::UniqueGateway.new.purchase(true)
+ end
+
def test_statsd_measure
- ActiveMerchant::UniqueGateway.statsd_measure :ssl_post, 'ActiveMerchant.Gateway.ssl_post'
+ ActiveMerchant::UniqueGateway.statsd_measure :ssl_post, 'ActiveMerchant.Gateway.ssl_post', 0.3
- StatsD.expects(:write).with('ActiveMerchant.Gateway.ssl_post', is_a(Float), :ms, is_a(Numeric)).returns({:success => true})
+ StatsD.expects(:write).with('ActiveMerchant.Gateway.ssl_post', is_a(Float), :ms, 0.3).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'
@@ -155,7 +165,7 @@ def test_instrumenting_class_method
ActiveMerchant::Gateway.singleton_class.extend StatsD::Instrument
ActiveMerchant::Gateway.singleton_class.statsd_count :sync, 'ActiveMerchant.Gateway.sync'
- StatsD.expects(:increment).with(includes('sync'))
+ StatsD.expects(:increment).with(includes('sync'), 1)
ActiveMerchant::Gateway.sync
end
Please sign in to comment.
Something went wrong with that request. Please try again.