statsd-instrument 1.3.3 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -27,15 +27,15 @@ module StatsD
27
27
  end
28
28
 
29
29
  module Instrument
30
- def statsd_measure(method, name)
30
+ def statsd_measure(method, name, sample_rate = StatsD.default_sample_rate)
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, nil, sample_rate) { send(old_method, *args, &block) }
34
34
  end
35
35
  end
36
36
  end
37
37
 
38
- def statsd_count_success(method, name)
38
+ def statsd_count_success(method, name, sample_rate = StatsD.default_sample_rate)
39
39
  add_to_method(method, name, :count_success) do |old_method, new_method, metric_name|
40
40
  define_method(new_method) do |*args, &block|
41
41
  begin
@@ -47,13 +47,13 @@ module StatsD
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}." + (truthiness == false ? 'failure' : 'success'), sample_rate)
51
51
  end
52
52
  end
53
53
  end
54
54
  end
55
55
 
56
- def statsd_count_if(method, name)
56
+ def statsd_count_if(method, name, sample_rate = StatsD.default_sample_rate)
57
57
  add_to_method(method, name, :count_if) do |old_method, new_method, metric_name|
58
58
  define_method(new_method) do |*args, &block|
59
59
  begin
@@ -65,16 +65,16 @@ module StatsD
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, sample_rate) if truthiness
69
69
  end
70
70
  end
71
71
  end
72
72
  end
73
73
 
74
- def statsd_count(method, name)
74
+ def statsd_count(method, name, sample_rate = StatsD.default_sample_rate)
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, sample_rate)
78
78
  send(old_method, *args, &block)
79
79
  end
80
80
  end
@@ -131,6 +131,8 @@ module StatsD
131
131
  return unless enabled
132
132
  return if sample_rate < 1 && rand > sample_rate
133
133
 
134
+ k = k.gsub('::', '.')
135
+
134
136
  command = "#{self.prefix + '.' if self.prefix}#{k}:#{v}"
135
137
  case op
136
138
  when :incr
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "statsd-instrument"
3
- s.version = '1.3.3'
3
+ s.version = '1.4.0'
4
4
  s.authors = ["Jesse Storimer", "Tobias Lutke"]
5
5
  s.email = ["jesse@shopify.com"]
6
6
  s.homepage = "http://github.com/shopify/statsd-instrument"
@@ -64,7 +64,7 @@ class StatsDTest < Test::Unit::TestCase
64
64
  def test_statsd_count_if
65
65
  ActiveMerchant::Gateway.statsd_count_if :ssl_post, 'ActiveMerchant.Gateway.if'
66
66
 
67
- StatsD.expects(:increment).with(includes('if')).once
67
+ StatsD.expects(:increment).with(includes('if'), 1).once
68
68
  ActiveMerchant::Gateway.new.purchase(true)
69
69
  ActiveMerchant::Gateway.new.purchase(false)
70
70
  end
@@ -84,18 +84,18 @@ class StatsDTest < Test::Unit::TestCase
84
84
  result[:success]
85
85
  end
86
86
 
87
- StatsD.expects(:increment).with(includes('block')).once
87
+ StatsD.expects(:increment).with(includes('block'), 1).once
88
88
  ActiveMerchant::UniqueGateway.new.purchase(true)
89
89
  ActiveMerchant::UniqueGateway.new.purchase(false)
90
90
  end
91
91
 
92
92
  def test_statsd_count_success
93
- ActiveMerchant::Gateway.statsd_count_success :ssl_post, 'ActiveMerchant.Gateway'
93
+ ActiveMerchant::Gateway.statsd_count_success :ssl_post, 'ActiveMerchant.Gateway', 0.5
94
94
 
95
- StatsD.expects(:increment).with(includes('success'))
95
+ StatsD.expects(:increment).with(includes('success'), 0.5)
96
96
  ActiveMerchant::Gateway.new.purchase(true)
97
97
 
98
- StatsD.expects(:increment).with(includes('failure'))
98
+ StatsD.expects(:increment).with(includes('failure'), 0.5)
99
99
  ActiveMerchant::Gateway.new.purchase(false)
100
100
  end
101
101
 
@@ -114,17 +114,17 @@ class StatsDTest < Test::Unit::TestCase
114
114
  result[:success]
115
115
  end
116
116
 
117
- StatsD.expects(:increment).with(includes('success'))
117
+ StatsD.expects(:increment).with(includes('success'), StatsD.default_sample_rate)
118
118
  ActiveMerchant::UniqueGateway.new.purchase(true)
119
119
 
120
- StatsD.expects(:increment).with(includes('failure'))
120
+ StatsD.expects(:increment).with(includes('failure'), StatsD.default_sample_rate)
121
121
  ActiveMerchant::UniqueGateway.new.purchase(false)
122
122
  end
123
123
 
124
124
  def test_statsd_count
125
125
  ActiveMerchant::Gateway.statsd_count :ssl_post, 'ActiveMerchant.Gateway.ssl_post'
126
126
 
127
- StatsD.expects(:increment).with(includes('ssl_post'))
127
+ StatsD.expects(:increment).with(includes('ssl_post'), 1)
128
128
  ActiveMerchant::Gateway.new.purchase(true)
129
129
  end
130
130
 
@@ -136,13 +136,23 @@ class StatsDTest < Test::Unit::TestCase
136
136
  assert_equal 'block called', return_value
137
137
  end
138
138
 
139
+ def test_statsd_measure_with_nested_modules
140
+ ActiveMerchant::UniqueGateway.statsd_measure :ssl_post, 'ActiveMerchant::Gateway.ssl_post'
141
+
142
+ StatsD.stubs(:mode).returns(:production)
143
+ UDPSocket.any_instance.expects(:send).with(regexp_matches(/ActiveMerchant\.Gateway\.ssl_post:\d\.\d{2,}\|ms/), 0, 'localhost', 123).at_least(1)
144
+
145
+ ActiveMerchant::UniqueGateway.new.purchase(true)
146
+ end
147
+
139
148
  def test_statsd_measure
140
- ActiveMerchant::UniqueGateway.statsd_measure :ssl_post, 'ActiveMerchant.Gateway.ssl_post'
149
+ ActiveMerchant::UniqueGateway.statsd_measure :ssl_post, 'ActiveMerchant.Gateway.ssl_post', 0.3
141
150
 
142
- StatsD.expects(:write).with('ActiveMerchant.Gateway.ssl_post', is_a(Float), :ms, is_a(Numeric)).returns({:success => true})
151
+ StatsD.expects(:write).with('ActiveMerchant.Gateway.ssl_post', is_a(Float), :ms, 0.3).returns({:success => true})
143
152
  ActiveMerchant::UniqueGateway.new.purchase(true)
144
153
  end
145
154
 
155
+
146
156
  def test_statsd_measure_with_method_receiving_block
147
157
  ActiveMerchant::Base.statsd_measure :post_with_block, 'ActiveMerchant.Base.post_with_block'
148
158
 
@@ -155,21 +165,21 @@ class StatsDTest < Test::Unit::TestCase
155
165
  ActiveMerchant::Gateway.singleton_class.extend StatsD::Instrument
156
166
  ActiveMerchant::Gateway.singleton_class.statsd_count :sync, 'ActiveMerchant.Gateway.sync'
157
167
 
158
- StatsD.expects(:increment).with(includes('sync'))
168
+ StatsD.expects(:increment).with(includes('sync'), 1)
159
169
  ActiveMerchant::Gateway.sync
160
170
  end
161
171
 
162
172
  def test_count_with_sampling
163
173
  StatsD.unstub(:increment)
164
- StatsD.stubs(:rand).returns(0.1)
174
+ StatsD.stubs(:rand).returns(0.6)
165
175
  StatsD.logger.expects(:info).never
166
176
 
167
- StatsD.increment('sampling.foo.bar', 1, 0.6)
177
+ StatsD.increment('sampling.foo.bar', 1, 0.1)
168
178
  end
169
179
 
170
180
  def test_count_with_successful_sample
171
181
  StatsD.unstub(:increment)
172
- StatsD.stubs(:rand).returns(0.2)
182
+ StatsD.stubs(:rand).returns(0.01)
173
183
  StatsD.logger.expects(:info).once.with do |string|
174
184
  string.include?('@0.1')
175
185
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: statsd-instrument
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.3
4
+ version: 1.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-09-19 00:00:00.000000000 Z
13
+ date: 2012-10-12 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: mocha