statsd-instrument 2.9.2 → 3.0.0.pre1

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.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +0 -11
  3. data/benchmark/send-metrics-to-dev-null-log +4 -2
  4. data/benchmark/send-metrics-to-local-udp-receiver +7 -6
  5. data/lib/statsd/instrument.rb +35 -69
  6. data/lib/statsd/instrument/assertions.rb +14 -14
  7. data/lib/statsd/instrument/client.rb +0 -10
  8. data/lib/statsd/instrument/environment.rb +1 -23
  9. data/lib/statsd/instrument/expectation.rb +14 -14
  10. data/lib/statsd/instrument/helpers.rb +1 -30
  11. data/lib/statsd/instrument/railtie.rb +0 -4
  12. data/lib/statsd/instrument/strict.rb +12 -118
  13. data/lib/statsd/instrument/version.rb +1 -1
  14. data/test/assertions_test.rb +9 -21
  15. data/test/client_test.rb +11 -0
  16. data/test/environment_test.rb +1 -37
  17. data/test/integration_test.rb +9 -24
  18. data/test/statsd_instrumentation_test.rb +25 -50
  19. data/test/statsd_test.rb +2 -29
  20. metadata +4 -26
  21. data/benchmark/datagram-client +0 -40
  22. data/lib/statsd/instrument/backend.rb +0 -18
  23. data/lib/statsd/instrument/backends/capture_backend.rb +0 -32
  24. data/lib/statsd/instrument/backends/logger_backend.rb +0 -20
  25. data/lib/statsd/instrument/backends/null_backend.rb +0 -9
  26. data/lib/statsd/instrument/backends/udp_backend.rb +0 -152
  27. data/lib/statsd/instrument/legacy_client.rb +0 -301
  28. data/lib/statsd/instrument/metric.rb +0 -155
  29. data/test/assertions_on_legacy_client_test.rb +0 -344
  30. data/test/capture_backend_test.rb +0 -26
  31. data/test/compatibility/dogstatsd_datagram_compatibility_test.rb +0 -161
  32. data/test/deprecations_test.rb +0 -139
  33. data/test/logger_backend_test.rb +0 -22
  34. data/test/metric_test.rb +0 -47
  35. data/test/udp_backend_test.rb +0 -228
@@ -1,344 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class AssertionsOnLegacyClientTest < Minitest::Test
6
- def setup
7
- @old_client = StatsD.singleton_client
8
- StatsD.singleton_client = StatsD.legacy_singleton_client
9
-
10
- test_class = Class.new(Minitest::Test)
11
- test_class.send(:include, StatsD::Instrument::Assertions)
12
- @test_case = test_class.new('fake')
13
- end
14
-
15
- def teardown
16
- StatsD.singleton_client = @old_client
17
- end
18
-
19
- def test_assert_no_statsd_calls
20
- @test_case.assert_no_statsd_calls('counter') do
21
- # noop
22
- end
23
-
24
- @test_case.assert_no_statsd_calls('counter') do
25
- StatsD.increment('other')
26
- end
27
-
28
- assertion = assert_raises(Minitest::Assertion) do
29
- @test_case.assert_no_statsd_calls('counter') do
30
- StatsD.increment('counter')
31
- end
32
- end
33
- assert_equal assertion.message, "No StatsD calls for metric counter expected."
34
-
35
- assertion = assert_raises(Minitest::Assertion) do
36
- @test_case.assert_no_statsd_calls do
37
- StatsD.increment('other')
38
- end
39
- end
40
- assert_equal assertion.message, "No StatsD calls for metric other expected."
41
-
42
- assertion = assert_raises(Minitest::Assertion) do
43
- @test_case.assert_no_statsd_calls do
44
- StatsD.increment('other')
45
- StatsD.increment('another')
46
- end
47
- end
48
- assert_equal assertion.message, "No StatsD calls for metric other, another expected."
49
- end
50
-
51
- def test_assert_statsd_call
52
- @test_case.assert_statsd_increment('counter') do
53
- StatsD.increment('counter')
54
- end
55
-
56
- @test_case.assert_statsd_increment('counter') do
57
- StatsD.increment('counter')
58
- StatsD.increment('other')
59
- end
60
-
61
- assert_raises(Minitest::Assertion) do
62
- @test_case.assert_statsd_increment('counter') do
63
- StatsD.increment('other')
64
- end
65
- end
66
-
67
- assert_raises(Minitest::Assertion) do
68
- @test_case.assert_statsd_increment('counter') do
69
- StatsD.gauge('counter', 42)
70
- end
71
- end
72
-
73
- assert_raises(Minitest::Assertion) do
74
- @test_case.assert_statsd_increment('counter') do
75
- StatsD.increment('counter')
76
- StatsD.increment('counter')
77
- end
78
- end
79
-
80
- @test_case.assert_statsd_increment('counter', times: 2) do
81
- StatsD.increment('counter')
82
- StatsD.increment('counter')
83
- end
84
-
85
- @test_case.assert_statsd_increment('counter', times: 2, tags: ['foo:1']) do
86
- StatsD.increment('counter', tags: { foo: 1 })
87
- StatsD.increment('counter', tags: { foo: 1 })
88
- end
89
-
90
- assert_raises(Minitest::Assertion) do
91
- @test_case.assert_statsd_increment('counter', times: 2, tags: ['foo:1']) do
92
- StatsD.increment('counter', tags: { foo: 1 })
93
- StatsD.increment('counter', tags: { foo: 2 })
94
- end
95
- end
96
-
97
- @test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: ['a', 'b']) do
98
- StatsD.increment('counter', sample_rate: 0.5, tags: ['a', 'b'])
99
- end
100
-
101
- assert_raises(Minitest::Assertion) do
102
- @test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: ['a', 'b']) do
103
- StatsD.increment('counter', sample_rate: 0.2, tags: ['c'])
104
- end
105
- end
106
- end
107
-
108
- def test_assert_statsd_gauge_call_with_numeric_value
109
- @test_case.assert_statsd_gauge('gauge', value: 42) do
110
- StatsD.gauge('gauge', 42)
111
- end
112
-
113
- @test_case.assert_statsd_gauge('gauge', value: '42') do
114
- StatsD.gauge('gauge', 42)
115
- end
116
-
117
- assert_raises(Minitest::Assertion) do
118
- @test_case.assert_statsd_gauge('gauge', value: 42) do
119
- StatsD.gauge('gauge', 45)
120
- end
121
- end
122
- end
123
-
124
- def test_assert_statsd_set_call_with_string_value
125
- @test_case.assert_statsd_set('set', value: 12345) do
126
- StatsD.set('set', '12345')
127
- end
128
-
129
- @test_case.assert_statsd_set('set', value: '12345') do
130
- StatsD.set('set', '12345')
131
- end
132
-
133
- @test_case.assert_statsd_set('set', value: 12345) do
134
- StatsD.set('set', 12345)
135
- end
136
-
137
- @test_case.assert_statsd_set('set', value: '12345') do
138
- StatsD.set('set', 12345)
139
- end
140
-
141
- assert_raises(Minitest::Assertion) do
142
- @test_case.assert_statsd_set('set', value: '42') do
143
- StatsD.set('set', 45)
144
- end
145
- end
146
- end
147
-
148
- def test_tags_will_match_subsets
149
- @test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: { a: 1 }) do
150
- StatsD.increment('counter', sample_rate: 0.5, tags: { a: 1, b: 2 })
151
- end
152
-
153
- assert_raises(Minitest::Assertion) do
154
- @test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: { a: 1, b: 3 }) do
155
- StatsD.increment('counter', sample_rate: 0.5, tags: { a: 1, b: 2, c: 4 })
156
- end
157
- end
158
- end
159
-
160
- def test_tags_friendly_error
161
- assertion = assert_raises(Minitest::Assertion) do
162
- @test_case.assert_statsd_increment('counter', tags: { class: "AnotherJob" }) do
163
- StatsD.increment('counter', tags: { class: "MyJob" })
164
- end
165
- end
166
-
167
- assert_includes assertion.message, "Captured metrics with the same key"
168
- assert_includes assertion.message, "MyJob"
169
- end
170
-
171
- def test_multiple_metrics_are_not_order_dependent
172
- foo_1_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 1, tags: ['foo:1'])
173
- foo_2_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 1, tags: ['foo:2'])
174
- @test_case.assert_statsd_calls([foo_1_metric, foo_2_metric]) do
175
- StatsD.increment('counter', tags: { foo: 1 })
176
- StatsD.increment('counter', tags: { foo: 2 })
177
- end
178
-
179
- foo_1_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 1, tags: ['foo:1'])
180
- foo_2_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 1, tags: ['foo:2'])
181
- @test_case.assert_statsd_calls([foo_2_metric, foo_1_metric]) do
182
- StatsD.increment('counter', tags: { foo: 1 })
183
- StatsD.increment('counter', tags: { foo: 2 })
184
- end
185
-
186
- foo_1_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 2, tags: ['foo:1'])
187
- foo_2_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 1, tags: ['foo:2'])
188
- @test_case.assert_statsd_calls([foo_1_metric, foo_2_metric]) do
189
- StatsD.increment('counter', tags: { foo: 1 })
190
- StatsD.increment('counter', tags: { foo: 1 })
191
- StatsD.increment('counter', tags: { foo: 2 })
192
- end
193
-
194
- foo_1_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 2, tags: ['foo:1'])
195
- foo_2_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 1, tags: ['foo:2'])
196
- @test_case.assert_statsd_calls([foo_2_metric, foo_1_metric]) do
197
- StatsD.increment('counter', tags: { foo: 1 })
198
- StatsD.increment('counter', tags: { foo: 1 })
199
- StatsD.increment('counter', tags: { foo: 2 })
200
- end
201
-
202
- foo_1_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 2, tags: ['foo:1'])
203
- foo_2_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 1, tags: ['foo:2'])
204
- @test_case.assert_statsd_calls([foo_2_metric, foo_1_metric]) do
205
- StatsD.increment('counter', tags: { foo: 1 })
206
- StatsD.increment('counter', tags: { foo: 2 })
207
- StatsD.increment('counter', tags: { foo: 1 })
208
- end
209
- end
210
-
211
- def test_assert_multiple_statsd_calls
212
- assert_raises(Minitest::Assertion) do
213
- foo_1_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 2, tags: ['foo:1'])
214
- foo_2_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 1, tags: ['foo:2'])
215
- @test_case.assert_statsd_calls([foo_1_metric, foo_2_metric]) do
216
- StatsD.increment('counter', tags: { foo: 1 })
217
- StatsD.increment('counter', tags: { foo: 2 })
218
- end
219
- end
220
-
221
- assert_raises(Minitest::Assertion) do
222
- foo_1_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 2, tags: ['foo:1'])
223
- foo_2_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 1, tags: ['foo:2'])
224
- @test_case.assert_statsd_calls([foo_1_metric, foo_2_metric]) do
225
- StatsD.increment('counter', tags: { foo: 1 })
226
- StatsD.increment('counter', tags: { foo: 1 })
227
- StatsD.increment('counter', tags: { foo: 2 })
228
- StatsD.increment('counter', tags: { foo: 2 })
229
- end
230
- end
231
-
232
- foo_1_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 2, tags: ['foo:1'])
233
- foo_2_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 1, tags: ['foo:2'])
234
- @test_case.assert_statsd_calls([foo_1_metric, foo_2_metric]) do
235
- StatsD.increment('counter', tags: { foo: 1 })
236
- StatsD.increment('counter', tags: { foo: 1 })
237
- StatsD.increment('counter', tags: { foo: 2 })
238
- end
239
- end
240
-
241
- def test_assert_statsd_call_with_tags
242
- @test_case.assert_statsd_increment('counter', tags: ['a:b', 'c:d']) do
243
- StatsD.increment('counter', tags: { a: 'b', c: 'd' })
244
- end
245
-
246
- @test_case.assert_statsd_increment('counter', tags: { a: 'b', c: 'd' }) do
247
- StatsD.increment('counter', tags: ['a:b', 'c:d'])
248
- end
249
- end
250
-
251
- def test_nested_assertions
252
- @test_case.assert_statsd_increment('counter1') do
253
- @test_case.assert_statsd_increment('counter2') do
254
- StatsD.increment('counter1')
255
- StatsD.increment('counter2')
256
- end
257
- end
258
-
259
- @test_case.assert_statsd_increment('counter1') do
260
- StatsD.increment('counter1')
261
- @test_case.assert_statsd_increment('counter2') do
262
- StatsD.increment('counter2')
263
- end
264
- end
265
-
266
- assert_raises(Minitest::Assertion) do
267
- @test_case.assert_statsd_increment('counter1') do
268
- @test_case.assert_statsd_increment('counter2') do
269
- StatsD.increment('counter2')
270
- end
271
- end
272
- end
273
-
274
- assert_raises(Minitest::Assertion) do
275
- @test_case.assert_statsd_increment('counter1') do
276
- @test_case.assert_statsd_increment('counter2') do
277
- StatsD.increment('counter1')
278
- end
279
- StatsD.increment('counter2')
280
- end
281
- end
282
- end
283
-
284
- def test_assertion_block_with_expected_exceptions
285
- @test_case.assert_statsd_increment('expected_happened') do
286
- @test_case.assert_raises(RuntimeError) do
287
- begin
288
- raise "expected"
289
- rescue
290
- StatsD.increment('expected_happened')
291
- raise
292
- end
293
- end
294
- end
295
-
296
- assertion = assert_raises(Minitest::Assertion) do
297
- @test_case.assert_statsd_increment('counter') do
298
- @test_case.assert_raises(RuntimeError) do
299
- raise "expected"
300
- end
301
- end
302
- end
303
- assert_includes assertion.message, "No StatsD calls for metric counter of type c were made"
304
- end
305
-
306
- def test_assertion_block_with_unexpected_exceptions
307
- assertion = assert_raises(Minitest::Assertion) do
308
- @test_case.assert_statsd_increment('counter') do
309
- StatsD.increment('counter')
310
- raise "unexpected"
311
- end
312
- end
313
- assert_includes assertion.message, "An exception occurred in the block provided to the StatsD assertion"
314
-
315
- assertion = assert_raises(Minitest::Assertion) do
316
- @test_case.assert_raises(RuntimeError) do
317
- @test_case.assert_statsd_increment('counter') do
318
- StatsD.increment('counter')
319
- raise "unexpected"
320
- end
321
- end
322
- end
323
- assert_includes assertion.message, "An exception occurred in the block provided to the StatsD assertion"
324
-
325
- assertion = assert_raises(Minitest::Assertion) do
326
- @test_case.assert_raises(RuntimeError) do
327
- @test_case.assert_no_statsd_calls do
328
- raise "unexpected"
329
- end
330
- end
331
- end
332
- assert_includes assertion.message, "An exception occurred in the block provided to the StatsD assertion"
333
- end
334
-
335
- def test_assertion_block_with_other_assertion_failures
336
- # If another assertion failure happens inside the block, that failure should have priority
337
- assertion = assert_raises(Minitest::Assertion) do
338
- @test_case.assert_statsd_increment('counter') do
339
- @test_case.flunk('other assertion failure')
340
- end
341
- end
342
- assert_equal "other assertion failure", assertion.message
343
- end
344
- end
@@ -1,26 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class CaptureBackendTest < Minitest::Test
6
- def setup
7
- @backend = StatsD::Instrument::Backends::CaptureBackend.new
8
- @metric1 = StatsD::Instrument::Metric.new(type: :c, name: 'mock.counter')
9
- @metric2 = StatsD::Instrument::Metric.new(type: :ms, name: 'mock.measure', value: 123)
10
- end
11
-
12
- def test_collecting_metric
13
- assert @backend.collected_metrics.empty?
14
- @backend.collect_metric(@metric1)
15
- @backend.collect_metric(@metric2)
16
- assert_equal [@metric1, @metric2], @backend.collected_metrics
17
- end
18
-
19
- def test_reset
20
- @backend.collect_metric(@metric1)
21
- @backend.reset
22
- assert @backend.collected_metrics.empty?
23
- @backend.collect_metric(@metric2)
24
- assert_equal [@metric2], @backend.collected_metrics
25
- end
26
- end
@@ -1,161 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- module Compatibility
6
- class DogStatsDDatagramCompatibilityTest < Minitest::Test
7
- def setup
8
- StatsD::Instrument::UDPSink.any_instance.stubs(:sample?).returns(true)
9
- StatsD::Instrument::Backends::UDPBackend.any_instance.stubs(:rand).returns(0)
10
-
11
- @server = UDPSocket.new
12
- @server.bind('localhost', 0)
13
- @host = @server.addr[2]
14
- @port = @server.addr[1]
15
- end
16
-
17
- def teardown
18
- @server.close
19
- end
20
-
21
- def test_increment_compatibility
22
- assert_equal_datagrams { |client| client.increment('counter') }
23
- assert_equal_datagrams { |client| client.increment('counter', no_prefix: true) }
24
- assert_equal_datagrams { |client| client.increment('counter', 12) }
25
- assert_equal_datagrams { |client| client.increment('counter', sample_rate: 0.1) }
26
- assert_equal_datagrams { |client| client.increment('counter', tags: ['foo', 'bar']) }
27
- assert_equal_datagrams { |client| client.increment('counter', tags: { foo: 'bar' }) }
28
- assert_equal_datagrams { |client| client.increment('counter', sample_rate: 0.1, tags: ['quc']) }
29
- end
30
-
31
- def test_measure_compatibility
32
- assert_equal_datagrams { |client| client.measure('timing', 12.34) }
33
- assert_equal_datagrams { |client| client.measure('timing', 0.01, no_prefix: true) }
34
- assert_equal_datagrams { |client| client.measure('timing', 0.12, sample_rate: 0.1) }
35
- assert_equal_datagrams { |client| client.measure('timing', 0.12, tags: ['foo', 'bar']) }
36
- end
37
-
38
- def test_measure_with_block_compatibility
39
- Process.stubs(:clock_gettime).with(Process::CLOCK_MONOTONIC).returns(12.1)
40
- assert_equal_datagrams do |client|
41
- return_value = client.measure('timing', tags: ['foo'], sample_rate: 0.1) { 'foo' }
42
- assert_equal 'foo', return_value
43
- end
44
- end
45
-
46
- def test_gauge_compatibility
47
- assert_equal_datagrams { |client| client.gauge('current', 1234) }
48
- assert_equal_datagrams { |client| client.gauge('current', 1234, no_prefix: true) }
49
- assert_equal_datagrams { |client| client.gauge('current', 1234, sample_rate: 0.1) }
50
- assert_equal_datagrams { |client| client.gauge('current', 1234, tags: ['foo', 'bar']) }
51
- assert_equal_datagrams { |client| client.gauge('current', 1234, tags: { foo: 'bar' }) }
52
- assert_equal_datagrams { |client| client.gauge('current', 1234, sample_rate: 0.1, tags: ['quc']) }
53
- end
54
-
55
- def test_set_compatibility
56
- assert_equal_datagrams { |client| client.set('unique', 'foo') }
57
- assert_equal_datagrams { |client| client.set('unique', 'foo', no_prefix: true) }
58
- assert_equal_datagrams { |client| client.set('unique', 'foo', sample_rate: 0.1) }
59
- assert_equal_datagrams { |client| client.set('unique', '1234', tags: ['foo', 'bar']) }
60
- assert_equal_datagrams { |client| client.set('unique', '1234', tags: { foo: 'bar' }) }
61
- assert_equal_datagrams { |client| client.set('unique', '1234', sample_rate: 0.1, tags: ['quc']) }
62
- end
63
-
64
- def test_histogram_compatibility
65
- assert_equal_datagrams { |client| client.histogram('sample', 12.44) }
66
- assert_equal_datagrams { |client| client.histogram('sample', 12.44, no_prefix: true) }
67
- assert_equal_datagrams { |client| client.histogram('sample', 12.44, sample_rate: 0.1) }
68
- assert_equal_datagrams { |client| client.histogram('sample', 12.44, tags: ['foo', 'bar']) }
69
- assert_equal_datagrams { |client| client.histogram('sample', 12.44, tags: { foo: 'bar' }) }
70
- assert_equal_datagrams { |client| client.histogram('sample', 12.44, sample_rate: 0.1, tags: ['quc']) }
71
- end
72
-
73
- def test_distribution_compatibility
74
- assert_equal_datagrams { |client| client.distribution('sample', 12.44) }
75
- assert_equal_datagrams { |client| client.distribution('sample', 12.44, no_prefix: true) }
76
- assert_equal_datagrams { |client| client.distribution('sample', 12.44, sample_rate: 0.1) }
77
- assert_equal_datagrams { |client| client.distribution('sample', 12.44, tags: ['foo', 'bar']) }
78
- assert_equal_datagrams { |client| client.distribution('sample', 12.44, tags: { foo: 'bar' }) }
79
- assert_equal_datagrams { |client| client.distribution('sample', 12.44, sample_rate: 0.1, tags: ['quc']) }
80
- end
81
-
82
- def test_distribution_with_block_compatibility
83
- Process.stubs(:clock_gettime).with(Process::CLOCK_MONOTONIC).returns(12.1)
84
- assert_equal_datagrams do |client|
85
- return_value = client.distribution('timing', tags: ['foo'], sample_rate: 0.1) { 'foo' }
86
- assert_equal 'foo', return_value
87
- end
88
- end
89
-
90
- def test_service_check_compatibility
91
- assert_equal_datagrams { |client| client.service_check('service', 0) }
92
- assert_equal_datagrams { |client| client.service_check('service', :critical, no_prefix: true) }
93
- assert_equal_datagrams { |client| client.event('foo', "bar\nbaz") }
94
- assert_equal_datagrams do |client|
95
- client.service_check('service', "ok", timestamp: Time.parse('2019-09-09T04:22:17Z'),
96
- hostname: 'localhost', tags: ['foo'], message: 'bar')
97
- end
98
- end
99
-
100
- def test_event_compatibility
101
- assert_equal_datagrams { |client| client.event('foo', "bar\nbaz") }
102
- assert_equal_datagrams { |client| client.event('foo', "bar\nbaz", no_prefix: true) }
103
- assert_equal_datagrams do |client|
104
- client.event('Something happened', "And it's not good", timestamp: Time.parse('2019-09-09T04:22:17Z'),
105
- hostname: 'localhost', tags: ['foo'], alert_type: 'warning', priority: 'low',
106
- aggregation_key: 'foo', source_type_name: 'logs')
107
- end
108
- end
109
-
110
- private
111
-
112
- MODES = [:normal, :with_prefix, :with_default_tags]
113
-
114
- def assert_equal_datagrams(&block)
115
- MODES.each do |mode|
116
- legacy_datagram = with_legacy_client(mode) { |client| read_datagram(client, &block) }
117
- new_datagram = with_new_client(mode) { |client| read_datagram(client, &block) }
118
-
119
- assert_equal legacy_datagram, new_datagram, "The datagrams emitted were not the same in #{mode} mode"
120
- end
121
- end
122
-
123
- def with_legacy_client(mode)
124
- old_prefix = StatsD.prefix
125
- StatsD.prefix = 'prefix' if mode == :with_prefix
126
-
127
- old_default_tags = StatsD.default_tags
128
- StatsD.default_tags = { key: 'value' } if mode == :with_default_tags
129
-
130
- old_backend = StatsD.backend
131
- new_backend = StatsD::Instrument::Backends::UDPBackend.new("#{@host}:#{@port}", :datadog)
132
- StatsD.backend = new_backend
133
-
134
- yield(StatsD)
135
- ensure
136
- new_backend.socket.close if new_backend&.socket
137
- StatsD.backend = old_backend
138
- StatsD.prefix = old_prefix
139
- StatsD.default_tags = old_default_tags
140
- end
141
-
142
- def with_new_client(mode)
143
- prefix = mode == :with_prefix ? 'prefix' : nil
144
- default_tags = mode == :with_default_tags ? { key: 'value' } : nil
145
- client = StatsD::Instrument::Client.new(
146
- sink: StatsD::Instrument::UDPSink.new(@host, @port),
147
- datagram_builder_class: StatsD::Instrument::DogStatsDDatagramBuilder,
148
- prefix: prefix,
149
- default_tags: default_tags
150
- )
151
-
152
- yield(client)
153
- end
154
-
155
- def read_datagram(client)
156
- yield(client)
157
- data, _origin = @server.recvfrom(100)
158
- data
159
- end
160
- end
161
- end