statsd-instrument 2.0.10 → 2.0.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -4
- data/lib/statsd/instrument.rb +22 -0
- data/lib/statsd/instrument/assertions.rb +2 -2
- data/lib/statsd/instrument/matchers.rb +7 -4
- data/lib/statsd/instrument/version.rb +1 -1
- data/test/matchers_test.rb +10 -0
- data/test/statsd_instrumentation_test.rb +84 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2b3d5d77001c8868328c05e1f62406ab13b92c03
|
4
|
+
data.tar.gz: 32b5ba8a24354acfb72f73ce2e514146e9e3773c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a94d925a45d26e5b5ebd366cc8aa2d2e61e06a577b79e5f4af908d1114598dad047cd24b0206d7864964be93f6a7e571f6f2d601ed68c26458433151cde8f540
|
7
|
+
data.tar.gz: d1bab44e9942aa4a7e1be4d1af6f88bcf33bf82106d7f8c3c5cfcb957579be583942693f30662f837a26e37f9a7adbadcebeea7a3c980ac06ae14e6931aa9bbc
|
data/README.md
CHANGED
@@ -87,7 +87,7 @@ StatsD.increment('GoogleBase.insert', 1, sample_rate: 0.1)
|
|
87
87
|
|
88
88
|
#### StatsD.gauge
|
89
89
|
|
90
|
-
A gauge is a single numerical value
|
90
|
+
A gauge is a single numerical value that tells you the state of the system at a point in time. A good example would be the number of messages in a queue.
|
91
91
|
|
92
92
|
``` ruby
|
93
93
|
StatsD.gauge('GoogleBase.queued', 12, sample_rate: 1.0)
|
@@ -262,6 +262,10 @@ end
|
|
262
262
|
### RSpec
|
263
263
|
|
264
264
|
```ruby
|
265
|
+
RSpec.configure do |config|
|
266
|
+
config.include StatsD::Instrument::Matchers
|
267
|
+
end
|
268
|
+
|
265
269
|
RSpec.describe 'Matchers' do
|
266
270
|
context 'trigger_statsd_increment' do
|
267
271
|
it 'will pass if there is exactly one matching StatsD call' do
|
@@ -269,9 +273,9 @@ RSpec.describe 'Matchers' do
|
|
269
273
|
end
|
270
274
|
|
271
275
|
it 'will pass if it matches the correct number of times' do
|
272
|
-
expect {
|
276
|
+
expect {
|
273
277
|
2.times do
|
274
|
-
StatsD.increment('counter')
|
278
|
+
StatsD.increment('counter')
|
275
279
|
end
|
276
280
|
}.to trigger_statsd_increment('counter', times: 2)
|
277
281
|
end
|
@@ -281,7 +285,6 @@ RSpec.describe 'Matchers' do
|
|
281
285
|
end
|
282
286
|
end
|
283
287
|
end
|
284
|
-
|
285
288
|
```
|
286
289
|
|
287
290
|
## Notes
|
data/lib/statsd/instrument.rb
CHANGED
@@ -210,17 +210,39 @@ module StatsD
|
|
210
210
|
raise ArgumentError, "already instrumented #{method} for #{self.name}" if method_defined? method_name_without_statsd
|
211
211
|
raise ArgumentError, "could not find method #{method} for #{self.name}" unless method_defined?(method) || private_method_defined?(method)
|
212
212
|
|
213
|
+
method_scope = method_visibility(method)
|
214
|
+
|
213
215
|
alias_method method_name_without_statsd, method
|
214
216
|
yield method_name_without_statsd, method_name_with_statsd, metric_name
|
215
217
|
alias_method method, method_name_with_statsd
|
218
|
+
|
219
|
+
private(method_name_without_statsd)
|
220
|
+
send(method_scope, method)
|
221
|
+
private(method_name_with_statsd)
|
216
222
|
end
|
217
223
|
|
218
224
|
def remove_from_method(method, name, action)
|
219
225
|
method_name_without_statsd = :"#{method}_for_#{action}_on_#{self.name}_without_#{name}"
|
220
226
|
method_name_with_statsd = :"#{method}_for_#{action}_on_#{self.name}_with_#{name}"
|
227
|
+
|
228
|
+
method_scope = method_visibility(method)
|
229
|
+
|
221
230
|
send(:remove_method, method_name_with_statsd)
|
222
231
|
alias_method method, method_name_without_statsd
|
223
232
|
send(:remove_method, method_name_without_statsd)
|
233
|
+
|
234
|
+
send(method_scope, method)
|
235
|
+
end
|
236
|
+
|
237
|
+
def method_visibility(method)
|
238
|
+
case
|
239
|
+
when private_method_defined?(method)
|
240
|
+
:private
|
241
|
+
when protected_method_defined?(method)
|
242
|
+
:protected
|
243
|
+
else
|
244
|
+
:public
|
245
|
+
end
|
224
246
|
end
|
225
247
|
end
|
226
248
|
|
@@ -41,8 +41,8 @@ module StatsD::Instrument::Assertions
|
|
41
41
|
assert options[:times] === metrics.length, "The amount of StatsD calls for metric #{metric_name} was unexpected. Expected #{options[:times].inspect}, found #{metrics.length}"
|
42
42
|
metric = metrics.first
|
43
43
|
|
44
|
-
assert_equal options[:sample_rate], metric.sample_rate, "Unexpected
|
45
|
-
assert_equal options[:value], metric.value, "Unexpected
|
44
|
+
assert_equal options[:sample_rate], metric.sample_rate, "Unexpected StatsD sample rate for metric #{metric_name}" if options[:sample_rate]
|
45
|
+
assert_equal options[:value], metric.value, "Unexpected value submitted for StatsD metric #{metric_name}" if options[:value]
|
46
46
|
|
47
47
|
if options[:tags]
|
48
48
|
expected_tags = Set.new(StatsD::Instrument::Metric.normalize_tags(options[:tags]))
|
@@ -11,6 +11,7 @@ module StatsD::Instrument::Matchers
|
|
11
11
|
}
|
12
12
|
|
13
13
|
class Matcher
|
14
|
+
include RSpec::Matchers::Composable
|
14
15
|
include StatsD::Instrument::Helpers
|
15
16
|
|
16
17
|
def initialize(metric_type, metric_name, options = {})
|
@@ -50,11 +51,13 @@ module StatsD::Instrument::Matchers
|
|
50
51
|
raise RSpec::Expectations::ExpectationNotMetError, "No StatsD calls for metric #{metric_name} were made." if metrics.empty?
|
51
52
|
raise RSpec::Expectations::ExpectationNotMetError, "The numbers of StatsD calls for metric #{metric_name} was unexpected. Expected #{options[:times].inspect}, got #{metrics.length}" if options[:times] && options[:times] != metrics.length
|
52
53
|
|
53
|
-
|
54
|
+
[:sample_rate, :value, :tags].each do |expectation|
|
55
|
+
next unless options[expectation]
|
54
56
|
|
55
|
-
|
56
|
-
|
57
|
-
|
57
|
+
if metrics.all? { |m| m.public_send(expectation) != options[expectation] }
|
58
|
+
raise RSpec::Expectations::ExpectationNotMetError, "Unexpected StatsD #{expectation.to_s.gsub('_', ' ')} for metric #{metric_name}"
|
59
|
+
end
|
60
|
+
end
|
58
61
|
|
59
62
|
true
|
60
63
|
end
|
data/test/matchers_test.rb
CHANGED
@@ -10,6 +10,16 @@ class MatchersTest < Minitest::Test
|
|
10
10
|
refute StatsD::Instrument::Matchers::Increment.new(:c, 'counter', {}).matches? lambda { StatsD.increment('not_counter') }
|
11
11
|
end
|
12
12
|
|
13
|
+
def test_statsd_increment_compound
|
14
|
+
matcher_1 = StatsD::Instrument::Matchers::Increment.new(:c, 'counter', tags: ['a'])
|
15
|
+
matcher_2 = StatsD::Instrument::Matchers::Increment.new(:c, 'counter', tags: ['b'])
|
16
|
+
|
17
|
+
assert RSpec::Matchers::BuiltIn::Compound::And.new(matcher_1, matcher_2).matches? lambda {
|
18
|
+
StatsD.increment('counter', tags: ['a'])
|
19
|
+
StatsD.increment('counter', tags: ['b'])
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
13
23
|
def test_statsd_increment_with_times_matched
|
14
24
|
assert StatsD::Instrument::Matchers::Increment.new(:c, 'counter', times: 1).matches? lambda { StatsD.increment('counter') }
|
15
25
|
end
|
@@ -45,6 +45,26 @@ end
|
|
45
45
|
class GatewaySubClass < ActiveMerchant::Gateway
|
46
46
|
end
|
47
47
|
|
48
|
+
class InstrumentedClass
|
49
|
+
extend StatsD::Instrument
|
50
|
+
|
51
|
+
def public_and_instrumented
|
52
|
+
end
|
53
|
+
statsd_count :public_and_instrumented, 'InstrumentedClass.public_and_instrumented'
|
54
|
+
|
55
|
+
protected
|
56
|
+
|
57
|
+
def protected_and_instrumented
|
58
|
+
end
|
59
|
+
statsd_count :protected_and_instrumented, 'InstrumentedClass.protected_and_instrumented'
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def private_and_instrumented
|
64
|
+
end
|
65
|
+
statsd_count :private_and_instrumented, 'InstrumentedClass.private_and_instrumented'
|
66
|
+
end
|
67
|
+
|
48
68
|
ActiveMerchant::Base.extend StatsD::Instrument
|
49
69
|
|
50
70
|
class StatsDInstrumentationTest < Minitest::Test
|
@@ -116,7 +136,7 @@ class StatsDInstrumentationTest < Minitest::Test
|
|
116
136
|
assert_statsd_increment('ActiveMerchant.Base.post_with_block.failure', times: 1) do
|
117
137
|
assert_equal 'successful', ActiveMerchant::Base.new.post_with_block { 'successful' }
|
118
138
|
assert_equal 'not so successful', ActiveMerchant::Base.new.post_with_block { 'not so successful' }
|
119
|
-
end
|
139
|
+
end
|
120
140
|
|
121
141
|
ActiveMerchant::Base.statsd_remove_count_success :post_with_block, 'ActiveMerchant.Base.post_with_block'
|
122
142
|
end
|
@@ -219,4 +239,67 @@ class StatsDInstrumentationTest < Minitest::Test
|
|
219
239
|
|
220
240
|
ActiveMerchant::Gateway.singleton_class.statsd_remove_count :sync, 'ActiveMerchant.Gateway.sync'
|
221
241
|
end
|
242
|
+
|
243
|
+
def test_statsd_doesnt_change_method_scope_of_public_method
|
244
|
+
assert_scope InstrumentedClass, :public_and_instrumented, :public
|
245
|
+
|
246
|
+
assert_statsd_increment('InstrumentedClass.public_and_instrumented') do
|
247
|
+
InstrumentedClass.new.send(:public_and_instrumented)
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
def test_statsd_doesnt_change_method_scope_of_protected_method
|
252
|
+
assert_scope InstrumentedClass, :protected_and_instrumented, :protected
|
253
|
+
|
254
|
+
assert_statsd_increment('InstrumentedClass.protected_and_instrumented') do
|
255
|
+
InstrumentedClass.new.send(:protected_and_instrumented)
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
def test_statsd_doesnt_change_method_scope_of_private_method
|
260
|
+
assert_scope InstrumentedClass, :private_and_instrumented, :private
|
261
|
+
|
262
|
+
assert_statsd_increment('InstrumentedClass.private_and_instrumented') do
|
263
|
+
InstrumentedClass.new.send(:private_and_instrumented)
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
def test_statsd_doesnt_change_method_scope_on_removal_of_public_method
|
268
|
+
assert_scope InstrumentedClass, :public_and_instrumented, :public
|
269
|
+
InstrumentedClass.statsd_remove_count :public_and_instrumented, 'InstrumentedClass.public_and_instrumented'
|
270
|
+
assert_scope InstrumentedClass, :public_and_instrumented, :public
|
271
|
+
|
272
|
+
InstrumentedClass.statsd_count :public_and_instrumented, 'InstrumentedClass.public_and_instrumented'
|
273
|
+
end
|
274
|
+
|
275
|
+
def test_statsd_doesnt_change_method_scope_on_removal_of_protected_method
|
276
|
+
assert_scope InstrumentedClass, :protected_and_instrumented, :protected
|
277
|
+
InstrumentedClass.statsd_remove_count :protected_and_instrumented, 'InstrumentedClass.protected_and_instrumented'
|
278
|
+
assert_scope InstrumentedClass, :protected_and_instrumented, :protected
|
279
|
+
|
280
|
+
InstrumentedClass.statsd_count :protected_and_instrumented, 'InstrumentedClass.protected_and_instrumented'
|
281
|
+
end
|
282
|
+
|
283
|
+
def test_statsd_doesnt_change_method_scope_on_removal_of_private_method
|
284
|
+
assert_scope InstrumentedClass, :private_and_instrumented, :private
|
285
|
+
InstrumentedClass.statsd_remove_count :private_and_instrumented, 'InstrumentedClass.private_and_instrumented'
|
286
|
+
assert_scope InstrumentedClass, :private_and_instrumented, :private
|
287
|
+
|
288
|
+
InstrumentedClass.statsd_count :private_and_instrumented, 'InstrumentedClass.private_and_instrumented'
|
289
|
+
end
|
290
|
+
|
291
|
+
private
|
292
|
+
|
293
|
+
def assert_scope(klass, method, expected_scope)
|
294
|
+
method_scope = case
|
295
|
+
when klass.private_method_defined?(method)
|
296
|
+
:private
|
297
|
+
when klass.protected_method_defined?(method)
|
298
|
+
:protected
|
299
|
+
else
|
300
|
+
:public
|
301
|
+
end
|
302
|
+
|
303
|
+
assert_equal method_scope, expected_scope, "Expected method to be #{expected_scope}"
|
304
|
+
end
|
222
305
|
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: 2.0.
|
4
|
+
version: 2.0.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jesse Storimer
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2016-02-18 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rake
|