statsd-instrument 2.0.7 → 2.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +28 -1
- data/lib/statsd/instrument.rb +3 -1
- data/lib/statsd/instrument/assertions.rb +1 -13
- data/lib/statsd/instrument/helpers.rb +14 -0
- data/lib/statsd/instrument/matchers.rb +72 -0
- data/lib/statsd/instrument/metric.rb +2 -2
- data/lib/statsd/instrument/version.rb +1 -1
- data/statsd-instrument.gemspec +1 -0
- data/test/assertions_test.rb +2 -17
- data/test/helpers_test.rb +24 -0
- data/test/matchers_test.rb +44 -0
- data/test/metric_test.rb +1 -1
- metadata +22 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 289395b86a2c3614c55f80acb4e90c5bd0678fa9
|
4
|
+
data.tar.gz: f6025d65f1ff0c564cfb83686baf31f0d06246e8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: becbd08e53ff69ac276c02add7b622da50a48ebf5395cc1fc7170049e39582806ee19f49e7a81cc7cc75a62e5620b5ece73e8a934771d952575a37cf5bdc6783
|
7
|
+
data.tar.gz: a6cbbc09993f6f88f4e48559386052dfdce381fd3532cacdcec1f13293efdb77318a6482a5adc0bc9fe369650086a5dde52dd3aee3674f7ffeb2a7fbb1892c4a
|
data/README.md
CHANGED
@@ -203,9 +203,11 @@ warning is logged to `StatsD.logger`.
|
|
203
203
|
|
204
204
|
## Testing
|
205
205
|
|
206
|
-
This library comes with a module called `StatsD::Instrument::Assertions` to help you write tests
|
206
|
+
This library comes with a module called `StatsD::Instrument::Assertions` and `StatsD::Instrument::Matchers` to help you write tests
|
207
207
|
to verify StatsD is called properly.
|
208
208
|
|
209
|
+
### minitest
|
210
|
+
|
209
211
|
``` ruby
|
210
212
|
class MyTestcase < Minitest::Test
|
211
213
|
include StatsD::Instrument::Assertions
|
@@ -257,6 +259,31 @@ end
|
|
257
259
|
|
258
260
|
```
|
259
261
|
|
262
|
+
### RSpec
|
263
|
+
|
264
|
+
```ruby
|
265
|
+
RSpec.describe 'Matchers' do
|
266
|
+
context 'trigger_statsd_increment' do
|
267
|
+
it 'will pass if there is exactly one matching StatsD call' do
|
268
|
+
expect { StatsD.increment('counter') }.to trigger_statsd_increment('counter')
|
269
|
+
end
|
270
|
+
|
271
|
+
it 'will pass if it matches the correct number of times' do
|
272
|
+
expect {
|
273
|
+
2.times do
|
274
|
+
StatsD.increment('counter')
|
275
|
+
end
|
276
|
+
}.to trigger_statsd_increment('counter', times: 2)
|
277
|
+
end
|
278
|
+
|
279
|
+
it 'will pass if there is no matching StatsD call on negative expectation' do
|
280
|
+
expect { StatsD.increment('other_counter') }.not_to trigger_statsd_increment('counter')
|
281
|
+
end
|
282
|
+
end
|
283
|
+
end
|
284
|
+
|
285
|
+
```
|
286
|
+
|
260
287
|
## Notes
|
261
288
|
|
262
289
|
### Compatibility
|
data/lib/statsd/instrument.rb
CHANGED
@@ -354,6 +354,8 @@ end
|
|
354
354
|
require 'statsd/instrument/version'
|
355
355
|
require 'statsd/instrument/metric'
|
356
356
|
require 'statsd/instrument/backend'
|
357
|
-
require 'statsd/instrument/assertions'
|
358
357
|
require 'statsd/instrument/environment'
|
358
|
+
require 'statsd/instrument/helpers'
|
359
|
+
require 'statsd/instrument/assertions'
|
360
|
+
require 'statsd/instrument/matchers' if defined?(::RSpec)
|
359
361
|
require 'statsd/instrument/railtie' if defined?(Rails)
|
@@ -1,17 +1,5 @@
|
|
1
1
|
module StatsD::Instrument::Assertions
|
2
|
-
|
3
|
-
def capture_statsd_calls(&block)
|
4
|
-
mock_backend = StatsD::Instrument::Backends::CaptureBackend.new
|
5
|
-
old_backend, StatsD.backend = StatsD.backend, mock_backend
|
6
|
-
block.call
|
7
|
-
mock_backend.collected_metrics
|
8
|
-
ensure
|
9
|
-
if old_backend.kind_of?(StatsD::Instrument::Backends::CaptureBackend)
|
10
|
-
old_backend.collected_metrics.concat(mock_backend.collected_metrics)
|
11
|
-
end
|
12
|
-
|
13
|
-
StatsD.backend = old_backend
|
14
|
-
end
|
2
|
+
include StatsD::Instrument::Helpers
|
15
3
|
|
16
4
|
def assert_no_statsd_calls(metric_name = nil, &block)
|
17
5
|
metrics = capture_statsd_calls(&block)
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module StatsD::Instrument::Helpers
|
2
|
+
def capture_statsd_calls(&block)
|
3
|
+
mock_backend = StatsD::Instrument::Backends::CaptureBackend.new
|
4
|
+
old_backend, StatsD.backend = StatsD.backend, mock_backend
|
5
|
+
block.call
|
6
|
+
mock_backend.collected_metrics
|
7
|
+
ensure
|
8
|
+
if old_backend.kind_of?(StatsD::Instrument::Backends::CaptureBackend)
|
9
|
+
old_backend.collected_metrics.concat(mock_backend.collected_metrics)
|
10
|
+
end
|
11
|
+
|
12
|
+
StatsD.backend = old_backend
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'rspec/expectations'
|
2
|
+
|
3
|
+
module StatsD::Instrument::Matchers
|
4
|
+
CUSTOM_MATCHERS = {
|
5
|
+
increment: :c,
|
6
|
+
measure: :ms,
|
7
|
+
gauge: :g,
|
8
|
+
histogram: :h,
|
9
|
+
set: :s,
|
10
|
+
key_value: :kv
|
11
|
+
}
|
12
|
+
|
13
|
+
class Matcher
|
14
|
+
include StatsD::Instrument::Helpers
|
15
|
+
|
16
|
+
def initialize(metric_type, metric_name, options = {})
|
17
|
+
@metric_type = metric_type
|
18
|
+
@metric_name = metric_name
|
19
|
+
@options = options
|
20
|
+
end
|
21
|
+
|
22
|
+
def matches?(block)
|
23
|
+
begin
|
24
|
+
expect_statsd_call(@metric_type, @metric_name, @options, &block)
|
25
|
+
rescue RSpec::Expectations::ExpectationNotMetError => e
|
26
|
+
@message = e.message
|
27
|
+
|
28
|
+
false
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def failure_message
|
33
|
+
@message
|
34
|
+
end
|
35
|
+
|
36
|
+
def failure_message_when_negated
|
37
|
+
"No StatsD calls for metric #{@metric_name} expected."
|
38
|
+
end
|
39
|
+
|
40
|
+
def supports_block_expectations?
|
41
|
+
true
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def expect_statsd_call(metric_type, metric_name, options, &block)
|
47
|
+
metrics = capture_statsd_calls(&block)
|
48
|
+
metrics = metrics.select { |m| m.type == metric_type && m.name == metric_name }
|
49
|
+
|
50
|
+
raise RSpec::Expectations::ExpectationNotMetError, "No StatsD calls for metric #{metric_name} were made." if metrics.empty?
|
51
|
+
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
|
+
metric = metrics.first
|
54
|
+
|
55
|
+
raise RSpec::Expectations::ExpectationNotMetError, "Unexpected value submitted for StatsD metric #{metric_name}" if options[:sample_rate] && options[:sample_rate] != metric.sample_rate
|
56
|
+
raise RSpec::Expectations::ExpectationNotMetError, "Unexpected StatsD sample rate for metric #{metric_name}" if options[:value] && options[:value] != metric.value
|
57
|
+
raise RSpec::Expectations::ExpectationNotMetError, "Unexpected StatsD tags for metric #{metric_name}" if options[:tags] && options[:tags] != metric.tags
|
58
|
+
|
59
|
+
true
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
CUSTOM_MATCHERS.each do |method_name, metric_type|
|
64
|
+
klass = Class.new(Matcher)
|
65
|
+
|
66
|
+
define_method "trigger_statsd_#{method_name}" do |metric_name, options = {}|
|
67
|
+
klass.new(metric_type, metric_name, options)
|
68
|
+
end
|
69
|
+
|
70
|
+
StatsD::Instrument::Matchers.const_set(method_name.capitalize, klass)
|
71
|
+
end
|
72
|
+
end
|
@@ -101,8 +101,8 @@ class StatsD::Instrument::Metric
|
|
101
101
|
# @param tags [Array<String>, Hash<String, String>, nil] Tags specified in any form.
|
102
102
|
# @return [Array<String>, nil] the list of tags in canonical form.
|
103
103
|
def self.normalize_tags(tags)
|
104
|
-
return
|
105
|
-
tags = tags.map { |k, v|
|
104
|
+
return unless tags
|
105
|
+
tags = tags.map { |k, v| k.to_s + ":".freeze + v.to_s } if tags.is_a?(Hash)
|
106
106
|
tags.map { |tag| tag.tr('|,'.freeze, ''.freeze) }
|
107
107
|
end
|
108
108
|
end
|
data/statsd-instrument.gemspec
CHANGED
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
|
|
20
20
|
|
21
21
|
spec.add_development_dependency 'rake'
|
22
22
|
spec.add_development_dependency 'minitest'
|
23
|
+
spec.add_development_dependency 'rspec'
|
23
24
|
spec.add_development_dependency 'mocha'
|
24
25
|
spec.add_development_dependency 'yard'
|
25
26
|
spec.add_development_dependency 'benchmark-ips'
|
data/test/assertions_test.rb
CHANGED
@@ -1,27 +1,12 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class AssertionsTest < Minitest::Test
|
4
|
-
|
5
4
|
def setup
|
6
5
|
test_class = Class.new(Minitest::Test)
|
7
6
|
test_class.send(:include, StatsD::Instrument::Assertions)
|
8
7
|
@test_case = test_class.new('fake')
|
9
8
|
end
|
10
9
|
|
11
|
-
def test_capture_metrics_inside_block_only
|
12
|
-
StatsD.increment('counter')
|
13
|
-
metrics = @test_case.capture_statsd_calls do
|
14
|
-
StatsD.increment('counter')
|
15
|
-
StatsD.gauge('gauge', 12)
|
16
|
-
end
|
17
|
-
StatsD.gauge('gauge', 15)
|
18
|
-
|
19
|
-
assert_equal 2, metrics.length
|
20
|
-
assert_equal 'counter', metrics[0].name
|
21
|
-
assert_equal 'gauge', metrics[1].name
|
22
|
-
assert_equal 12, metrics[1].value
|
23
|
-
end
|
24
|
-
|
25
10
|
def test_assert_no_statsd_calls
|
26
11
|
assert_no_assertion_triggered do
|
27
12
|
@test_case.assert_no_statsd_calls('counter') do
|
@@ -45,7 +30,7 @@ class AssertionsTest < Minitest::Test
|
|
45
30
|
@test_case.assert_no_statsd_calls do
|
46
31
|
StatsD.increment('other')
|
47
32
|
end
|
48
|
-
end
|
33
|
+
end
|
49
34
|
end
|
50
35
|
|
51
36
|
def test_assert_statsd_call
|
@@ -132,7 +117,7 @@ class AssertionsTest < Minitest::Test
|
|
132
117
|
StatsD.increment('counter2')
|
133
118
|
end
|
134
119
|
end
|
135
|
-
end
|
120
|
+
end
|
136
121
|
|
137
122
|
assert_assertion_triggered do
|
138
123
|
@test_case.assert_statsd_increment('counter1') do
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class HelpersTest < Minitest::Test
|
4
|
+
def setup
|
5
|
+
test_class = Class.new(Minitest::Test)
|
6
|
+
test_class.send(:include, StatsD::Instrument::Helpers)
|
7
|
+
@test_case = test_class.new('fake')
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_capture_metrics_inside_block_only
|
11
|
+
StatsD.increment('counter')
|
12
|
+
metrics = @test_case.capture_statsd_calls do
|
13
|
+
StatsD.increment('counter')
|
14
|
+
StatsD.gauge('gauge', 12)
|
15
|
+
end
|
16
|
+
StatsD.gauge('gauge', 15)
|
17
|
+
|
18
|
+
assert_equal 2, metrics.length
|
19
|
+
assert_equal 'counter', metrics[0].name
|
20
|
+
assert_equal 'gauge', metrics[1].name
|
21
|
+
assert_equal 12, metrics[1].value
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'statsd/instrument/matchers'
|
3
|
+
|
4
|
+
class MatchersTest < Minitest::Test
|
5
|
+
def test_statsd_increment_matched
|
6
|
+
assert StatsD::Instrument::Matchers::Increment.new(:c, 'counter', {}).matches? lambda { StatsD.increment('counter') }
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_statsd_increment_not_matched
|
10
|
+
refute StatsD::Instrument::Matchers::Increment.new(:c, 'counter', {}).matches? lambda { StatsD.increment('not_counter') }
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_statsd_increment_with_times_matched
|
14
|
+
assert StatsD::Instrument::Matchers::Increment.new(:c, 'counter', times: 1).matches? lambda { StatsD.increment('counter') }
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_statsd_increment_with_times_not_matched
|
18
|
+
refute StatsD::Instrument::Matchers::Increment.new(:c, 'counter', times: 2).matches? lambda { StatsD.increment('counter', times: 3) }
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_statsd_increment_with_sample_rate_matched
|
22
|
+
assert StatsD::Instrument::Matchers::Increment.new(:c, 'counter', sample_rate: 0.5).matches? lambda { StatsD.increment('counter', sample_rate: 0.5) }
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_statsd_increment_with_sample_rate_not_matched
|
26
|
+
refute StatsD::Instrument::Matchers::Increment.new(:c, 'counter', sample_rate: 0.5).matches? lambda { StatsD.increment('counter', sample_rate: 0.7) }
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_statsd_increment_with_value_matched
|
30
|
+
assert StatsD::Instrument::Matchers::Increment.new(:c, 'counter', value: 1).matches? lambda { StatsD.increment('counter') }
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_statsd_increment_with_value_not_matched
|
34
|
+
refute StatsD::Instrument::Matchers::Increment.new(:c, 'counter', value: 3).matches? lambda { StatsD.increment('counter') }
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_statsd_increment_with_tags_matched
|
38
|
+
assert StatsD::Instrument::Matchers::Increment.new(:c, 'counter', tags: ['a', 'b']).matches? lambda { StatsD.increment('counter', tags: ['a', 'b']) }
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_statsd_increment_with_tags_not_matched
|
42
|
+
refute StatsD::Instrument::Matchers::Increment.new(:c, 'counter', tags: ['a', 'b']).matches? lambda { StatsD.increment('counter', tags: ['c']) }
|
43
|
+
end
|
44
|
+
end
|
data/test/metric_test.rb
CHANGED
@@ -26,7 +26,7 @@ class MetricTest < Minitest::Test
|
|
26
26
|
|
27
27
|
def test_handle_bad_tags
|
28
28
|
assert_equal ['ignored'], StatsD::Instrument::Metric.normalize_tags(['igno|red'])
|
29
|
-
assert_equal ['
|
29
|
+
assert_equal ['lol::class:omg::lol'], StatsD::Instrument::Metric.normalize_tags({ :"lol::class" => "omg::lol" })
|
30
30
|
end
|
31
31
|
|
32
32
|
def test_rewrite_tags_provided_as_hash
|
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.8
|
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: 2015-
|
13
|
+
date: 2015-06-29 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rake
|
@@ -40,6 +40,20 @@ dependencies:
|
|
40
40
|
- - '>='
|
41
41
|
- !ruby/object:Gem::Version
|
42
42
|
version: '0'
|
43
|
+
- !ruby/object:Gem::Dependency
|
44
|
+
name: rspec
|
45
|
+
requirement: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
type: :development
|
51
|
+
prerelease: false
|
52
|
+
version_requirements: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
43
57
|
- !ruby/object:Gem::Dependency
|
44
58
|
name: mocha
|
45
59
|
requirement: !ruby/object:Gem::Requirement
|
@@ -107,6 +121,8 @@ files:
|
|
107
121
|
- lib/statsd/instrument/backends/null_backend.rb
|
108
122
|
- lib/statsd/instrument/backends/udp_backend.rb
|
109
123
|
- lib/statsd/instrument/environment.rb
|
124
|
+
- lib/statsd/instrument/helpers.rb
|
125
|
+
- lib/statsd/instrument/matchers.rb
|
110
126
|
- lib/statsd/instrument/metric.rb
|
111
127
|
- lib/statsd/instrument/railtie.rb
|
112
128
|
- lib/statsd/instrument/version.rb
|
@@ -116,8 +132,10 @@ files:
|
|
116
132
|
- test/benchmark/tags.rb
|
117
133
|
- test/capture_backend_test.rb
|
118
134
|
- test/environment_test.rb
|
135
|
+
- test/helpers_test.rb
|
119
136
|
- test/integration_test.rb
|
120
137
|
- test/logger_backend_test.rb
|
138
|
+
- test/matchers_test.rb
|
121
139
|
- test/metric_test.rb
|
122
140
|
- test/statsd_instrumentation_test.rb
|
123
141
|
- test/statsd_test.rb
|
@@ -152,8 +170,10 @@ test_files:
|
|
152
170
|
- test/benchmark/tags.rb
|
153
171
|
- test/capture_backend_test.rb
|
154
172
|
- test/environment_test.rb
|
173
|
+
- test/helpers_test.rb
|
155
174
|
- test/integration_test.rb
|
156
175
|
- test/logger_backend_test.rb
|
176
|
+
- test/matchers_test.rb
|
157
177
|
- test/metric_test.rb
|
158
178
|
- test/statsd_instrumentation_test.rb
|
159
179
|
- test/statsd_test.rb
|