statsd-instrument 3.0.1 → 3.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/lint.yml +22 -0
  3. data/.github/workflows/{ci.yml → tests.yml} +3 -21
  4. data/.rubocop.yml +2 -1
  5. data/CHANGELOG.md +18 -0
  6. data/Gemfile +8 -10
  7. data/README.md +7 -4
  8. data/Rakefile +6 -6
  9. data/benchmark/send-metrics-to-dev-null-log +14 -14
  10. data/benchmark/send-metrics-to-local-udp-receiver +18 -18
  11. data/lib/statsd/instrument/assertions.rb +7 -7
  12. data/lib/statsd/instrument/batched_udp_sink.rb +159 -0
  13. data/lib/statsd/instrument/client.rb +3 -3
  14. data/lib/statsd/instrument/datagram.rb +1 -1
  15. data/lib/statsd/instrument/datagram_builder.rb +10 -22
  16. data/lib/statsd/instrument/dogstatsd_datagram_builder.rb +2 -2
  17. data/lib/statsd/instrument/environment.rb +19 -11
  18. data/lib/statsd/instrument/expectation.rb +6 -18
  19. data/lib/statsd/instrument/matchers.rb +8 -4
  20. data/lib/statsd/instrument/railtie.rb +1 -1
  21. data/lib/statsd/instrument/rubocop/measure_as_dist_argument.rb +1 -1
  22. data/lib/statsd/instrument/rubocop/metaprogramming_positional_arguments.rb +2 -2
  23. data/lib/statsd/instrument/rubocop/metric_prefix_argument.rb +1 -1
  24. data/lib/statsd/instrument/rubocop/metric_return_value.rb +3 -3
  25. data/lib/statsd/instrument/rubocop/metric_value_keyword_argument.rb +1 -1
  26. data/lib/statsd/instrument/rubocop/positional_arguments.rb +4 -4
  27. data/lib/statsd/instrument/rubocop/singleton_configuration.rb +1 -1
  28. data/lib/statsd/instrument/rubocop/splat_arguments.rb +2 -2
  29. data/lib/statsd/instrument/rubocop.rb +13 -34
  30. data/lib/statsd/instrument/strict.rb +1 -1
  31. data/lib/statsd/instrument/udp_sink.rb +11 -13
  32. data/lib/statsd/instrument/version.rb +1 -1
  33. data/lib/statsd/instrument.rb +56 -59
  34. data/lib/statsd-instrument.rb +1 -1
  35. data/statsd-instrument.gemspec +2 -0
  36. data/test/assertions_test.rb +200 -155
  37. data/test/benchmark/clock_gettime.rb +1 -1
  38. data/test/benchmark/metrics.rb +8 -8
  39. data/test/benchmark/tags.rb +4 -4
  40. data/test/capture_sink_test.rb +11 -11
  41. data/test/client_test.rb +64 -64
  42. data/test/datagram_builder_test.rb +41 -41
  43. data/test/datagram_test.rb +5 -5
  44. data/test/dogstatsd_datagram_builder_test.rb +22 -22
  45. data/test/environment_test.rb +26 -17
  46. data/test/helpers/rubocop_helper.rb +3 -3
  47. data/test/helpers_test.rb +12 -12
  48. data/test/integration_test.rb +6 -6
  49. data/test/log_sink_test.rb +2 -2
  50. data/test/matchers_test.rb +46 -46
  51. data/test/null_sink_test.rb +2 -2
  52. data/test/rubocop/measure_as_dist_argument_test.rb +2 -2
  53. data/test/rubocop/metaprogramming_positional_arguments_test.rb +2 -2
  54. data/test/rubocop/metric_prefix_argument_test.rb +2 -2
  55. data/test/rubocop/metric_return_value_test.rb +3 -3
  56. data/test/rubocop/metric_value_keyword_argument_test.rb +3 -3
  57. data/test/rubocop/positional_arguments_test.rb +2 -2
  58. data/test/rubocop/singleton_configuration_test.rb +8 -8
  59. data/test/rubocop/splat_arguments_test.rb +2 -2
  60. data/test/statsd_datagram_builder_test.rb +6 -6
  61. data/test/statsd_instrumentation_test.rb +104 -104
  62. data/test/statsd_test.rb +35 -35
  63. data/test/test_helper.rb +13 -6
  64. data/test/udp_sink_test.rb +142 -44
  65. metadata +21 -7
  66. data/test/benchmark/default_tags.rb +0 -47
@@ -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
@@ -6,7 +6,7 @@ module StatsD
6
6
  # to become the new default in the next major release of this library.
7
7
  class UDPSink
8
8
  def self.for_addr(addr)
9
- host, port_as_string = addr.split(':', 2)
9
+ host, port_as_string = addr.split(":", 2)
10
10
  new(host, Integer(port_as_string))
11
11
  end
12
12
 
@@ -20,11 +20,11 @@ module StatsD
20
20
  end
21
21
 
22
22
  def sample?(sample_rate)
23
- sample_rate == 1 || rand < sample_rate
23
+ sample_rate == 1.0 || rand < sample_rate
24
24
  end
25
25
 
26
26
  def <<(datagram)
27
- with_socket { |socket| socket.send(datagram, 0) > 0 }
27
+ with_socket { |socket| socket.send(datagram, 0) }
28
28
  self
29
29
 
30
30
  rescue ThreadError
@@ -33,15 +33,13 @@ module StatsD
33
33
  # to try and send the datagram without a lock.
34
34
  socket.send(datagram, 0) > 0
35
35
 
36
- rescue SocketError, IOError, SystemCallError
37
- # TODO: log?
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
38
40
  invalidate_socket
39
41
  end
40
42
 
41
- def addr
42
- "#{host}:#{port}"
43
- end
44
-
45
43
  private
46
44
 
47
45
  def with_socket
@@ -49,11 +47,11 @@ module StatsD
49
47
  end
50
48
 
51
49
  def socket
52
- if @socket.nil?
53
- @socket = UDPSocket.new
54
- @socket.connect(@host, @port)
50
+ @socket ||= begin
51
+ socket = UDPSocket.new
52
+ socket.connect(@host, @port)
53
+ socket
55
54
  end
56
- @socket
57
55
  end
58
56
 
59
57
  def invalidate_socket
@@ -2,6 +2,6 @@
2
2
 
3
3
  module StatsD
4
4
  module Instrument
5
- VERSION = "3.0.1"
5
+ VERSION = "3.1.2"
6
6
  end
7
7
  end
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'socket'
4
- require 'logger'
5
- require 'forwardable'
3
+ require "socket"
4
+ require "logger"
5
+ require "forwardable"
6
6
 
7
7
  # The `StatsD` module contains low-level metrics for collecting metrics and
8
8
  # sending them to the backend.
@@ -31,7 +31,7 @@ module StatsD
31
31
  # @private
32
32
  # @return [String]
33
33
  def self.generate_metric_name(name, callee, *args)
34
- name.respond_to?(:call) ? name.call(callee, args).gsub('::', '.') : name.gsub('::', '.')
34
+ name.respond_to?(:call) ? name.call(callee, args).gsub("::", ".") : name.gsub("::", ".")
35
35
  end
36
36
 
37
37
  # Even though this method is considered private, and is no longer used internally,
@@ -113,26 +113,24 @@ module StatsD
113
113
  def statsd_count_success(method, name, sample_rate: nil, tags: nil, no_prefix: false, client: nil)
114
114
  add_to_method(method, name, :count_success) do
115
115
  define_method(method) do |*args, &block|
116
- begin
117
- truthiness = result = super(*args, &block)
118
- rescue
119
- truthiness = false
120
- raise
121
- else
122
- if block_given?
123
- begin
124
- truthiness = yield(result)
125
- rescue
126
- truthiness = false
127
- end
116
+ truthiness = result = super(*args, &block)
117
+ rescue
118
+ truthiness = false
119
+ raise
120
+ else
121
+ if block_given?
122
+ begin
123
+ truthiness = yield(result)
124
+ rescue
125
+ truthiness = false
128
126
  end
129
- result
130
- ensure
131
- client ||= StatsD.singleton_client
132
- suffix = truthiness == false ? 'failure' : 'success'
133
- key = StatsD::Instrument.generate_metric_name(name, self, *args)
134
- client.increment("#{key}.#{suffix}", sample_rate: sample_rate, tags: tags, no_prefix: no_prefix)
135
127
  end
128
+ result
129
+ ensure
130
+ client ||= StatsD.singleton_client
131
+ suffix = truthiness == false ? "failure" : "success"
132
+ key = StatsD::Instrument.generate_metric_name(name, self, *args)
133
+ client.increment("#{key}.#{suffix}", sample_rate: sample_rate, tags: tags, no_prefix: no_prefix)
136
134
  end
137
135
  end
138
136
  end
@@ -152,27 +150,25 @@ module StatsD
152
150
  def statsd_count_if(method, name, sample_rate: nil, tags: nil, no_prefix: false, client: nil)
153
151
  add_to_method(method, name, :count_if) do
154
152
  define_method(method) do |*args, &block|
155
- begin
156
- truthiness = result = super(*args, &block)
157
- rescue
158
- truthiness = false
159
- raise
160
- else
161
- if block_given?
162
- begin
163
- truthiness = yield(result)
164
- rescue
165
- truthiness = false
166
- end
167
- end
168
- result
169
- ensure
170
- if truthiness
171
- client ||= StatsD.singleton_client
172
- key = StatsD::Instrument.generate_metric_name(name, self, *args)
173
- client.increment(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix)
153
+ truthiness = result = super(*args, &block)
154
+ rescue
155
+ truthiness = false
156
+ raise
157
+ else
158
+ if block_given?
159
+ begin
160
+ truthiness = yield(result)
161
+ rescue
162
+ truthiness = false
174
163
  end
175
164
  end
165
+ result
166
+ ensure
167
+ if truthiness
168
+ client ||= StatsD.singleton_client
169
+ key = StatsD::Instrument.generate_metric_name(name, self, *args)
170
+ client.increment(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix)
171
+ end
176
172
  end
177
173
  end
178
174
  end
@@ -347,21 +343,22 @@ module StatsD
347
343
  end
348
344
  end
349
345
 
350
- require 'statsd/instrument/version'
351
- require 'statsd/instrument/client'
352
- require 'statsd/instrument/datagram'
353
- require 'statsd/instrument/dogstatsd_datagram'
354
- require 'statsd/instrument/datagram_builder'
355
- require 'statsd/instrument/statsd_datagram_builder'
356
- require 'statsd/instrument/dogstatsd_datagram_builder'
357
- require 'statsd/instrument/null_sink'
358
- require 'statsd/instrument/udp_sink'
359
- require 'statsd/instrument/capture_sink'
360
- require 'statsd/instrument/log_sink'
361
- require 'statsd/instrument/environment'
362
- require 'statsd/instrument/helpers'
363
- require 'statsd/instrument/assertions'
364
- require 'statsd/instrument/expectation'
365
- require 'statsd/instrument/matchers' if defined?(::RSpec)
366
- require 'statsd/instrument/railtie' if defined?(::Rails::Railtie)
367
- require 'statsd/instrument/strict' if ENV['STATSD_STRICT_MODE']
346
+ require "statsd/instrument/version"
347
+ require "statsd/instrument/client"
348
+ require "statsd/instrument/datagram"
349
+ require "statsd/instrument/dogstatsd_datagram"
350
+ require "statsd/instrument/datagram_builder"
351
+ require "statsd/instrument/statsd_datagram_builder"
352
+ require "statsd/instrument/dogstatsd_datagram_builder"
353
+ require "statsd/instrument/null_sink"
354
+ require "statsd/instrument/udp_sink"
355
+ require "statsd/instrument/batched_udp_sink"
356
+ require "statsd/instrument/capture_sink"
357
+ require "statsd/instrument/log_sink"
358
+ require "statsd/instrument/environment"
359
+ require "statsd/instrument/helpers"
360
+ require "statsd/instrument/assertions"
361
+ require "statsd/instrument/expectation"
362
+ require "statsd/instrument/matchers" if defined?(::RSpec)
363
+ require "statsd/instrument/railtie" if defined?(::Rails::Railtie)
364
+ require "statsd/instrument/strict" if ENV["STATSD_STRICT_MODE"]
@@ -1,3 +1,3 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'statsd/instrument'
3
+ require "statsd/instrument"
@@ -21,4 +21,6 @@ Gem::Specification.new do |spec|
21
21
  spec.require_paths = ["lib"]
22
22
 
23
23
  spec.metadata['allowed_push_host'] = "https://rubygems.org"
24
+
25
+ spec.add_development_dependency 'concurrent-ruby'
24
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,187 +18,187 @@ 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
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
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
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
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
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
 
@@ -208,160 +208,158 @@ class AssertionsTest < Minitest::Test
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
@@ -372,8 +370,8 @@ class AssertionsTest < Minitest::Test
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
@@ -381,8 +379,8 @@ class AssertionsTest < Minitest::Test
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
@@ -402,10 +400,57 @@ class AssertionsTest < Minitest::Test
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
407
  assert_equal("other assertion failure", assertion.message)
410
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
442
+ end
443
+
444
+ def test_client_propagation_to_expectations
445
+ foo_1_metric = StatsD::Instrument::Expectation.increment("foo")
446
+ @test_case.assert_statsd_expectations([foo_1_metric]) do
447
+ StatsD.increment("foo")
448
+ end
449
+
450
+ client = StatsD::Instrument::Client.new(prefix: "prefix")
451
+ foo_2_metric = StatsD::Instrument::Expectation.increment("foo", client: client)
452
+ @test_case.assert_statsd_expectations([foo_2_metric]) do
453
+ StatsD.increment("prefix.foo")
454
+ end
455
+ end
411
456
  end