statsd-instrument 2.4.0 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/benchmark.yml +32 -0
  3. data/.github/workflows/ci.yml +24 -8
  4. data/.rubocop.yml +24 -0
  5. data/CHANGELOG.md +116 -3
  6. data/CONTRIBUTING.md +8 -6
  7. data/Gemfile +3 -0
  8. data/Rakefile +1 -1
  9. data/benchmark/README.md +29 -0
  10. data/benchmark/send-metrics-to-dev-null-log +47 -0
  11. data/benchmark/send-metrics-to-local-udp-receiver +57 -0
  12. data/lib/statsd/instrument.rb +126 -94
  13. data/lib/statsd/instrument/assertions.rb +69 -37
  14. data/lib/statsd/instrument/backends/capture_backend.rb +2 -0
  15. data/lib/statsd/instrument/helpers.rb +12 -8
  16. data/lib/statsd/instrument/metric.rb +56 -42
  17. data/lib/statsd/instrument/rubocop/metaprogramming_positional_arguments.rb +46 -0
  18. data/lib/statsd/instrument/rubocop/metric_return_value.rb +31 -0
  19. data/lib/statsd/instrument/rubocop/metric_value_keyword_argument.rb +45 -0
  20. data/lib/statsd/instrument/rubocop/positional_arguments.rb +99 -0
  21. data/lib/statsd/instrument/rubocop/splat_arguments.rb +37 -0
  22. data/lib/statsd/instrument/strict.rb +145 -0
  23. data/lib/statsd/instrument/version.rb +1 -1
  24. data/test/assertions_test.rb +37 -0
  25. data/test/benchmark/clock_gettime.rb +27 -0
  26. data/test/benchmark/default_tags.rb +1 -1
  27. data/test/deprecations_test.rb +86 -0
  28. data/test/helpers/rubocop_helper.rb +47 -0
  29. data/test/integration_test.rb +6 -2
  30. data/test/matchers_test.rb +9 -9
  31. data/test/metric_test.rb +3 -18
  32. data/test/rubocop/metaprogramming_positional_arguments_test.rb +58 -0
  33. data/test/rubocop/metric_return_value_test.rb +78 -0
  34. data/test/rubocop/metric_value_keyword_argument_test.rb +39 -0
  35. data/test/rubocop/positional_arguments_test.rb +110 -0
  36. data/test/rubocop/splat_arguments_test.rb +27 -0
  37. data/test/statsd_instrumentation_test.rb +77 -86
  38. data/test/statsd_test.rb +32 -65
  39. data/test/test_helper.rb +12 -1
  40. data/test/udp_backend_test.rb +8 -0
  41. metadata +28 -2
data/test/statsd_test.rb CHANGED
@@ -15,9 +15,7 @@ class StatsDTest < Minitest::Test
15
15
  end
16
16
 
17
17
  def test_statsd_measure_with_explicit_value
18
- result = nil
19
- metric = capture_statsd_call { result = StatsD.measure('values.foobar', 42) }
20
- assert_equal metric, result
18
+ metric = capture_statsd_call { StatsD.measure('values.foobar', 42) }
21
19
  assert_equal 'values.foobar', metric.name
22
20
  assert_equal 42, metric.value
23
21
  assert_equal :ms, metric.type
@@ -28,20 +26,6 @@ class StatsDTest < Minitest::Test
28
26
  assert_equal :d, metric.type
29
27
  end
30
28
 
31
- def test_statsd_measure_with_explicit_value_as_keyword_argument
32
- result = nil
33
- metric = capture_statsd_call { result = StatsD.measure('values.foobar', value: 42) }
34
- assert_equal metric, result
35
- assert_equal 'values.foobar', metric.name
36
- assert_equal 42, metric.value
37
- assert_equal :ms, metric.type
38
- end
39
-
40
- def test_statsd_measure_with_explicit_value_keyword_and_distribution_override
41
- metric = capture_statsd_call { StatsD.measure('values.foobar', value: 42, as_dist: true) }
42
- assert_equal :d, metric.type
43
- end
44
-
45
29
  def test_statsd_measure_without_value_or_block
46
30
  assert_raises(ArgumentError) { StatsD.measure('values.foobar', tags: 123) }
47
31
  end
@@ -52,7 +36,7 @@ class StatsDTest < Minitest::Test
52
36
  end
53
37
 
54
38
  def test_statsd_measure_with_benchmarked_block_duration
55
- StatsD::Instrument.stubs(:current_timestamp).returns(5.0, 5.0 + 1.12)
39
+ Process.stubs(:clock_gettime).returns(5.0, 5.0 + 1.12)
56
40
  metric = capture_statsd_call do
57
41
  StatsD.measure('values.foobar') { 'foo' }
58
42
  end
@@ -77,7 +61,7 @@ class StatsDTest < Minitest::Test
77
61
  end
78
62
 
79
63
  def test_statsd_measure_with_return_in_block_still_captures
80
- StatsD::Instrument.stubs(:current_timestamp).returns(5.0, 6.12)
64
+ Process.stubs(:clock_gettime).returns(5.0, 6.12)
81
65
  result = nil
82
66
  metric = capture_statsd_call do
83
67
  lambda = -> do
@@ -92,7 +76,7 @@ class StatsDTest < Minitest::Test
92
76
  end
93
77
 
94
78
  def test_statsd_measure_with_exception_in_block_still_captures
95
- StatsD::Instrument.stubs(:current_timestamp).returns(5.0, 6.12)
79
+ Process.stubs(:clock_gettime).returns(5.0, 6.12)
96
80
  result = nil
97
81
  metric = capture_statsd_call do
98
82
  lambda = -> do
@@ -110,9 +94,7 @@ class StatsDTest < Minitest::Test
110
94
  end
111
95
 
112
96
  def test_statsd_increment
113
- result = nil
114
- metric = capture_statsd_call { result = StatsD.increment('values.foobar', 3) }
115
- assert_equal metric, result
97
+ metric = capture_statsd_call { StatsD.increment('values.foobar', 3) }
116
98
  assert_equal :c, metric.type
117
99
  assert_equal 'values.foobar', metric.name
118
100
  assert_equal 3, metric.value
@@ -125,83 +107,55 @@ class StatsDTest < Minitest::Test
125
107
  assert_equal 1, metric.value
126
108
  end
127
109
 
128
- def test_statsd_increment_with_value_as_keyword_argument
129
- metric = capture_statsd_call { StatsD.increment('values.foobar', value: 2) }
130
- assert_equal StatsD.default_sample_rate, metric.sample_rate
131
- assert_equal 2, metric.value
132
- end
133
-
134
- def test_statsd_increment_with_multiple_arguments
135
- metric = capture_statsd_call { StatsD.increment('values.foobar', 12, nil, ['test']) }
136
- assert_equal StatsD.default_sample_rate, metric.sample_rate
137
- assert_equal ['test'], metric.tags
138
- assert_equal 12, metric.value
139
- end
140
-
141
110
  def test_statsd_gauge
142
- result = nil
143
- metric = capture_statsd_call { result = StatsD.gauge('values.foobar', 12) }
144
- assert_equal metric, result
111
+ metric = capture_statsd_call { StatsD.gauge('values.foobar', 12) }
145
112
  assert_equal :g, metric.type
146
113
  assert_equal 'values.foobar', metric.name
147
114
  assert_equal 12, metric.value
148
115
  end
149
116
 
150
- def test_statsd_gauge_with_keyword_argument
151
- result = nil
152
- metric = capture_statsd_call { result = StatsD.gauge('values.foobar', value: 13) }
153
- assert_equal metric, result
154
- assert_equal :g, metric.type
155
- assert_equal 'values.foobar', metric.name
156
- assert_equal 13, metric.value
157
- end
158
-
159
117
  def test_statsd_gauge_without_value
160
118
  assert_raises(ArgumentError) { StatsD.gauge('values.foobar', tags: 123) }
161
119
  end
162
120
 
163
121
  def test_statsd_set
164
- result = nil
165
- metric = capture_statsd_call { result = StatsD.set('values.foobar', 'unique_identifier') }
166
- assert_equal metric, result
122
+ metric = capture_statsd_call { StatsD.set('values.foobar', 'unique_identifier') }
167
123
  assert_equal :s, metric.type
168
124
  assert_equal 'values.foobar', metric.name
169
125
  assert_equal 'unique_identifier', metric.value
170
126
  end
171
127
 
172
128
  def test_statsd_histogram
173
- result = nil
174
- metric = capture_statsd_call { result = StatsD.histogram('values.foobar', 42) }
175
- assert_equal metric, result
129
+ metric = capture_statsd_call { StatsD.histogram('values.foobar', 42) }
176
130
  assert_equal :h, metric.type
177
131
  assert_equal 'values.foobar', metric.name
178
132
  assert_equal 42, metric.value
179
133
  end
180
134
 
181
135
  def test_statsd_distribution
182
- result = nil
183
- metric = capture_statsd_call { result = StatsD.distribution('values.foobar', 42) }
184
- assert_equal metric, result
136
+ metric = capture_statsd_call { StatsD.distribution('values.foobar', 42) }
185
137
  assert_equal :d, metric.type
186
138
  assert_equal 'values.foobar', metric.name
187
139
  assert_equal 42, metric.value
188
140
  end
189
141
 
190
142
  def test_statsd_distribution_with_benchmarked_block_duration
191
- StatsD::Instrument.stubs(:current_timestamp).returns(5.0, 5.0 + 1.12)
143
+ Process.stubs(:clock_gettime).returns(5.0, 5.0 + 1.12)
192
144
  metric = capture_statsd_call do
193
- StatsD.distribution('values.foobar') { 'foo' }
145
+ result = StatsD.distribution('values.foobar') { 'foo' }
146
+ assert_equal 'foo', result
194
147
  end
195
148
  assert_equal :d, metric.type
196
149
  assert_equal 1120.0, metric.value
197
150
  end
198
151
 
199
152
  def test_statsd_distribution_with_return_in_block_still_captures
200
- StatsD::Instrument.stubs(:current_timestamp).returns(5.0, 5.0 + 1.12)
153
+ Process.stubs(:clock_gettime).returns(5.0, 5.0 + 1.12)
201
154
  result = nil
202
155
  metric = capture_statsd_call do
203
156
  lambda = -> do
204
157
  StatsD.distribution('values.foobar') { return 'from lambda' }
158
+ flunk("This code should not be reached")
205
159
  end
206
160
 
207
161
  result = lambda.call
@@ -213,7 +167,7 @@ class StatsDTest < Minitest::Test
213
167
  end
214
168
 
215
169
  def test_statsd_distribution_with_exception_in_block_still_captures
216
- StatsD::Instrument.stubs(:current_timestamp).returns(5.0, 5.0 + 1.12)
170
+ Process.stubs(:clock_gettime).returns(5.0, 5.0 + 1.12)
217
171
  result = nil
218
172
  metric = capture_statsd_call do
219
173
  lambda = -> do
@@ -232,7 +186,7 @@ class StatsDTest < Minitest::Test
232
186
  end
233
187
 
234
188
  def test_statsd_distribution_with_block_and_options
235
- StatsD::Instrument.stubs(:current_timestamp).returns(5.0, 5.0 + 1.12)
189
+ Process.stubs(:clock_gettime).returns(5.0, 5.0 + 1.12)
236
190
  metric = capture_statsd_call do
237
191
  StatsD.distribution('values.foobar', tags: ['test'], sample_rate: 0.9) { 'foo' }
238
192
  end
@@ -253,9 +207,7 @@ class StatsDTest < Minitest::Test
253
207
  end
254
208
 
255
209
  def test_statsd_key_value
256
- result = nil
257
- metric = capture_statsd_call { result = StatsD.key_value('values.foobar', 42) }
258
- assert_equal metric, result
210
+ metric = capture_statsd_call { StatsD.key_value('values.foobar', 42) }
259
211
  assert_equal :kv, metric.type
260
212
  assert_equal 'values.foobar', metric.name
261
213
  assert_equal 42, metric.value
@@ -277,6 +229,21 @@ class StatsDTest < Minitest::Test
277
229
  assert_equal ['first_tag:first_value', 'second_tag:second_value'], StatsD.default_tags
278
230
  end
279
231
 
232
+ def test_name_prefix
233
+ StatsD.stubs(:prefix).returns('prefix')
234
+ m = capture_statsd_call { StatsD.increment('counter') }
235
+ assert_equal 'prefix.counter', m.name
236
+
237
+ m = capture_statsd_call { StatsD.increment('counter', no_prefix: true) }
238
+ assert_equal 'counter', m.name
239
+
240
+ m = capture_statsd_call { StatsD.increment('counter', prefix: "foobar") }
241
+ assert_equal 'foobar.counter', m.name
242
+
243
+ m = capture_statsd_call { StatsD.increment('counter', prefix: "foobar", no_prefix: true) }
244
+ assert_equal 'counter', m.name
245
+ end
246
+
280
247
  protected
281
248
 
282
249
  def capture_statsd_call(&block)
data/test/test_helper.rb CHANGED
@@ -9,4 +9,15 @@ require 'set'
9
9
  require 'logger'
10
10
  require 'statsd-instrument'
11
11
 
12
- StatsD.logger = Logger.new('/dev/null')
12
+ require_relative 'helpers/rubocop_helper'
13
+
14
+ require 'statsd/instrument/strict' if ENV['STATSD_STRICT_MODE']
15
+
16
+ module StatsD::Instrument
17
+ def self.strict_mode_enabled?
18
+ StatsD::Instrument.const_defined?(:Strict) &&
19
+ StatsD.singleton_class.ancestors.include?(StatsD::Instrument::Strict)
20
+ end
21
+ end
22
+
23
+ StatsD.logger = Logger.new(File::NULL)
@@ -148,11 +148,19 @@ class UDPBackendTest < Minitest::Test
148
148
  StatsD.key_value('fooy', 42)
149
149
  end
150
150
 
151
+ # For key_value metrics (only supported by statsite), the sample rate
152
+ # part of the datagram format is (ab)used to be set to a timestamp instead.
153
+ # Changing that to `sample_rate: timestamp` does not make sense, so we
154
+ # disable the rubocop rule for positional arguments for now,
155
+ # until we figure out how we want to handle this.
156
+
157
+ # rubocop:disable StatsD/PositionalArguments
151
158
  def test_supports_key_value_with_timestamp_on_statsite
152
159
  @backend.implementation = :statsite
153
160
  @backend.expects(:write_packet).with("fooy:42|kv|@123456\n")
154
161
  StatsD.key_value('fooy', 42, 123456)
155
162
  end
163
+ # rubocop:enable StatsD/PositionalArguments
156
164
 
157
165
  def test_warn_when_using_key_value_and_not_on_statsite
158
166
  @backend.implementation = :other
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.4.0
4
+ version: 2.5.0
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: 2019-09-20 00:00:00.000000000 Z
13
+ date: 2019-09-27 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
@@ -120,6 +120,7 @@ extra_rdoc_files: []
120
120
  files:
121
121
  - ".github/CODEOWNERS"
122
122
  - ".github/probots.yml"
123
+ - ".github/workflows/benchmark.yml"
123
124
  - ".github/workflows/ci.yml"
124
125
  - ".gitignore"
125
126
  - ".rubocop-https---shopify-github-io-ruby-style-guide-rubocop-yml"
@@ -130,6 +131,9 @@ files:
130
131
  - LICENSE
131
132
  - README.md
132
133
  - Rakefile
134
+ - benchmark/README.md
135
+ - benchmark/send-metrics-to-dev-null-log
136
+ - benchmark/send-metrics-to-local-udp-receiver
133
137
  - lib/statsd-instrument.rb
134
138
  - lib/statsd/instrument.rb
135
139
  - lib/statsd/instrument/assertions.rb
@@ -144,20 +148,34 @@ files:
144
148
  - lib/statsd/instrument/metric.rb
145
149
  - lib/statsd/instrument/metric_expectation.rb
146
150
  - lib/statsd/instrument/railtie.rb
151
+ - lib/statsd/instrument/rubocop/metaprogramming_positional_arguments.rb
152
+ - lib/statsd/instrument/rubocop/metric_return_value.rb
153
+ - lib/statsd/instrument/rubocop/metric_value_keyword_argument.rb
154
+ - lib/statsd/instrument/rubocop/positional_arguments.rb
155
+ - lib/statsd/instrument/rubocop/splat_arguments.rb
156
+ - lib/statsd/instrument/strict.rb
147
157
  - lib/statsd/instrument/version.rb
148
158
  - shipit.rubygems.yml
149
159
  - statsd-instrument.gemspec
150
160
  - test/assertions_test.rb
161
+ - test/benchmark/clock_gettime.rb
151
162
  - test/benchmark/default_tags.rb
152
163
  - test/benchmark/metrics.rb
153
164
  - test/benchmark/tags.rb
154
165
  - test/capture_backend_test.rb
166
+ - test/deprecations_test.rb
155
167
  - test/environment_test.rb
168
+ - test/helpers/rubocop_helper.rb
156
169
  - test/helpers_test.rb
157
170
  - test/integration_test.rb
158
171
  - test/logger_backend_test.rb
159
172
  - test/matchers_test.rb
160
173
  - test/metric_test.rb
174
+ - test/rubocop/metaprogramming_positional_arguments_test.rb
175
+ - test/rubocop/metric_return_value_test.rb
176
+ - test/rubocop/metric_value_keyword_argument_test.rb
177
+ - test/rubocop/positional_arguments_test.rb
178
+ - test/rubocop/splat_arguments_test.rb
161
179
  - test/statsd_instrumentation_test.rb
162
180
  - test/statsd_test.rb
163
181
  - test/test_helper.rb
@@ -187,16 +205,24 @@ specification_version: 4
187
205
  summary: A StatsD client for Ruby apps
188
206
  test_files:
189
207
  - test/assertions_test.rb
208
+ - test/benchmark/clock_gettime.rb
190
209
  - test/benchmark/default_tags.rb
191
210
  - test/benchmark/metrics.rb
192
211
  - test/benchmark/tags.rb
193
212
  - test/capture_backend_test.rb
213
+ - test/deprecations_test.rb
194
214
  - test/environment_test.rb
215
+ - test/helpers/rubocop_helper.rb
195
216
  - test/helpers_test.rb
196
217
  - test/integration_test.rb
197
218
  - test/logger_backend_test.rb
198
219
  - test/matchers_test.rb
199
220
  - test/metric_test.rb
221
+ - test/rubocop/metaprogramming_positional_arguments_test.rb
222
+ - test/rubocop/metric_return_value_test.rb
223
+ - test/rubocop/metric_value_keyword_argument_test.rb
224
+ - test/rubocop/positional_arguments_test.rb
225
+ - test/rubocop/splat_arguments_test.rb
200
226
  - test/statsd_instrumentation_test.rb
201
227
  - test/statsd_test.rb
202
228
  - test/test_helper.rb