statsd-instrument 1.7.2 → 2.0.0beta

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,126 @@
1
+ require 'test_helper'
2
+
3
+ class AssertionsTest < Minitest::Test
4
+
5
+ def setup
6
+ test_class = Class.new(Minitest::Test)
7
+ test_class.send(:include, StatsD::Instrument::Assertions)
8
+ @test_case = test_class.new('fake')
9
+ end
10
+
11
+ def test_capture_metrics_inside_block_only
12
+ StatsD.increment('counter')
13
+ metrics = @test_case.capture_statsd_calls do
14
+ StatsD.increment('counter')
15
+ StatsD.gauge('gauge', 12)
16
+ end
17
+ StatsD.gauge('gauge', 15)
18
+
19
+ assert_equal 2, metrics.length
20
+ assert_equal 'counter', metrics[0].name
21
+ assert_equal 'gauge', metrics[1].name
22
+ assert_equal 12, metrics[1].value
23
+ end
24
+
25
+ def test_assert_no_statsd_calls
26
+ assert_no_assertion_triggered do
27
+ @test_case.assert_no_statsd_calls('counter') do
28
+ # noop
29
+ end
30
+ end
31
+
32
+ assert_no_assertion_triggered do
33
+ @test_case.assert_no_statsd_calls('counter') do
34
+ StatsD.increment('other')
35
+ end
36
+ end
37
+
38
+ assert_assertion_triggered do
39
+ @test_case.assert_no_statsd_calls('counter') do
40
+ StatsD.increment('counter')
41
+ end
42
+ end
43
+
44
+ assert_assertion_triggered do
45
+ @test_case.assert_no_statsd_calls do
46
+ StatsD.increment('other')
47
+ end
48
+ end
49
+ end
50
+
51
+ def test_assert_statsd_call
52
+ assert_no_assertion_triggered do
53
+ @test_case.assert_statsd_increment('counter') do
54
+ StatsD.increment('counter')
55
+ end
56
+ end
57
+
58
+ assert_no_assertion_triggered do
59
+ @test_case.assert_statsd_increment('counter') do
60
+ StatsD.increment('counter')
61
+ StatsD.increment('other')
62
+ end
63
+ end
64
+
65
+ assert_assertion_triggered do
66
+ @test_case.assert_statsd_increment('counter') do
67
+ StatsD.increment('other')
68
+ end
69
+ end
70
+
71
+ assert_assertion_triggered do
72
+ @test_case.assert_statsd_increment('counter') do
73
+ StatsD.gauge('counter', 42)
74
+ end
75
+ end
76
+
77
+ assert_assertion_triggered do
78
+ @test_case.assert_statsd_increment('counter') do
79
+ StatsD.increment('counter')
80
+ StatsD.increment('counter')
81
+ end
82
+ end
83
+
84
+ assert_no_assertion_triggered do
85
+ @test_case.assert_statsd_increment('counter', times: 2) do
86
+ StatsD.increment('counter')
87
+ StatsD.increment('counter')
88
+ end
89
+ end
90
+
91
+ assert_no_assertion_triggered do
92
+ @test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: ['a', 'b']) do
93
+ StatsD.increment('counter', sample_rate: 0.5, tags: ['a', 'b'])
94
+ end
95
+ end
96
+
97
+ assert_assertion_triggered do
98
+ @test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: ['a', 'b']) do
99
+ StatsD.increment('counter', sample_rate: 0.2, tags: ['c'])
100
+ end
101
+ end
102
+ end
103
+
104
+ private
105
+
106
+ def assert_no_assertion_triggered(&block)
107
+ block.call
108
+ rescue MiniTest::Assertion => assertion
109
+ flunk "No assertion trigger expected, but one was triggered with message #{assertion.message}."
110
+ else
111
+ pass
112
+ end
113
+
114
+ def assert_assertion_triggered(message = nil, &block)
115
+ block.call
116
+ rescue MiniTest::Assertion => assertion
117
+ if message
118
+ assert_equal message, assertion.message, "Assertion triggered, but message was not what was expected."
119
+ else
120
+ pass
121
+ end
122
+ assertion
123
+ else
124
+ flunk "No assertion was triggered, but one was expected."
125
+ end
126
+ end
@@ -0,0 +1,24 @@
1
+ require 'test_helper'
2
+
3
+ class CaptureBackendTest < Minitest::Test
4
+ def setup
5
+ @backend = StatsD::Instrument::Backends::CaptureBackend.new
6
+ @metric1 = StatsD::Instrument::Metric::new(type: :c, name: 'mock.counter')
7
+ @metric2 = StatsD::Instrument::Metric::new(type: :ms, name: 'mock.measure', value: 123)
8
+ end
9
+
10
+ def test_collecting_metric
11
+ assert @backend.collected_metrics.empty?
12
+ @backend.collect_metric(@metric1)
13
+ @backend.collect_metric(@metric2)
14
+ assert_equal [@metric1, @metric2], @backend.collected_metrics
15
+ end
16
+
17
+ def test_reset
18
+ @backend.collect_metric(@metric1)
19
+ @backend.reset
20
+ assert @backend.collected_metrics.empty?
21
+ @backend.collect_metric(@metric2)
22
+ assert_equal [@metric2], @backend.collected_metrics
23
+ end
24
+ end
@@ -0,0 +1,35 @@
1
+ require 'test_helper'
2
+
3
+ class EnvironmentTest < Minitest::Test
4
+
5
+ def setup
6
+ ENV['STATSD_ADDR'] = nil
7
+ ENV['IMPLEMENTATION'] = nil
8
+ end
9
+
10
+ def test_uses_logger_in_development_environment
11
+ StatsD::Instrument::Environment.stubs(:environment).returns('development')
12
+ assert_instance_of StatsD::Instrument::Backends::LoggerBackend, StatsD::Instrument::Environment.default_backend
13
+ end
14
+
15
+ def test_uses_null_backend_in_test_environment
16
+ StatsD::Instrument::Environment.stubs(:environment).returns('test')
17
+ assert_instance_of StatsD::Instrument::Backends::NullBackend, StatsD::Instrument::Environment.default_backend
18
+ end
19
+
20
+ def test_uses_udp_backend_in_production_environment
21
+ StatsD::Instrument::Environment.stubs(:environment).returns('production')
22
+ assert_instance_of StatsD::Instrument::Backends::UDPBackend, StatsD::Instrument::Environment.default_backend
23
+ end
24
+
25
+ def test_uses_environment_variables_in_production_environment
26
+ StatsD::Instrument::Environment.stubs(:environment).returns('production')
27
+ ENV['STATSD_ADDR'] = '127.0.0.1:1234'
28
+ ENV['STATSD_IMPLEMENTATION'] = 'datadog'
29
+
30
+ backend = StatsD::Instrument::Environment.default_backend
31
+ assert_equal '127.0.0.1', backend.host
32
+ assert_equal 1234, backend.port
33
+ assert_equal :datadog, backend.implementation
34
+ end
35
+ end
@@ -0,0 +1,20 @@
1
+ require 'test_helper'
2
+
3
+ class IntegrationTest < Minitest::Test
4
+
5
+ def setup
6
+ @old_backend, StatsD.backend = StatsD.backend, StatsD::Instrument::Backends::UDPBackend.new("localhost:31798")
7
+ end
8
+
9
+ def teardown
10
+ StatsD.backend = @old_backend
11
+ end
12
+
13
+ def test_live_local_udp_socket
14
+ server = UDPSocket.new
15
+ server.bind('localhost', 31798)
16
+
17
+ StatsD.increment('counter')
18
+ assert_equal "counter:1|c", server.recvfrom(100).first
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ require 'test_helper'
2
+
3
+ class LoggerBackendTest < Minitest::Test
4
+ def setup
5
+ logger = Logger.new(@io = StringIO.new)
6
+ logger.formatter = lambda { |_,_,_, msg| "#{msg}\n" }
7
+ @backend = StatsD::Instrument::Backends::LoggerBackend.new(logger)
8
+ @metric1 = StatsD::Instrument::Metric::new(type: :c, name: 'mock.counter', tags: { a: 'b', c: 'd'})
9
+ @metric2 = StatsD::Instrument::Metric::new(type: :ms, name: 'mock.measure', value: 123, sample_rate: 0.3)
10
+ end
11
+
12
+ def test_logs_metrics
13
+ @backend.collect_metric(@metric1)
14
+ assert_equal @io.string, "[StatsD] increment mock.counter:1 #a:b #c:d\n"
15
+ @io.string = ""
16
+
17
+ @backend.collect_metric(@metric2)
18
+ assert_equal @io.string, "[StatsD] measure mock.measure:123 @0.3\n"
19
+ end
20
+ end
@@ -0,0 +1,38 @@
1
+ require 'test_helper'
2
+
3
+ class MetricTest < Minitest::Test
4
+
5
+ def test_required_arguments
6
+ assert_raises(ArgumentError) { StatsD::Instrument::Metric.new(type: :c) }
7
+ assert_raises(ArgumentError) { StatsD::Instrument::Metric.new(name: 'test') }
8
+ assert_raises(ArgumentError) { StatsD::Instrument::Metric.new(type: :ms, name: 'test') }
9
+ end
10
+
11
+ def test_default_values
12
+ m = StatsD::Instrument::Metric.new(type: :c, name: 'counter')
13
+ assert_equal 1, m.value
14
+ assert_equal StatsD.default_sample_rate, m.sample_rate
15
+ assert m.tags.nil?
16
+ end
17
+
18
+ def test_name_prefix
19
+ StatsD.stubs(:prefix).returns('prefix')
20
+ m = StatsD::Instrument::Metric.new(type: :c, name: 'counter')
21
+ assert_equal 'prefix.counter', m.name
22
+
23
+ m = StatsD::Instrument::Metric.new(type: :c, name: 'counter', no_prefix: true)
24
+ assert_equal 'counter', m.name
25
+ end
26
+
27
+ def test_rewrite_shitty_tags
28
+ assert_equal ['igno_red'], StatsD::Instrument::Metric.normalize_tags(['igno,red'])
29
+ assert_equal ['igno_red'], StatsD::Instrument::Metric.normalize_tags(['igno red'])
30
+ assert_equal ['test:test_test'], StatsD::Instrument::Metric.normalize_tags(['test:test:test'])
31
+ assert_equal ['topic:foo_foo', 'bar_'], StatsD::Instrument::Metric.normalize_tags(['topic:foo : foo', 'bar '])
32
+ end
33
+
34
+ def test_rewrite_tags_provided_as_hash
35
+ assert_equal ['tag:value'], StatsD::Instrument::Metric.normalize_tags(:tag => 'value')
36
+ assert_equal ['tag:value', 'tag2:value2'], StatsD::Instrument::Metric.normalize_tags(:tag => 'value', :tag2 => 'value2')
37
+ end
38
+ end
@@ -47,13 +47,16 @@ end
47
47
 
48
48
  ActiveMerchant::Base.extend StatsD::Instrument
49
49
 
50
- class StatsDInstrumentationTest < Test::Unit::TestCase
50
+ class StatsDInstrumentationTest < Minitest::Test
51
+ include StatsD::Instrument::Assertions
52
+
51
53
  def test_statsd_count_if
52
54
  ActiveMerchant::Gateway.statsd_count_if :ssl_post, 'ActiveMerchant.Gateway.if'
53
55
 
54
- StatsD.expects(:increment).with('ActiveMerchant.Gateway.if').once
55
- ActiveMerchant::Gateway.new.purchase(true)
56
- ActiveMerchant::Gateway.new.purchase(false)
56
+ assert_statsd_increment('ActiveMerchant.Gateway.if') do
57
+ ActiveMerchant::Gateway.new.purchase(true)
58
+ ActiveMerchant::Gateway.new.purchase(false)
59
+ end
57
60
 
58
61
  ActiveMerchant::Gateway.statsd_remove_count_if :ssl_post, 'ActiveMerchant.Gateway.if'
59
62
  end
@@ -63,9 +66,10 @@ class StatsDInstrumentationTest < Test::Unit::TestCase
63
66
  result == 'true'
64
67
  end
65
68
 
66
- StatsD.expects(:collect).with(:c, 'ActiveMerchant.Base.post_with_block', 1, {}).once
67
- assert_equal 'true', ActiveMerchant::Base.new.post_with_block { 'true' }
68
- assert_equal 'false', ActiveMerchant::Base.new.post_with_block { 'false' }
69
+ assert_statsd_increment('ActiveMerchant.Base.post_with_block') do
70
+ assert_equal 'true', ActiveMerchant::Base.new.post_with_block { 'true' }
71
+ assert_equal 'false', ActiveMerchant::Base.new.post_with_block { 'false' }
72
+ end
69
73
 
70
74
  ActiveMerchant::Base.statsd_remove_count_if :post_with_block, 'ActiveMerchant.Base.post_with_block'
71
75
  end
@@ -75,9 +79,10 @@ class StatsDInstrumentationTest < Test::Unit::TestCase
75
79
  result[:success]
76
80
  end
77
81
 
78
- StatsD.expects(:increment).with('ActiveMerchant.Gateway.block').once
79
- ActiveMerchant::UniqueGateway.new.purchase(true)
80
- ActiveMerchant::UniqueGateway.new.purchase(false)
82
+ assert_statsd_increment('ActiveMerchant.Gateway.block', times: 1) do
83
+ ActiveMerchant::UniqueGateway.new.purchase(true)
84
+ ActiveMerchant::UniqueGateway.new.purchase(false)
85
+ end
81
86
 
82
87
  ActiveMerchant::UniqueGateway.statsd_remove_count_if :ssl_post, 'ActiveMerchant.Gateway.block'
83
88
  end
@@ -85,11 +90,15 @@ class StatsDInstrumentationTest < Test::Unit::TestCase
85
90
  def test_statsd_count_success
86
91
  ActiveMerchant::Gateway.statsd_count_success :ssl_post, 'ActiveMerchant.Gateway', 0.5
87
92
 
88
- StatsD.expects(:increment).with('ActiveMerchant.Gateway.success', 1, 0.5).once
89
- StatsD.expects(:increment).with('ActiveMerchant.Gateway.failure', 1, 0.5).once
93
+ assert_statsd_increment('ActiveMerchant.Gateway.success', sample_rate: 0.5, times: 1) do
94
+ ActiveMerchant::Gateway.new.purchase(true)
95
+ ActiveMerchant::Gateway.new.purchase(false)
96
+ end
90
97
 
91
- ActiveMerchant::Gateway.new.purchase(true)
92
- ActiveMerchant::Gateway.new.purchase(false)
98
+ assert_statsd_increment('ActiveMerchant.Gateway.failure', sample_rate: 0.5, times: 1) do
99
+ ActiveMerchant::Gateway.new.purchase(false)
100
+ ActiveMerchant::Gateway.new.purchase(true)
101
+ end
93
102
 
94
103
  ActiveMerchant::Gateway.statsd_remove_count_success :ssl_post, 'ActiveMerchant.Gateway'
95
104
  end
@@ -99,11 +108,15 @@ class StatsDInstrumentationTest < Test::Unit::TestCase
99
108
  result == 'successful'
100
109
  end
101
110
 
102
- StatsD.expects(:collect).with(:c, 'ActiveMerchant.Base.post_with_block.success', 1, {}).once
103
- StatsD.expects(:collect).with(:c, 'ActiveMerchant.Base.post_with_block.failure', 1, {}).once
104
-
105
- assert_equal 'successful', ActiveMerchant::Base.new.post_with_block { 'successful' }
106
- assert_equal 'not so successful', ActiveMerchant::Base.new.post_with_block { 'not so successful' }
111
+ assert_statsd_increment('ActiveMerchant.Base.post_with_block.success', times: 1) do
112
+ assert_equal 'successful', ActiveMerchant::Base.new.post_with_block { 'successful' }
113
+ assert_equal 'not so successful', ActiveMerchant::Base.new.post_with_block { 'not so successful' }
114
+ end
115
+
116
+ assert_statsd_increment('ActiveMerchant.Base.post_with_block.failure', times: 1) do
117
+ assert_equal 'successful', ActiveMerchant::Base.new.post_with_block { 'successful' }
118
+ assert_equal 'not so successful', ActiveMerchant::Base.new.post_with_block { 'not so successful' }
119
+ end
107
120
 
108
121
  ActiveMerchant::Base.statsd_remove_count_success :post_with_block, 'ActiveMerchant.Base.post_with_block'
109
122
  end
@@ -113,11 +126,13 @@ class StatsDInstrumentationTest < Test::Unit::TestCase
113
126
  result[:success]
114
127
  end
115
128
 
116
- StatsD.expects(:increment).with('ActiveMerchant.Gateway.success', 1)
117
- ActiveMerchant::UniqueGateway.new.purchase(true)
129
+ assert_statsd_increment('ActiveMerchant.Gateway.success') do
130
+ ActiveMerchant::UniqueGateway.new.purchase(true)
131
+ end
118
132
 
119
- StatsD.expects(:increment).with('ActiveMerchant.Gateway.failure', 1)
120
- ActiveMerchant::UniqueGateway.new.purchase(false)
133
+ assert_statsd_increment('ActiveMerchant.Gateway.failure') do
134
+ ActiveMerchant::UniqueGateway.new.purchase(false)
135
+ end
121
136
 
122
137
  ActiveMerchant::UniqueGateway.statsd_remove_count_success :ssl_post, 'ActiveMerchant.Gateway'
123
138
  end
@@ -125,8 +140,9 @@ class StatsDInstrumentationTest < Test::Unit::TestCase
125
140
  def test_statsd_count
126
141
  ActiveMerchant::Gateway.statsd_count :ssl_post, 'ActiveMerchant.Gateway.ssl_post'
127
142
 
128
- StatsD.expects(:increment).with('ActiveMerchant.Gateway.ssl_post', 1)
129
- ActiveMerchant::Gateway.new.purchase(true)
143
+ assert_statsd_increment('ActiveMerchant.Gateway.ssl_post') do
144
+ ActiveMerchant::Gateway.new.purchase(true)
145
+ end
130
146
 
131
147
  ActiveMerchant::Gateway.statsd_remove_count :ssl_post, 'ActiveMerchant.Gateway.ssl_post'
132
148
  end
@@ -135,8 +151,9 @@ class StatsDInstrumentationTest < Test::Unit::TestCase
135
151
  metric_namer = lambda { |object, args| object.class.to_s.downcase + ".insert." + args.first.to_s }
136
152
  ActiveMerchant::Gateway.statsd_count(:ssl_post, metric_namer)
137
153
 
138
- StatsD.expects(:increment).with('gatewaysubclass.insert.true', 1)
139
- GatewaySubClass.new.purchase(true)
154
+ assert_statsd_increment('gatewaysubclass.insert.true') do
155
+ GatewaySubClass.new.purchase(true)
156
+ end
140
157
 
141
158
  ActiveMerchant::Gateway.statsd_remove_count(:ssl_post, metric_namer)
142
159
  end
@@ -144,35 +161,39 @@ class StatsDInstrumentationTest < Test::Unit::TestCase
144
161
  def test_statsd_count_with_method_receiving_block
145
162
  ActiveMerchant::Base.statsd_count :post_with_block, 'ActiveMerchant.Base.post_with_block'
146
163
 
147
- StatsD.expects(:collect).with(:c, 'ActiveMerchant.Base.post_with_block', 1, {})
148
- assert_equal 'block called', ActiveMerchant::Base.new.post_with_block { 'block called' }
164
+ assert_statsd_increment('ActiveMerchant.Base.post_with_block') do
165
+ assert_equal 'block called', ActiveMerchant::Base.new.post_with_block { 'block called' }
166
+ end
149
167
 
150
168
  ActiveMerchant::Base.statsd_remove_count :post_with_block, 'ActiveMerchant.Base.post_with_block'
151
169
  end
152
170
 
153
- def test_statsd_measure_with_nested_modules
154
- ActiveMerchant::UniqueGateway.statsd_measure :ssl_post, 'ActiveMerchant::Gateway.ssl_post'
171
+ def test_statsd_measure
172
+ ActiveMerchant::UniqueGateway.statsd_measure :ssl_post, 'ActiveMerchant.Gateway.ssl_post', sample_rate: 0.3
155
173
 
156
- StatsD.expects(:measure).with('ActiveMerchant.Gateway.ssl_post', nil)
157
- ActiveMerchant::UniqueGateway.new.purchase(true)
174
+ assert_statsd_measure('ActiveMerchant.Gateway.ssl_post', sample_rate: 0.3) do
175
+ ActiveMerchant::UniqueGateway.new.purchase(true)
176
+ end
158
177
 
159
- ActiveMerchant::UniqueGateway.statsd_remove_measure :ssl_post, 'ActiveMerchant::Gateway.ssl_post'
178
+ ActiveMerchant::UniqueGateway.statsd_remove_measure :ssl_post, 'ActiveMerchant.Gateway.ssl_post'
160
179
  end
161
180
 
162
- def test_statsd_measure
163
- ActiveMerchant::UniqueGateway.statsd_measure :ssl_post, 'ActiveMerchant.Gateway.ssl_post', 0.3
181
+ def test_statsd_measure_uses_normalized_metric_name
182
+ ActiveMerchant::UniqueGateway.statsd_measure :ssl_post, 'ActiveMerchant::Gateway.ssl_post'
164
183
 
165
- StatsD.expects(:measure).with('ActiveMerchant.Gateway.ssl_post', nil, 0.3)
166
- ActiveMerchant::UniqueGateway.new.purchase(true)
184
+ assert_statsd_measure('ActiveMerchant.Gateway.ssl_post') do
185
+ ActiveMerchant::UniqueGateway.new.purchase(true)
186
+ end
167
187
 
168
- ActiveMerchant::UniqueGateway.statsd_remove_measure :ssl_post, 'ActiveMerchant.Gateway.ssl_post'
188
+ ActiveMerchant::UniqueGateway.statsd_remove_measure :ssl_post, 'ActiveMerchant::Gateway.ssl_post'
169
189
  end
170
190
 
171
191
  def test_statsd_measure_with_method_receiving_block
172
192
  ActiveMerchant::Base.statsd_measure :post_with_block, 'ActiveMerchant.Base.post_with_block'
173
193
 
174
- StatsD.expects(:collect).with(:ms, 'ActiveMerchant.Base.post_with_block', is_a(Float), {})
175
- assert_equal 'block called', ActiveMerchant::Base.new.post_with_block { 'block called' }
194
+ assert_statsd_measure('ActiveMerchant.Base.post_with_block') do
195
+ assert_equal 'block called', ActiveMerchant::Base.new.post_with_block { 'block called' }
196
+ end
176
197
 
177
198
  ActiveMerchant::Base.statsd_remove_measure :post_with_block, 'ActiveMerchant.Base.post_with_block'
178
199
  end
@@ -181,18 +202,20 @@ class StatsDInstrumentationTest < Test::Unit::TestCase
181
202
  ActiveMerchant::Gateway.singleton_class.extend StatsD::Instrument
182
203
  ActiveMerchant::Gateway.singleton_class.statsd_count :sync, 'ActiveMerchant.Gateway.sync'
183
204
 
184
- StatsD.expects(:increment).with('ActiveMerchant.Gateway.sync', 1)
185
- ActiveMerchant::Gateway.sync
205
+ assert_statsd_increment('ActiveMerchant.Gateway.sync') do
206
+ ActiveMerchant::Gateway.sync
207
+ end
186
208
 
187
209
  ActiveMerchant::Gateway.singleton_class.statsd_remove_count :sync, 'ActiveMerchant.Gateway.sync'
188
210
  end
189
211
 
190
212
  def test_statsd_count_with_tags
191
213
  ActiveMerchant::Gateway.singleton_class.extend StatsD::Instrument
192
- ActiveMerchant::Gateway.singleton_class.statsd_count :sync, 'ActiveMerchant.Gateway.sync', :tags => ['t']
214
+ ActiveMerchant::Gateway.singleton_class.statsd_count :sync, 'ActiveMerchant.Gateway.sync', tags: { key: 'value' }
193
215
 
194
- StatsD.expects(:increment).with('ActiveMerchant.Gateway.sync', 1, :tags => ['t'])
195
- ActiveMerchant::Gateway.sync
216
+ assert_statsd_increment('ActiveMerchant.Gateway.sync', tags: ['key:value']) do
217
+ ActiveMerchant::Gateway.sync
218
+ end
196
219
 
197
220
  ActiveMerchant::Gateway.singleton_class.statsd_remove_count :sync, 'ActiveMerchant.Gateway.sync'
198
221
  end