statsd-instrument 3.0.0.pre2 → 3.1.1

Sign up to get free protection for your applications and to get access to all the features.
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 +43 -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 +159 -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 +199 -167
  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 +147 -49
  74. metadata +12 -92
  75. data/.github/workflows/ci.yml +0 -49
  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.pre2"
5
+ VERSION = "3.1.1"
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,376 +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', 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', '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', 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
148
 
149
- @test_case.assert_statsd_gauge('gauge', value: 42) do
150
- StatsD.gauge('gauge', 42)
149
+ @test_case.assert_statsd_gauge("gauge", value: 42) do
150
+ StatsD.gauge("gauge", 42)
151
151
  end
152
152
  end
153
153
 
154
154
  def test_assert_statsd_set_call_with_string_value
155
- @test_case.assert_statsd_set('set', 12345) do
156
- StatsD.set('set', '12345')
155
+ @test_case.assert_statsd_set("set", 12345) do
156
+ StatsD.set("set", "12345")
157
157
  end
158
158
 
159
- @test_case.assert_statsd_set('set', '12345') do
160
- StatsD.set('set', '12345')
159
+ @test_case.assert_statsd_set("set", "12345") do
160
+ StatsD.set("set", "12345")
161
161
  end
162
162
 
163
- @test_case.assert_statsd_set('set', 12345) do
164
- StatsD.set('set', 12345)
163
+ @test_case.assert_statsd_set("set", 12345) do
164
+ StatsD.set("set", 12345)
165
165
  end
166
166
 
167
- @test_case.assert_statsd_set('set', '12345') do
168
- StatsD.set('set', 12345)
167
+ @test_case.assert_statsd_set("set", "12345") do
168
+ StatsD.set("set", 12345)
169
169
  end
170
170
 
171
171
  assert_raises(Minitest::Assertion) do
172
- @test_case.assert_statsd_set('set', '42') do
173
- StatsD.set('set', 45)
172
+ @test_case.assert_statsd_set("set", "42") do
173
+ StatsD.set("set", 45)
174
174
  end
175
175
  end
176
176
 
177
- @test_case.assert_statsd_set('set', value: 12345) do
178
- StatsD.set('set', '12345')
177
+ @test_case.assert_statsd_set("set", value: 12345) do
178
+ StatsD.set("set", "12345")
179
179
  end
180
180
 
181
- @test_case.assert_statsd_set('set', 'wrong_value', value: 12345) do
182
- StatsD.set('set', '12345')
181
+ @test_case.assert_statsd_set("set", "wrong_value", value: 12345) do
182
+ StatsD.set("set", "12345")
183
183
  end
184
184
  end
185
185
 
186
186
  def test_tags_will_match_subsets
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 })
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 })
189
189
  end
190
190
 
191
191
  assert_raises(Minitest::Assertion) do
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 })
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 })
194
194
  end
195
195
  end
196
196
  end
197
197
 
198
198
  def test_tags_friendly_error
199
199
  assertion = assert_raises(Minitest::Assertion) do
200
- @test_case.assert_statsd_increment('counter', tags: { class: "AnotherJob" }) do
201
- StatsD.increment('counter', tags: { class: "MyJob" })
200
+ @test_case.assert_statsd_increment("counter", tags: { class: "AnotherJob" }) do
201
+ StatsD.increment("counter", tags: { class: "MyJob" })
202
202
  end
203
203
  end
204
204
 
205
- assert_includes assertion.message, "Captured metrics with the same key"
206
- assert_includes assertion.message, "MyJob"
205
+ assert_includes(assertion.message, "Captured metrics with the same key")
206
+ assert_includes(assertion.message, "MyJob")
207
207
  end
208
208
 
209
209
  def test_capture_and_assert
210
210
  datagrams = @test_case.capture_statsd_datagrams do
211
- StatsD.increment('counter', tags: { foo: 1 })
212
- StatsD.increment('counter', tags: { foo: 2 })
211
+ StatsD.increment("counter", tags: { foo: 1 })
212
+ StatsD.increment("counter", tags: { foo: 2 })
213
213
  end
214
214
 
215
- @test_case.assert_statsd_increment('counter', tags: ['foo:1'], datagrams: datagrams)
216
- @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)
217
217
  end
218
218
 
219
219
  def test_capture_from_different_client
220
220
  client = StatsD::Instrument::Client.new
221
- @test_case.assert_statsd_increment('foo', client: client) do
222
- client.increment('foo')
221
+ @test_case.assert_statsd_increment("foo", client: client) do
222
+ client.increment("foo")
223
223
  end
224
224
 
225
225
  assert_raises(Minitest::Assertion) do
226
- @test_case.assert_statsd_increment('foo', client: client) do
227
- StatsD.increment('foo')
226
+ @test_case.assert_statsd_increment("foo", client: client) do
227
+ StatsD.increment("foo")
228
228
  end
229
229
  end
230
230
 
231
231
  assert_raises(Minitest::Assertion) do
232
- @test_case.assert_statsd_increment('foo') do
233
- client.increment('foo')
232
+ @test_case.assert_statsd_increment("foo") do
233
+ client.increment("foo")
234
234
  end
235
235
  end
236
236
  end
237
237
 
238
238
  def test_multiple_expectations_are_not_order_dependent
239
- foo_1_metric = StatsD::Instrument::Expectation.increment('counter', tags: ['foo:1'])
240
- 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"])
241
241
  @test_case.assert_statsd_expectations([foo_1_metric, foo_2_metric]) do
242
- StatsD.increment('counter', tags: { foo: 1 })
243
- StatsD.increment('counter', tags: { foo: 2 })
242
+ StatsD.increment("counter", tags: { foo: 1 })
243
+ StatsD.increment("counter", tags: { foo: 2 })
244
244
  end
245
245
 
246
- foo_1_metric = StatsD::Instrument::Expectation.increment('counter', tags: ['foo:1'])
247
- 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"])
248
248
  @test_case.assert_statsd_expectations([foo_2_metric, foo_1_metric]) do
249
- StatsD.increment('counter', tags: { foo: 1 })
250
- StatsD.increment('counter', tags: { foo: 2 })
249
+ StatsD.increment("counter", tags: { foo: 1 })
250
+ StatsD.increment("counter", tags: { foo: 2 })
251
251
  end
252
252
 
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'])
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"])
255
255
  @test_case.assert_statsd_expectations([foo_1_metric, foo_2_metric]) do
256
- StatsD.increment('counter', tags: { foo: 1 })
257
- StatsD.increment('counter', tags: { foo: 1 })
258
- 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 })
259
259
  end
260
260
 
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'])
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"])
263
263
  @test_case.assert_statsd_expectations([foo_2_metric, foo_1_metric]) do
264
- StatsD.increment('counter', tags: { foo: 1 })
265
- StatsD.increment('counter', tags: { foo: 1 })
266
- 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 })
267
267
  end
268
268
 
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'])
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"])
271
271
  @test_case.assert_statsd_expectations([foo_2_metric, foo_1_metric]) do
272
- StatsD.increment('counter', tags: { foo: 1 })
273
- StatsD.increment('counter', tags: { foo: 2 })
274
- 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 })
275
275
  end
276
276
  end
277
277
 
278
278
  def test_assert_multiple_statsd_expectations
279
279
  assert_raises(Minitest::Assertion) do
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'])
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"])
282
282
  @test_case.assert_statsd_expectations([foo_1_metric, foo_2_metric]) do
283
- StatsD.increment('counter', tags: { foo: 1 })
284
- StatsD.increment('counter', tags: { foo: 2 })
283
+ StatsD.increment("counter", tags: { foo: 1 })
284
+ StatsD.increment("counter", tags: { foo: 2 })
285
285
  end
286
286
  end
287
287
 
288
288
  assert_raises(Minitest::Assertion) do
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'])
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"])
291
291
  @test_case.assert_statsd_expectations([foo_1_metric, foo_2_metric]) do
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 })
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 })
296
296
  end
297
297
  end
298
298
 
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'])
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"])
301
301
  @test_case.assert_statsd_expectations([foo_1_metric, foo_2_metric]) do
302
- StatsD.increment('counter', tags: { foo: 1 })
303
- StatsD.increment('counter', tags: { foo: 1 })
304
- 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 })
305
305
  end
306
306
  end
307
307
 
308
308
  def test_assert_statsd_increment_with_tags
309
- @test_case.assert_statsd_increment('counter', tags: ['a:b', 'c:d']) do
310
- 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" })
311
311
  end
312
312
 
313
- @test_case.assert_statsd_increment('counter', tags: { a: 'b', c: 'd' }) do
314
- 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"])
315
315
  end
316
316
  end
317
317
 
318
318
  def test_nested_assertions
319
- @test_case.assert_statsd_increment('counter1') do
320
- @test_case.assert_statsd_increment('counter2') do
321
- StatsD.increment('counter1')
322
- 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")
323
323
  end
324
324
  end
325
325
 
326
- @test_case.assert_statsd_increment('counter1') do
327
- StatsD.increment('counter1')
328
- @test_case.assert_statsd_increment('counter2') do
329
- 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")
330
330
  end
331
331
  end
332
332
 
333
333
  assert_raises(Minitest::Assertion) do
334
- @test_case.assert_statsd_increment('counter1') do
335
- @test_case.assert_statsd_increment('counter2') do
336
- StatsD.increment('counter2')
334
+ @test_case.assert_statsd_increment("counter1") do
335
+ @test_case.assert_statsd_increment("counter2") do
336
+ StatsD.increment("counter2")
337
337
  end
338
338
  end
339
339
  end
340
340
 
341
341
  assert_raises(Minitest::Assertion) do
342
- @test_case.assert_statsd_increment('counter1') do
343
- @test_case.assert_statsd_increment('counter2') do
344
- StatsD.increment('counter1')
342
+ @test_case.assert_statsd_increment("counter1") do
343
+ @test_case.assert_statsd_increment("counter2") do
344
+ StatsD.increment("counter1")
345
345
  end
346
- StatsD.increment('counter2')
346
+ StatsD.increment("counter2")
347
347
  end
348
348
  end
349
349
  end
350
350
 
351
351
  def test_assertion_block_with_expected_exceptions
352
- @test_case.assert_statsd_increment('expected_happened') do
352
+ @test_case.assert_statsd_increment("expected_happened") do
353
353
  @test_case.assert_raises(RuntimeError) do
354
- begin
355
- raise "expected"
356
- rescue
357
- StatsD.increment('expected_happened')
358
- raise
359
- end
354
+ raise "expected"
355
+ rescue
356
+ StatsD.increment("expected_happened")
357
+ raise
360
358
  end
361
359
  end
362
360
 
363
361
  assertion = assert_raises(Minitest::Assertion) do
364
- @test_case.assert_statsd_increment('counter') do
362
+ @test_case.assert_statsd_increment("counter") do
365
363
  @test_case.assert_raises(RuntimeError) do
366
364
  raise "expected"
367
365
  end
368
366
  end
369
367
  end
370
- 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")
371
369
  end
372
370
 
373
371
  def test_assertion_block_with_unexpected_exceptions
374
372
  assertion = assert_raises(Minitest::Assertion) do
375
- @test_case.assert_statsd_increment('counter') do
376
- StatsD.increment('counter')
373
+ @test_case.assert_statsd_increment("counter") do
374
+ StatsD.increment("counter")
377
375
  raise "unexpected"
378
376
  end
379
377
  end
380
- 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")
381
379
 
382
380
  assertion = assert_raises(Minitest::Assertion) do
383
381
  @test_case.assert_raises(RuntimeError) do
384
- @test_case.assert_statsd_increment('counter') do
385
- StatsD.increment('counter')
382
+ @test_case.assert_statsd_increment("counter") do
383
+ StatsD.increment("counter")
386
384
  raise "unexpected"
387
385
  end
388
386
  end
389
387
  end
390
- 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")
391
389
 
392
390
  assertion = assert_raises(Minitest::Assertion) do
393
391
  @test_case.assert_raises(RuntimeError) do
@@ -396,16 +394,50 @@ class AssertionsTest < Minitest::Test
396
394
  end
397
395
  end
398
396
  end
399
- 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")
400
398
  end
401
399
 
402
400
  def test_assertion_block_with_other_assertion_failures
403
401
  # If another assertion failure happens inside the block, that failure should have priority
404
402
  assertion = assert_raises(Minitest::Assertion) do
405
- @test_case.assert_statsd_increment('counter') do
406
- @test_case.flunk('other assertion failure')
403
+ @test_case.assert_statsd_increment("counter") do
404
+ @test_case.flunk("other assertion failure")
407
405
  end
408
406
  end
409
- 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
410
442
  end
411
443
  end