statsd-instrument 3.0.0.pre1 → 3.1.0

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 (76) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/lint.yml +22 -0
  3. data/.github/workflows/tests.yml +31 -0
  4. data/.rubocop.yml +3 -13
  5. data/CHANGELOG.md +50 -0
  6. data/Gemfile +8 -2
  7. data/README.md +6 -3
  8. data/Rakefile +7 -7
  9. data/benchmark/send-metrics-to-dev-null-log +12 -11
  10. data/benchmark/send-metrics-to-local-udp-receiver +16 -15
  11. data/bin/rake +29 -0
  12. data/bin/rubocop +29 -0
  13. data/lib/statsd-instrument.rb +1 -1
  14. data/lib/statsd/instrument.rb +112 -145
  15. data/lib/statsd/instrument/assertions.rb +200 -208
  16. data/lib/statsd/instrument/batched_udp_sink.rb +154 -0
  17. data/lib/statsd/instrument/capture_sink.rb +23 -19
  18. data/lib/statsd/instrument/client.rb +410 -306
  19. data/lib/statsd/instrument/datagram.rb +69 -65
  20. data/lib/statsd/instrument/datagram_builder.rb +81 -77
  21. data/lib/statsd/instrument/dogstatsd_datagram.rb +76 -72
  22. data/lib/statsd/instrument/dogstatsd_datagram_builder.rb +68 -64
  23. data/lib/statsd/instrument/environment.rb +88 -77
  24. data/lib/statsd/instrument/expectation.rb +96 -96
  25. data/lib/statsd/instrument/helpers.rb +11 -7
  26. data/lib/statsd/instrument/log_sink.rb +20 -16
  27. data/lib/statsd/instrument/matchers.rb +93 -74
  28. data/lib/statsd/instrument/null_sink.rb +12 -8
  29. data/lib/statsd/instrument/railtie.rb +11 -7
  30. data/lib/statsd/instrument/rubocop.rb +8 -8
  31. data/lib/statsd/instrument/rubocop/measure_as_dist_argument.rb +1 -1
  32. data/lib/statsd/instrument/rubocop/metaprogramming_positional_arguments.rb +2 -2
  33. data/lib/statsd/instrument/rubocop/metric_prefix_argument.rb +1 -1
  34. data/lib/statsd/instrument/rubocop/metric_return_value.rb +2 -2
  35. data/lib/statsd/instrument/rubocop/metric_value_keyword_argument.rb +1 -1
  36. data/lib/statsd/instrument/rubocop/positional_arguments.rb +4 -4
  37. data/lib/statsd/instrument/rubocop/singleton_configuration.rb +1 -1
  38. data/lib/statsd/instrument/rubocop/splat_arguments.rb +2 -2
  39. data/lib/statsd/instrument/statsd_datagram_builder.rb +12 -8
  40. data/lib/statsd/instrument/strict.rb +1 -6
  41. data/lib/statsd/instrument/udp_sink.rb +49 -47
  42. data/lib/statsd/instrument/version.rb +1 -1
  43. data/statsd-instrument.gemspec +4 -8
  44. data/test/assertions_test.rb +205 -161
  45. data/test/benchmark/clock_gettime.rb +1 -1
  46. data/test/benchmark/default_tags.rb +9 -9
  47. data/test/benchmark/metrics.rb +8 -8
  48. data/test/benchmark/tags.rb +4 -4
  49. data/test/capture_sink_test.rb +14 -14
  50. data/test/client_test.rb +96 -96
  51. data/test/datagram_builder_test.rb +55 -55
  52. data/test/datagram_test.rb +5 -5
  53. data/test/dogstatsd_datagram_builder_test.rb +37 -37
  54. data/test/environment_test.rb +30 -21
  55. data/test/helpers/rubocop_helper.rb +12 -9
  56. data/test/helpers_test.rb +15 -15
  57. data/test/integration_test.rb +7 -7
  58. data/test/log_sink_test.rb +4 -4
  59. data/test/matchers_test.rb +54 -54
  60. data/test/null_sink_test.rb +4 -4
  61. data/test/rubocop/measure_as_dist_argument_test.rb +2 -2
  62. data/test/rubocop/metaprogramming_positional_arguments_test.rb +2 -2
  63. data/test/rubocop/metric_prefix_argument_test.rb +2 -2
  64. data/test/rubocop/metric_return_value_test.rb +6 -6
  65. data/test/rubocop/metric_value_keyword_argument_test.rb +3 -3
  66. data/test/rubocop/positional_arguments_test.rb +12 -12
  67. data/test/rubocop/singleton_configuration_test.rb +8 -8
  68. data/test/rubocop/splat_arguments_test.rb +2 -2
  69. data/test/statsd_datagram_builder_test.rb +6 -6
  70. data/test/statsd_instrumentation_test.rb +122 -122
  71. data/test/statsd_test.rb +69 -67
  72. data/test/test_helper.rb +19 -10
  73. data/test/udp_sink_test.rb +122 -50
  74. metadata +12 -92
  75. data/.github/workflows/ci.yml +0 -56
  76. data/.rubocop-https---shopify-github-io-ruby-style-guide-rubocop-yml +0 -1027
@@ -1,14 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # @note This class is part of the new Client implementation that is intended
4
- # to become the new default in the next major release of this library.
5
- class StatsD::Instrument::StatsDDatagramBuilder < StatsD::Instrument::DatagramBuilder
6
- unsupported_datagram_types :h, :d, :kv
3
+ module StatsD
4
+ module Instrument
5
+ # @note This class is part of the new Client implementation that is intended
6
+ # to become the new default in the next major release of this library.
7
+ class StatsDDatagramBuilder < StatsD::Instrument::DatagramBuilder
8
+ unsupported_datagram_types :h, :d, :kv
7
9
 
8
- protected
10
+ protected
9
11
 
10
- def normalize_tags(tags)
11
- raise NotImplementedError, "#{self.class.name} does not support tags" if tags
12
- super
12
+ def normalize_tags(tags)
13
+ raise NotImplementedError, "#{self.class.name} does not support tags" if tags
14
+ super
15
+ end
16
+ end
13
17
  end
14
18
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'statsd-instrument' unless Object.const_defined?(:StatsD)
3
+ require "statsd-instrument" unless Object.const_defined?(:StatsD)
4
4
 
5
5
  module StatsD
6
6
  module Instrument
@@ -79,11 +79,6 @@ module StatsD
79
79
  super
80
80
  end
81
81
 
82
- def key_value(*)
83
- raise NotImplementedError, "The key_value metric type will be removed " \
84
- "from the next major version of statsd-instrument"
85
- end
86
-
87
82
  private
88
83
 
89
84
  def check_block_or_numeric_value(value)
@@ -1,62 +1,64 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # @note This class is part of the new Client implementation that is intended
4
- # to become the new default in the next major release of this library.
5
- class StatsD::Instrument::UDPSink
6
- def self.for_addr(addr)
7
- host, port_as_string = addr.split(':', 2)
8
- new(host, Integer(port_as_string))
9
- end
3
+ module StatsD
4
+ module Instrument
5
+ # @note This class is part of the new Client implementation that is intended
6
+ # to become the new default in the next major release of this library.
7
+ class UDPSink
8
+ def self.for_addr(addr)
9
+ host, port_as_string = addr.split(":", 2)
10
+ new(host, Integer(port_as_string))
11
+ end
10
12
 
11
- attr_reader :host, :port
13
+ attr_reader :host, :port
12
14
 
13
- def initialize(host, port)
14
- @host = host
15
- @port = port
16
- @mutex = Mutex.new
17
- @socket = nil
18
- end
15
+ def initialize(host, port)
16
+ @host = host
17
+ @port = port
18
+ @mutex = Mutex.new
19
+ @socket = nil
20
+ end
19
21
 
20
- def sample?(sample_rate)
21
- sample_rate == 1 || rand < sample_rate
22
- end
22
+ def sample?(sample_rate)
23
+ sample_rate == 1 || rand < sample_rate
24
+ end
23
25
 
24
- def <<(datagram)
25
- with_socket { |socket| socket.send(datagram, 0) > 0 }
26
- self
26
+ def <<(datagram)
27
+ with_socket { |socket| socket.send(datagram, 0) }
28
+ self
27
29
 
28
- rescue ThreadError
29
- # In cases where a TERM or KILL signal has been sent, and we send stats as
30
- # part of a signal handler, locks cannot be acquired, so we do our best
31
- # to try and send the datagram without a lock.
32
- socket.send(datagram, 0) > 0
30
+ rescue ThreadError
31
+ # In cases where a TERM or KILL signal has been sent, and we send stats as
32
+ # part of a signal handler, locks cannot be acquired, so we do our best
33
+ # to try and send the datagram without a lock.
34
+ socket.send(datagram, 0) > 0
33
35
 
34
- rescue SocketError, IOError, SystemCallError
35
- # TODO: log?
36
- invalidate_socket
37
- end
36
+ rescue SocketError, IOError, SystemCallError => error
37
+ StatsD.logger.debug do
38
+ "[StatsD::Instrument::UDPSink] Resetting connection because of #{error.class}: #{error.message}"
39
+ end
40
+ invalidate_socket
41
+ end
38
42
 
39
- def addr
40
- "#{host}:#{port}"
41
- end
43
+ private
42
44
 
43
- private
45
+ def with_socket
46
+ @mutex.synchronize { yield(socket) }
47
+ end
44
48
 
45
- def with_socket
46
- @mutex.synchronize { yield(socket) }
47
- end
48
-
49
- def socket
50
- if @socket.nil?
51
- @socket = UDPSocket.new
52
- @socket.connect(@host, @port)
53
- end
54
- @socket
55
- end
49
+ def socket
50
+ @socket ||= begin
51
+ socket = UDPSocket.new
52
+ socket.connect(@host, @port)
53
+ socket
54
+ end
55
+ end
56
56
 
57
- def invalidate_socket
58
- @mutex.synchronize do
59
- @socket = nil
57
+ def invalidate_socket
58
+ @mutex.synchronize do
59
+ @socket = nil
60
+ end
61
+ end
60
62
  end
61
63
  end
62
64
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module StatsD
4
4
  module Instrument
5
- VERSION = "3.0.0.pre1"
5
+ VERSION = "3.1.0"
6
6
  end
7
7
  end
@@ -16,15 +16,11 @@ Gem::Specification.new do |spec|
16
16
  spec.license = "MIT"
17
17
 
18
18
  spec.files = `git ls-files`.split($/)
19
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
20
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
21
  spec.require_paths = ["lib"]
22
22
 
23
- spec.add_development_dependency 'rake'
24
- spec.add_development_dependency 'minitest'
25
- spec.add_development_dependency 'rspec'
26
- spec.add_development_dependency 'mocha'
27
- spec.add_development_dependency 'yard'
28
- spec.add_development_dependency 'rubocop'
29
- spec.add_development_dependency 'benchmark-ips'
23
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
24
+
25
+ spec.add_development_dependency 'concurrent-ruby'
30
26
  end
@@ -1,16 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'test_helper'
3
+ require "test_helper"
4
4
 
5
5
  class AssertionsTest < Minitest::Test
6
6
  def setup
7
7
  @old_client = StatsD.singleton_client
8
- env = StatsD::Instrument::Environment.new('STATSD_IMPLEMENTATION' => 'datadog')
8
+ env = StatsD::Instrument::Environment.new("STATSD_IMPLEMENTATION" => "datadog")
9
9
  StatsD.singleton_client = StatsD::Instrument::Client.from_env(env)
10
10
 
11
11
  test_class = Class.new(Minitest::Test)
12
12
  test_class.send(:include, StatsD::Instrument::Assertions)
13
- @test_case = test_class.new('fake')
13
+ @test_case = test_class.new("fake")
14
14
  end
15
15
 
16
16
  def teardown
@@ -18,364 +18,374 @@ class AssertionsTest < Minitest::Test
18
18
  end
19
19
 
20
20
  def test_assert_no_statsd_calls
21
- @test_case.assert_no_statsd_calls('counter') do
21
+ @test_case.assert_no_statsd_calls("counter") do
22
22
  # noop
23
23
  end
24
24
 
25
- @test_case.assert_no_statsd_calls('counter') do
26
- StatsD.increment('other')
25
+ @test_case.assert_no_statsd_calls("counter") do
26
+ StatsD.increment("other")
27
27
  end
28
28
 
29
29
  assertion = assert_raises(Minitest::Assertion) do
30
- @test_case.assert_no_statsd_calls('counter') do
31
- StatsD.increment('counter')
30
+ @test_case.assert_no_statsd_calls("counter") do
31
+ StatsD.increment("counter")
32
32
  end
33
33
  end
34
- assert_equal assertion.message, "No StatsD calls for metric counter expected."
34
+ assert_equal(assertion.message, "No StatsD calls for metric counter expected.")
35
35
 
36
- @test_case.assert_no_statsd_calls('counter1', 'counter2') do
36
+ @test_case.assert_no_statsd_calls("counter1", "counter2") do
37
37
  # noop
38
38
  end
39
39
 
40
- @test_case.assert_no_statsd_calls('counter1', 'counter2') do
41
- StatsD.increment('counter')
40
+ @test_case.assert_no_statsd_calls("counter1", "counter2") do
41
+ StatsD.increment("counter")
42
42
  end
43
43
 
44
44
  assertion = assert_raises(Minitest::Assertion) do
45
- @test_case.assert_no_statsd_calls('counter1', 'counter2') do
46
- StatsD.increment('counter0')
47
- StatsD.increment('counter1')
48
- StatsD.increment('counter2')
49
- StatsD.increment('counter3')
45
+ @test_case.assert_no_statsd_calls("counter1", "counter2") do
46
+ StatsD.increment("counter0")
47
+ StatsD.increment("counter1")
48
+ StatsD.increment("counter2")
49
+ StatsD.increment("counter3")
50
50
  end
51
51
  end
52
- assert_equal assertion.message, "No StatsD calls for metric counter1, counter2 expected."
52
+ assert_equal(assertion.message, "No StatsD calls for metric counter1, counter2 expected.")
53
53
 
54
54
  assertion = assert_raises(Minitest::Assertion) do
55
- @test_case.assert_no_statsd_calls('counter0', 'counter1', 'counter2') do
56
- StatsD.increment('counter1')
55
+ @test_case.assert_no_statsd_calls("counter0", "counter1", "counter2") do
56
+ StatsD.increment("counter1")
57
57
  end
58
58
  end
59
- assert_equal assertion.message, "No StatsD calls for metric counter1 expected."
59
+ assert_equal(assertion.message, "No StatsD calls for metric counter1 expected.")
60
60
 
61
61
  assertion = assert_raises(Minitest::Assertion) do
62
62
  @test_case.assert_no_statsd_calls do
63
- StatsD.increment('other')
63
+ StatsD.increment("other")
64
64
  end
65
65
  end
66
- assert_equal assertion.message, "No StatsD calls for metric other expected."
66
+ assert_equal(assertion.message, "No StatsD calls for metric other expected.")
67
67
 
68
68
  assertion = assert_raises(Minitest::Assertion) do
69
69
  @test_case.assert_no_statsd_calls do
70
- StatsD.increment('other')
71
- StatsD.increment('another')
70
+ StatsD.increment("other")
71
+ StatsD.increment("another")
72
72
  end
73
73
  end
74
- assert_equal assertion.message, "No StatsD calls for metric other, another expected."
74
+ assert_equal(assertion.message, "No StatsD calls for metric other, another expected.")
75
75
  end
76
76
 
77
77
  def test_assert_statsd
78
- @test_case.assert_statsd_increment('counter') do
79
- StatsD.increment('counter')
78
+ @test_case.assert_statsd_increment("counter") do
79
+ StatsD.increment("counter")
80
80
  end
81
81
 
82
- @test_case.assert_statsd_increment('counter') do
83
- StatsD.increment('counter')
84
- StatsD.increment('other')
82
+ @test_case.assert_statsd_increment("counter") do
83
+ StatsD.increment("counter")
84
+ StatsD.increment("other")
85
85
  end
86
86
 
87
87
  assert_raises(Minitest::Assertion) do
88
- @test_case.assert_statsd_increment('counter') do
89
- StatsD.increment('other')
88
+ @test_case.assert_statsd_increment("counter") do
89
+ StatsD.increment("other")
90
90
  end
91
91
  end
92
92
 
93
93
  assert_raises(Minitest::Assertion) do
94
- @test_case.assert_statsd_increment('counter') do
95
- StatsD.gauge('counter', 42)
94
+ @test_case.assert_statsd_increment("counter") do
95
+ StatsD.gauge("counter", 42)
96
96
  end
97
97
  end
98
98
 
99
99
  assert_raises(Minitest::Assertion) do
100
- @test_case.assert_statsd_increment('counter') do
101
- StatsD.increment('counter')
102
- StatsD.increment('counter')
100
+ @test_case.assert_statsd_increment("counter") do
101
+ StatsD.increment("counter")
102
+ StatsD.increment("counter")
103
103
  end
104
104
  end
105
105
 
106
- @test_case.assert_statsd_increment('counter', times: 2) do
107
- StatsD.increment('counter')
108
- StatsD.increment('counter')
106
+ @test_case.assert_statsd_increment("counter", times: 2) do
107
+ StatsD.increment("counter")
108
+ StatsD.increment("counter")
109
109
  end
110
110
 
111
- @test_case.assert_statsd_increment('counter', times: 2, tags: ['foo:1']) do
112
- StatsD.increment('counter', tags: { foo: 1 })
113
- StatsD.increment('counter', tags: { foo: 1 })
111
+ @test_case.assert_statsd_increment("counter", times: 2, tags: ["foo:1"]) do
112
+ StatsD.increment("counter", tags: { foo: 1 })
113
+ StatsD.increment("counter", tags: { foo: 1 })
114
114
  end
115
115
 
116
116
  assert_raises(Minitest::Assertion) do
117
- @test_case.assert_statsd_increment('counter', times: 2, tags: ['foo:1']) do
118
- StatsD.increment('counter', tags: { foo: 1 })
119
- StatsD.increment('counter', tags: { foo: 2 })
117
+ @test_case.assert_statsd_increment("counter", times: 2, tags: ["foo:1"]) do
118
+ StatsD.increment("counter", tags: { foo: 1 })
119
+ StatsD.increment("counter", tags: { foo: 2 })
120
120
  end
121
121
  end
122
122
 
123
- @test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: ['a', 'b']) do
124
- StatsD.increment('counter', sample_rate: 0.5, tags: ['a', 'b'])
123
+ @test_case.assert_statsd_increment("counter", sample_rate: 0.5, tags: ["a", "b"]) do
124
+ StatsD.increment("counter", sample_rate: 0.5, tags: ["a", "b"])
125
125
  end
126
126
 
127
127
  assert_raises(Minitest::Assertion) do
128
- @test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: ['a', 'b']) do
129
- StatsD.increment('counter', sample_rate: 0.2, tags: ['c'])
128
+ @test_case.assert_statsd_increment("counter", sample_rate: 0.5, tags: ["a", "b"]) do
129
+ StatsD.increment("counter", sample_rate: 0.2, tags: ["c"])
130
130
  end
131
131
  end
132
132
  end
133
133
 
134
134
  def test_assert_statsd_gauge_call_with_numeric_value
135
- @test_case.assert_statsd_gauge('gauge', value: 42) do
136
- StatsD.gauge('gauge', 42)
135
+ @test_case.assert_statsd_gauge("gauge", 42) do
136
+ StatsD.gauge("gauge", 42)
137
137
  end
138
138
 
139
- @test_case.assert_statsd_gauge('gauge', value: '42') do
140
- StatsD.gauge('gauge', 42)
139
+ @test_case.assert_statsd_gauge("gauge", "42") do
140
+ StatsD.gauge("gauge", 42)
141
141
  end
142
142
 
143
143
  assert_raises(Minitest::Assertion) do
144
- @test_case.assert_statsd_gauge('gauge', value: 42) do
145
- StatsD.gauge('gauge', 45)
144
+ @test_case.assert_statsd_gauge("gauge", 42) do
145
+ StatsD.gauge("gauge", 45)
146
146
  end
147
147
  end
148
+
149
+ @test_case.assert_statsd_gauge("gauge", value: 42) do
150
+ StatsD.gauge("gauge", 42)
151
+ end
148
152
  end
149
153
 
150
154
  def test_assert_statsd_set_call_with_string_value
151
- @test_case.assert_statsd_set('set', value: 12345) do
152
- StatsD.set('set', '12345')
155
+ @test_case.assert_statsd_set("set", 12345) do
156
+ StatsD.set("set", "12345")
153
157
  end
154
158
 
155
- @test_case.assert_statsd_set('set', value: '12345') do
156
- StatsD.set('set', '12345')
159
+ @test_case.assert_statsd_set("set", "12345") do
160
+ StatsD.set("set", "12345")
157
161
  end
158
162
 
159
- @test_case.assert_statsd_set('set', value: 12345) do
160
- StatsD.set('set', 12345)
163
+ @test_case.assert_statsd_set("set", 12345) do
164
+ StatsD.set("set", 12345)
161
165
  end
162
166
 
163
- @test_case.assert_statsd_set('set', value: '12345') do
164
- StatsD.set('set', 12345)
167
+ @test_case.assert_statsd_set("set", "12345") do
168
+ StatsD.set("set", 12345)
165
169
  end
166
170
 
167
171
  assert_raises(Minitest::Assertion) do
168
- @test_case.assert_statsd_set('set', value: '42') do
169
- StatsD.set('set', 45)
172
+ @test_case.assert_statsd_set("set", "42") do
173
+ StatsD.set("set", 45)
170
174
  end
171
175
  end
176
+
177
+ @test_case.assert_statsd_set("set", value: 12345) do
178
+ StatsD.set("set", "12345")
179
+ end
180
+
181
+ @test_case.assert_statsd_set("set", "wrong_value", value: 12345) do
182
+ StatsD.set("set", "12345")
183
+ end
172
184
  end
173
185
 
174
186
  def test_tags_will_match_subsets
175
- @test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: { a: 1 }) do
176
- StatsD.increment('counter', sample_rate: 0.5, tags: { a: 1, b: 2 })
187
+ @test_case.assert_statsd_increment("counter", sample_rate: 0.5, tags: { a: 1 }) do
188
+ StatsD.increment("counter", sample_rate: 0.5, tags: { a: 1, b: 2 })
177
189
  end
178
190
 
179
191
  assert_raises(Minitest::Assertion) do
180
- @test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: { a: 1, b: 3 }) do
181
- StatsD.increment('counter', sample_rate: 0.5, tags: { a: 1, b: 2, c: 4 })
192
+ @test_case.assert_statsd_increment("counter", sample_rate: 0.5, tags: { a: 1, b: 3 }) do
193
+ StatsD.increment("counter", sample_rate: 0.5, tags: { a: 1, b: 2, c: 4 })
182
194
  end
183
195
  end
184
196
  end
185
197
 
186
198
  def test_tags_friendly_error
187
199
  assertion = assert_raises(Minitest::Assertion) do
188
- @test_case.assert_statsd_increment('counter', tags: { class: "AnotherJob" }) do
189
- StatsD.increment('counter', tags: { class: "MyJob" })
200
+ @test_case.assert_statsd_increment("counter", tags: { class: "AnotherJob" }) do
201
+ StatsD.increment("counter", tags: { class: "MyJob" })
190
202
  end
191
203
  end
192
204
 
193
- assert_includes assertion.message, "Captured metrics with the same key"
194
- assert_includes assertion.message, "MyJob"
205
+ assert_includes(assertion.message, "Captured metrics with the same key")
206
+ assert_includes(assertion.message, "MyJob")
195
207
  end
196
208
 
197
209
  def test_capture_and_assert
198
210
  datagrams = @test_case.capture_statsd_datagrams do
199
- StatsD.increment('counter', tags: { foo: 1 })
200
- StatsD.increment('counter', tags: { foo: 2 })
211
+ StatsD.increment("counter", tags: { foo: 1 })
212
+ StatsD.increment("counter", tags: { foo: 2 })
201
213
  end
202
214
 
203
- @test_case.assert_statsd_increment('counter', tags: ['foo:1'], datagrams: datagrams)
204
- @test_case.assert_statsd_increment('counter', tags: ['foo:2'], datagrams: datagrams)
215
+ @test_case.assert_statsd_increment("counter", tags: ["foo:1"], datagrams: datagrams)
216
+ @test_case.assert_statsd_increment("counter", tags: ["foo:2"], datagrams: datagrams)
205
217
  end
206
218
 
207
219
  def test_capture_from_different_client
208
220
  client = StatsD::Instrument::Client.new
209
- @test_case.assert_statsd_increment('foo', client: client) do
210
- client.increment('foo')
221
+ @test_case.assert_statsd_increment("foo", client: client) do
222
+ client.increment("foo")
211
223
  end
212
224
 
213
225
  assert_raises(Minitest::Assertion) do
214
- @test_case.assert_statsd_increment('foo', client: client) do
215
- StatsD.increment('foo')
226
+ @test_case.assert_statsd_increment("foo", client: client) do
227
+ StatsD.increment("foo")
216
228
  end
217
229
  end
218
230
 
219
231
  assert_raises(Minitest::Assertion) do
220
- @test_case.assert_statsd_increment('foo') do
221
- client.increment('foo')
232
+ @test_case.assert_statsd_increment("foo") do
233
+ client.increment("foo")
222
234
  end
223
235
  end
224
236
  end
225
237
 
226
238
  def test_multiple_expectations_are_not_order_dependent
227
- foo_1_metric = StatsD::Instrument::Expectation.increment('counter', tags: ['foo:1'])
228
- foo_2_metric = StatsD::Instrument::Expectation.increment('counter', tags: ['foo:2'])
239
+ foo_1_metric = StatsD::Instrument::Expectation.increment("counter", tags: ["foo:1"])
240
+ foo_2_metric = StatsD::Instrument::Expectation.increment("counter", tags: ["foo:2"])
229
241
  @test_case.assert_statsd_expectations([foo_1_metric, foo_2_metric]) do
230
- StatsD.increment('counter', tags: { foo: 1 })
231
- StatsD.increment('counter', tags: { foo: 2 })
242
+ StatsD.increment("counter", tags: { foo: 1 })
243
+ StatsD.increment("counter", tags: { foo: 2 })
232
244
  end
233
245
 
234
- foo_1_metric = StatsD::Instrument::Expectation.increment('counter', tags: ['foo:1'])
235
- foo_2_metric = StatsD::Instrument::Expectation.increment('counter', tags: ['foo:2'])
246
+ foo_1_metric = StatsD::Instrument::Expectation.increment("counter", tags: ["foo:1"])
247
+ foo_2_metric = StatsD::Instrument::Expectation.increment("counter", tags: ["foo:2"])
236
248
  @test_case.assert_statsd_expectations([foo_2_metric, foo_1_metric]) do
237
- StatsD.increment('counter', tags: { foo: 1 })
238
- StatsD.increment('counter', tags: { foo: 2 })
249
+ StatsD.increment("counter", tags: { foo: 1 })
250
+ StatsD.increment("counter", tags: { foo: 2 })
239
251
  end
240
252
 
241
- foo_1_metric = StatsD::Instrument::Expectation.increment('counter', times: 2, tags: ['foo:1'])
242
- foo_2_metric = StatsD::Instrument::Expectation.increment('counter', tags: ['foo:2'])
253
+ foo_1_metric = StatsD::Instrument::Expectation.increment("counter", times: 2, tags: ["foo:1"])
254
+ foo_2_metric = StatsD::Instrument::Expectation.increment("counter", tags: ["foo:2"])
243
255
  @test_case.assert_statsd_expectations([foo_1_metric, foo_2_metric]) do
244
- StatsD.increment('counter', tags: { foo: 1 })
245
- StatsD.increment('counter', tags: { foo: 1 })
246
- StatsD.increment('counter', tags: { foo: 2 })
256
+ StatsD.increment("counter", tags: { foo: 1 })
257
+ StatsD.increment("counter", tags: { foo: 1 })
258
+ StatsD.increment("counter", tags: { foo: 2 })
247
259
  end
248
260
 
249
- foo_1_metric = StatsD::Instrument::Expectation.increment('counter', times: 2, tags: ['foo:1'])
250
- foo_2_metric = StatsD::Instrument::Expectation.increment('counter', tags: ['foo:2'])
261
+ foo_1_metric = StatsD::Instrument::Expectation.increment("counter", times: 2, tags: ["foo:1"])
262
+ foo_2_metric = StatsD::Instrument::Expectation.increment("counter", tags: ["foo:2"])
251
263
  @test_case.assert_statsd_expectations([foo_2_metric, foo_1_metric]) do
252
- StatsD.increment('counter', tags: { foo: 1 })
253
- StatsD.increment('counter', tags: { foo: 1 })
254
- StatsD.increment('counter', tags: { foo: 2 })
264
+ StatsD.increment("counter", tags: { foo: 1 })
265
+ StatsD.increment("counter", tags: { foo: 1 })
266
+ StatsD.increment("counter", tags: { foo: 2 })
255
267
  end
256
268
 
257
- foo_1_metric = StatsD::Instrument::Expectation.increment('counter', times: 2, tags: ['foo:1'])
258
- foo_2_metric = StatsD::Instrument::Expectation.increment('counter', tags: ['foo:2'])
269
+ foo_1_metric = StatsD::Instrument::Expectation.increment("counter", times: 2, tags: ["foo:1"])
270
+ foo_2_metric = StatsD::Instrument::Expectation.increment("counter", tags: ["foo:2"])
259
271
  @test_case.assert_statsd_expectations([foo_2_metric, foo_1_metric]) do
260
- StatsD.increment('counter', tags: { foo: 1 })
261
- StatsD.increment('counter', tags: { foo: 2 })
262
- StatsD.increment('counter', tags: { foo: 1 })
272
+ StatsD.increment("counter", tags: { foo: 1 })
273
+ StatsD.increment("counter", tags: { foo: 2 })
274
+ StatsD.increment("counter", tags: { foo: 1 })
263
275
  end
264
276
  end
265
277
 
266
278
  def test_assert_multiple_statsd_expectations
267
279
  assert_raises(Minitest::Assertion) do
268
- foo_1_metric = StatsD::Instrument::Expectation.increment('counter', times: 2, tags: ['foo:1'])
269
- foo_2_metric = StatsD::Instrument::Expectation.increment('counter', tags: ['foo:2'])
280
+ foo_1_metric = StatsD::Instrument::Expectation.increment("counter", times: 2, tags: ["foo:1"])
281
+ foo_2_metric = StatsD::Instrument::Expectation.increment("counter", tags: ["foo:2"])
270
282
  @test_case.assert_statsd_expectations([foo_1_metric, foo_2_metric]) do
271
- StatsD.increment('counter', tags: { foo: 1 })
272
- StatsD.increment('counter', tags: { foo: 2 })
283
+ StatsD.increment("counter", tags: { foo: 1 })
284
+ StatsD.increment("counter", tags: { foo: 2 })
273
285
  end
274
286
  end
275
287
 
276
288
  assert_raises(Minitest::Assertion) do
277
- foo_1_metric = StatsD::Instrument::Expectation.increment('counter', times: 2, tags: ['foo:1'])
278
- foo_2_metric = StatsD::Instrument::Expectation.increment('counter', tags: ['foo:2'])
289
+ foo_1_metric = StatsD::Instrument::Expectation.increment("counter", times: 2, tags: ["foo:1"])
290
+ foo_2_metric = StatsD::Instrument::Expectation.increment("counter", tags: ["foo:2"])
279
291
  @test_case.assert_statsd_expectations([foo_1_metric, foo_2_metric]) do
280
- StatsD.increment('counter', tags: { foo: 1 })
281
- StatsD.increment('counter', tags: { foo: 1 })
282
- StatsD.increment('counter', tags: { foo: 2 })
283
- StatsD.increment('counter', tags: { foo: 2 })
292
+ StatsD.increment("counter", tags: { foo: 1 })
293
+ StatsD.increment("counter", tags: { foo: 1 })
294
+ StatsD.increment("counter", tags: { foo: 2 })
295
+ StatsD.increment("counter", tags: { foo: 2 })
284
296
  end
285
297
  end
286
298
 
287
- foo_1_metric = StatsD::Instrument::Expectation.increment('counter', times: 2, tags: ['foo:1'])
288
- foo_2_metric = StatsD::Instrument::Expectation.increment('counter', tags: ['foo:2'])
299
+ foo_1_metric = StatsD::Instrument::Expectation.increment("counter", times: 2, tags: ["foo:1"])
300
+ foo_2_metric = StatsD::Instrument::Expectation.increment("counter", 1, tags: ["foo:2"])
289
301
  @test_case.assert_statsd_expectations([foo_1_metric, foo_2_metric]) do
290
- StatsD.increment('counter', tags: { foo: 1 })
291
- StatsD.increment('counter', tags: { foo: 1 })
292
- StatsD.increment('counter', tags: { foo: 2 })
302
+ StatsD.increment("counter", tags: { foo: 1 })
303
+ StatsD.increment("counter", tags: { foo: 1 })
304
+ StatsD.increment("counter", tags: { foo: 2 })
293
305
  end
294
306
  end
295
307
 
296
308
  def test_assert_statsd_increment_with_tags
297
- @test_case.assert_statsd_increment('counter', tags: ['a:b', 'c:d']) do
298
- StatsD.increment('counter', tags: { a: 'b', c: 'd' })
309
+ @test_case.assert_statsd_increment("counter", tags: ["a:b", "c:d"]) do
310
+ StatsD.increment("counter", tags: { a: "b", c: "d" })
299
311
  end
300
312
 
301
- @test_case.assert_statsd_increment('counter', tags: { a: 'b', c: 'd' }) do
302
- StatsD.increment('counter', tags: ['a:b', 'c:d'])
313
+ @test_case.assert_statsd_increment("counter", tags: { a: "b", c: "d" }) do
314
+ StatsD.increment("counter", tags: ["a:b", "c:d"])
303
315
  end
304
316
  end
305
317
 
306
318
  def test_nested_assertions
307
- @test_case.assert_statsd_increment('counter1') do
308
- @test_case.assert_statsd_increment('counter2') do
309
- StatsD.increment('counter1')
310
- StatsD.increment('counter2')
319
+ @test_case.assert_statsd_increment("counter1") do
320
+ @test_case.assert_statsd_increment("counter2") do
321
+ StatsD.increment("counter1")
322
+ StatsD.increment("counter2")
311
323
  end
312
324
  end
313
325
 
314
- @test_case.assert_statsd_increment('counter1') do
315
- StatsD.increment('counter1')
316
- @test_case.assert_statsd_increment('counter2') do
317
- StatsD.increment('counter2')
326
+ @test_case.assert_statsd_increment("counter1") do
327
+ StatsD.increment("counter1")
328
+ @test_case.assert_statsd_increment("counter2") do
329
+ StatsD.increment("counter2")
318
330
  end
319
331
  end
320
332
 
321
333
  assert_raises(Minitest::Assertion) do
322
- @test_case.assert_statsd_increment('counter1') do
323
- @test_case.assert_statsd_increment('counter2') do
324
- StatsD.increment('counter2')
334
+ @test_case.assert_statsd_increment("counter1") do
335
+ @test_case.assert_statsd_increment("counter2") do
336
+ StatsD.increment("counter2")
325
337
  end
326
338
  end
327
339
  end
328
340
 
329
341
  assert_raises(Minitest::Assertion) do
330
- @test_case.assert_statsd_increment('counter1') do
331
- @test_case.assert_statsd_increment('counter2') do
332
- StatsD.increment('counter1')
342
+ @test_case.assert_statsd_increment("counter1") do
343
+ @test_case.assert_statsd_increment("counter2") do
344
+ StatsD.increment("counter1")
333
345
  end
334
- StatsD.increment('counter2')
346
+ StatsD.increment("counter2")
335
347
  end
336
348
  end
337
349
  end
338
350
 
339
351
  def test_assertion_block_with_expected_exceptions
340
- @test_case.assert_statsd_increment('expected_happened') do
352
+ @test_case.assert_statsd_increment("expected_happened") do
341
353
  @test_case.assert_raises(RuntimeError) do
342
- begin
343
- raise "expected"
344
- rescue
345
- StatsD.increment('expected_happened')
346
- raise
347
- end
354
+ raise "expected"
355
+ rescue
356
+ StatsD.increment("expected_happened")
357
+ raise
348
358
  end
349
359
  end
350
360
 
351
361
  assertion = assert_raises(Minitest::Assertion) do
352
- @test_case.assert_statsd_increment('counter') do
362
+ @test_case.assert_statsd_increment("counter") do
353
363
  @test_case.assert_raises(RuntimeError) do
354
364
  raise "expected"
355
365
  end
356
366
  end
357
367
  end
358
- assert_includes assertion.message, "No StatsD calls for metric counter of type c were made"
368
+ assert_includes(assertion.message, "No StatsD calls for metric counter of type c were made")
359
369
  end
360
370
 
361
371
  def test_assertion_block_with_unexpected_exceptions
362
372
  assertion = assert_raises(Minitest::Assertion) do
363
- @test_case.assert_statsd_increment('counter') do
364
- StatsD.increment('counter')
373
+ @test_case.assert_statsd_increment("counter") do
374
+ StatsD.increment("counter")
365
375
  raise "unexpected"
366
376
  end
367
377
  end
368
- assert_includes assertion.message, "An exception occurred in the block provided to the StatsD assertion"
378
+ assert_includes(assertion.message, "An exception occurred in the block provided to the StatsD assertion")
369
379
 
370
380
  assertion = assert_raises(Minitest::Assertion) do
371
381
  @test_case.assert_raises(RuntimeError) do
372
- @test_case.assert_statsd_increment('counter') do
373
- StatsD.increment('counter')
382
+ @test_case.assert_statsd_increment("counter") do
383
+ StatsD.increment("counter")
374
384
  raise "unexpected"
375
385
  end
376
386
  end
377
387
  end
378
- assert_includes assertion.message, "An exception occurred in the block provided to the StatsD assertion"
388
+ assert_includes(assertion.message, "An exception occurred in the block provided to the StatsD assertion")
379
389
 
380
390
  assertion = assert_raises(Minitest::Assertion) do
381
391
  @test_case.assert_raises(RuntimeError) do
@@ -384,16 +394,50 @@ class AssertionsTest < Minitest::Test
384
394
  end
385
395
  end
386
396
  end
387
- assert_includes assertion.message, "An exception occurred in the block provided to the StatsD assertion"
397
+ assert_includes(assertion.message, "An exception occurred in the block provided to the StatsD assertion")
388
398
  end
389
399
 
390
400
  def test_assertion_block_with_other_assertion_failures
391
401
  # If another assertion failure happens inside the block, that failure should have priority
392
402
  assertion = assert_raises(Minitest::Assertion) do
393
- @test_case.assert_statsd_increment('counter') do
394
- @test_case.flunk('other assertion failure')
403
+ @test_case.assert_statsd_increment("counter") do
404
+ @test_case.flunk("other assertion failure")
395
405
  end
396
406
  end
397
- assert_equal "other assertion failure", assertion.message
407
+ assert_equal("other assertion failure", assertion.message)
408
+ end
409
+
410
+ def test_assert_when_using_no_prefix
411
+ env = StatsD::Instrument::Environment.new("STATSD_PREFIX" => nil)
412
+ StatsD.singleton_client = StatsD::Instrument::Client.from_env(env)
413
+
414
+ @test_case.assert_statsd_increment("incr", no_prefix: false) do
415
+ StatsD.increment("incr")
416
+ end
417
+
418
+ @test_case.assert_statsd_increment("incr", no_prefix: true) do
419
+ StatsD.increment("incr")
420
+ end
421
+
422
+ env = StatsD::Instrument::Environment.new("STATSD_PREFIX" => "prefix")
423
+ StatsD.singleton_client = StatsD::Instrument::Client.from_env(env)
424
+
425
+ @test_case.assert_statsd_increment("incr", no_prefix: false) do
426
+ StatsD.increment("incr")
427
+ end
428
+
429
+ assert_raises(Minitest::Assertion) do
430
+ @test_case.assert_statsd_increment("incr", no_prefix: true) do
431
+ StatsD.increment("incr")
432
+ end
433
+ end
434
+
435
+ @test_case.assert_statsd_increment("prefix.incr", no_prefix: true) do
436
+ StatsD.increment("incr")
437
+ end
438
+
439
+ @test_case.assert_statsd_increment("incr", no_prefix: true) do
440
+ StatsD.increment("incr", no_prefix: true)
441
+ end
398
442
  end
399
443
  end