statsd-instrument 2.3.0.beta3 → 2.3.0.beta4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4fed77a70470567d6ed49c57a482ca7e7f89dc93
4
- data.tar.gz: 007f5373e64aa4a4ff2b082430fe4a8f1f65a10d
3
+ metadata.gz: 3e554f96249614fda4598c73f8a374eb17d53aac
4
+ data.tar.gz: 0d2b774c6545501caf5a4e8a2c254a68fea40239
5
5
  SHA512:
6
- metadata.gz: 6f2adb3365c37f6fb8a5abcea906b5f50e6f8cc7be751216680a1dcae0a453ddf39c36ee69066bccb995345b2102f7f68fdbd53284887a3d1d7f32ad46aa5133
7
- data.tar.gz: 74948e391f080f3706bfb394f120f7eb3d9f9eabe410667257545426711ecef178162f666cd9f97a87365a32e382742a0bd48d0a79459461f4590757c8f7f0fd
6
+ metadata.gz: 35310f0da894755a60b5517d8d555169805eb2e27c789c79d8a36fe8f87a0e7e14183814f6c81d4734338f4384ed329c6a9041df21f573b95e9c1d1e60ec9f55
7
+ data.tar.gz: 4c146f7e47bd0b181c3844e126fa8b0760ad6f6dd91f979cf83be422ac58e89bc9cb9e43744aa5a6096e592d5a5b43ed56688bbbf921bd5b80fba06f2dc9029a
@@ -72,7 +72,7 @@ module StatsD
72
72
  end
73
73
  end
74
74
 
75
- # Adds execution duration instrumentation to a method.
75
+ # Adds execution duration instrumentation to a method as a timing.
76
76
  #
77
77
  # @param method [Symbol] The name of the method to instrument.
78
78
  # @param name [String, #call] The name of the metric to use. You can also pass in a
@@ -87,6 +87,22 @@ module StatsD
87
87
  end
88
88
  end
89
89
 
90
+ # Adds execution duration instrumentation to a method as a distribution.
91
+ #
92
+ # @param method [Symbol] The name of the method to instrument.
93
+ # @param name [String, #call] The name of the metric to use. You can also pass in a
94
+ # callable to dynamically generate a metric name
95
+ # @param metric_options (see StatsD#measure)
96
+ # @return [void]
97
+ # @note Supported by the datadog implementation only (in beta)
98
+ def statsd_distribution(method, name, *metric_options)
99
+ add_to_method(method, name, :distribution) do
100
+ define_method(method) do |*args, &block|
101
+ StatsD.distribution(StatsD::Instrument.generate_metric_name(name, self, *args), *metric_options) { super(*args, &block) }
102
+ end
103
+ end
104
+ end
105
+
90
106
  # Adds success and failure counter instrumentation to a method.
91
107
  #
92
108
  # A method call will be considered successful if it does not raise an exception, and the result is true-y.
@@ -206,6 +222,15 @@ module StatsD
206
222
  remove_from_method(method, name, :measure)
207
223
  end
208
224
 
225
+ # Removes StatsD distribution instrumentation from a method
226
+ # @param method (see #statsd_remove_count)
227
+ # @param name (see #statsd_remove_count)
228
+ # @return [void]
229
+ # @see #statsd_measure
230
+ def statsd_remove_distribution(method, name)
231
+ remove_from_method(method, name, :distribution)
232
+ end
233
+
209
234
  private
210
235
 
211
236
  def statsd_instrumentation_for(method, name, action)
@@ -282,17 +307,13 @@ module StatsD
282
307
  # HTTP.get(url)
283
308
  # end
284
309
  def measure(key, value = nil, *metric_options, &block)
285
- if value.is_a?(Hash) && metric_options.empty?
286
- metric_options = [value]
287
- value = value.fetch(:value, nil)
288
- end
310
+ value, metric_options = parse_options(value, metric_options)
289
311
  type = (!metric_options.empty? && metric_options.first[:as_dist] ? :d : :ms)
290
312
 
291
313
  result = nil
292
314
  value = 1000 * StatsD::Instrument.duration { result = block.call } if block_given?
293
- metric = collect_metric(hash_argument(metric_options).merge(type: type, name: key, value: value))
294
- result = metric unless block_given?
295
- result
315
+ metric = collect_metric(type, key, value, metric_options)
316
+ (result || metric)
296
317
  end
297
318
 
298
319
  # Emits a counter metric.
@@ -307,12 +328,7 @@ module StatsD
307
328
  # @param metric_options [Hash] (default: {}) Metric options
308
329
  # @return (see #collect_metric)
309
330
  def increment(key, value = 1, *metric_options)
310
- if value.is_a?(Hash) && metric_options.empty?
311
- metric_options = [value]
312
- value = value.fetch(:value, 1)
313
- end
314
-
315
- collect_metric(hash_argument(metric_options).merge(type: :c, name: key, value: value))
331
+ collect_metric(:c, key, value, metric_options)
316
332
  end
317
333
 
318
334
  # Emits a gauge metric.
@@ -321,12 +337,7 @@ module StatsD
321
337
  # @param metric_options [Hash] (default: {}) Metric options
322
338
  # @return (see #collect_metric)
323
339
  def gauge(key, value, *metric_options)
324
- if value.is_a?(Hash) && metric_options.empty?
325
- metric_options = [value]
326
- value = value.fetch(:value, nil)
327
- end
328
-
329
- collect_metric(hash_argument(metric_options).merge(type: :g, name: key, value: value))
340
+ collect_metric(:g, key, value, metric_options)
330
341
  end
331
342
 
332
343
  # Emits a histogram metric.
@@ -336,12 +347,7 @@ module StatsD
336
347
  # @return (see #collect_metric)
337
348
  # @note Supported by the datadog implementation only.
338
349
  def histogram(key, value, *metric_options)
339
- if value.is_a?(Hash) && metric_options.empty?
340
- metric_options = [value]
341
- value = value.fetch(:value, nil)
342
- end
343
-
344
- collect_metric(hash_argument(metric_options).merge(type: :h, name: key, value: value))
350
+ collect_metric(:h, key, value, metric_options)
345
351
  end
346
352
 
347
353
  # Emits a distribution metric.
@@ -350,13 +356,26 @@ module StatsD
350
356
  # @param metric_options [Hash] (default: {}) Metric options
351
357
  # @return (see #collect_metric)
352
358
  # @note Supported by the datadog implementation only (in beta)
353
- def distribution(key, value, *metric_options)
354
- if value.is_a?(Hash) && metric_options.empty?
355
- metric_options = [value]
356
- value = value.fetch(:value, nil)
357
- end
358
-
359
- collect_metric(hash_argument(metric_options).merge(type: :d, name: key, value: value))
359
+ #
360
+ # @overload distribution(key, metric_options = {}, &block)
361
+ # Emits a distribution metric, after measuring the execution duration of the
362
+ # block passed to this method.
363
+ # @param key [String] The name of the metric.
364
+ # @param metric_options [Hash] Options for the metric
365
+ # @yield The method will yield the block that was passed to this method to measure its duration.
366
+ # @return The value that was returns by the block passed to this method.
367
+ # @note Supported by the datadog implementation only.
368
+ #
369
+ # @example
370
+ # http_response = StatsD.distribution('HTTP.call.duration') do
371
+ # HTTP.get(url)
372
+ # end
373
+ def distribution(key, value=nil, *metric_options, &block)
374
+ value, metric_options = parse_options(value, metric_options)
375
+ result = nil
376
+ value = 1000 * StatsD::Instrument.duration { result = block.call } if block_given?
377
+ metric = collect_metric(:d, key, value, metric_options)
378
+ (result || metric)
360
379
  end
361
380
 
362
381
  # Emits a key/value metric.
@@ -366,12 +385,7 @@ module StatsD
366
385
  # @return (see #collect_metric)
367
386
  # @note Supported by the statsite implementation only.
368
387
  def key_value(key, value, *metric_options)
369
- if value.is_a?(Hash) && metric_options.empty?
370
- metric_options = [value]
371
- value = value.fetch(:value, nil)
372
- end
373
-
374
- collect_metric(hash_argument(metric_options).merge(type: :kv, name: key, value: value))
388
+ collect_metric(:kv, key, value, metric_options)
375
389
  end
376
390
 
377
391
  # Emits a set metric.
@@ -381,12 +395,7 @@ module StatsD
381
395
  # @return (see #collect_metric)
382
396
  # @note Supported by the datadog implementation only.
383
397
  def set(key, value, *metric_options)
384
- if value.is_a?(Hash) && metric_options.empty?
385
- metric_options = [value]
386
- value = value.fetch(:value, nil)
387
- end
388
-
389
- collect_metric(hash_argument(metric_options).merge(type: :s, name: key, value: value))
398
+ collect_metric(:s, key, value, metric_options)
390
399
  end
391
400
 
392
401
  # Emits an event metric.
@@ -396,12 +405,7 @@ module StatsD
396
405
  # @return (see #collect_metric)
397
406
  # @note Supported by the datadog implementation only.
398
407
  def event(title, text, *metric_options)
399
- if text.is_a?(Hash) && metric_options.empty?
400
- metric_options = [text]
401
- text = text.fetch(:text, nil)
402
- end
403
-
404
- collect_metric(hash_argument(metric_options).merge(type: :_e, name: title, value: text))
408
+ collect_metric(:_e, title, text, metric_options)
405
409
  end
406
410
 
407
411
  # Emits a service check metric.
@@ -411,12 +415,7 @@ module StatsD
411
415
  # @return (see #collect_metric)
412
416
  # @note Supported by the datadog implementation only.
413
417
  def service_check(name, status, *metric_options)
414
- if status.is_a?(Hash) && metric_options.empty?
415
- metric_options = [status]
416
- status = status.fetch(:status, nil)
417
- end
418
-
419
- collect_metric(hash_argument(metric_options).merge(type: :_sc, name: name, value: status))
418
+ collect_metric(:_sc, name, status, metric_options)
420
419
  end
421
420
 
422
421
  private
@@ -437,10 +436,21 @@ module StatsD
437
436
  return hash
438
437
  end
439
438
 
439
+ def parse_options(value, metric_options)
440
+ if value.is_a?(Hash) && metric_options.empty?
441
+ metric_options = [value]
442
+ value = value.fetch(:value, nil)
443
+ end
444
+ [value, metric_options]
445
+ end
446
+
440
447
  # Instantiates a metric, and sends it to the backend for further processing.
441
448
  # @param options (see StatsD::Instrument::Metric#initialize)
442
449
  # @return [StatsD::Instrument::Metric] The meric that was sent to the backend.
443
- def collect_metric(options)
450
+ def collect_metric(type, name, value, metric_options)
451
+ value, metric_options = parse_options(value, metric_options)
452
+
453
+ options = hash_argument(metric_options).merge(type: type, name: name, value: value)
444
454
  backend.collect_metric(metric = StatsD::Instrument::Metric.new(options))
445
455
  metric
446
456
  end
@@ -1,5 +1,5 @@
1
1
  module StatsD
2
2
  module Instrument
3
- VERSION = "2.3.0.beta3"
3
+ VERSION = "2.3.0.beta4"
4
4
  end
5
5
  end
@@ -265,6 +265,64 @@ class StatsDInstrumentationTest < Minitest::Test
265
265
  ActiveMerchant::UniqueGateway.statsd_remove_measure :ssl_post, 'ActiveMerchant.Gateway.ssl_post'
266
266
  end
267
267
 
268
+
269
+ def test_statsd_distribution
270
+ ActiveMerchant::UniqueGateway.statsd_distribution :ssl_post, 'ActiveMerchant.Gateway.ssl_post', sample_rate: 0.3
271
+
272
+ assert_statsd_distribution('ActiveMerchant.Gateway.ssl_post', sample_rate: 0.3) do
273
+ ActiveMerchant::UniqueGateway.new.purchase(true)
274
+ end
275
+ ensure
276
+ ActiveMerchant::UniqueGateway.statsd_remove_distribution :ssl_post, 'ActiveMerchant.Gateway.ssl_post'
277
+ end
278
+
279
+ def test_statsd_distribution_uses_normalized_metric_name
280
+ ActiveMerchant::UniqueGateway.statsd_distribution :ssl_post, 'ActiveMerchant::Gateway.ssl_post'
281
+
282
+ assert_statsd_distribution('ActiveMerchant.Gateway.ssl_post') do
283
+ ActiveMerchant::UniqueGateway.new.purchase(true)
284
+ end
285
+ ensure
286
+ ActiveMerchant::UniqueGateway.statsd_remove_distribution :ssl_post, 'ActiveMerchant::Gateway.ssl_post'
287
+ end
288
+
289
+ def test_statsd_distribution_yells_without_block
290
+ err = assert_raises(ArgumentError) do
291
+ assert_statsd_distribution('ActiveMerchant.Gateway.ssl_post')
292
+ end
293
+ assert_equal "block must be given", err.to_s
294
+ end
295
+
296
+ def test_statsd_distribution_with_method_receiving_block
297
+ ActiveMerchant::Base.statsd_distribution :post_with_block, 'ActiveMerchant.Base.post_with_block'
298
+
299
+ assert_statsd_distribution('ActiveMerchant.Base.post_with_block') do
300
+ assert_equal 'block called', ActiveMerchant::Base.new.post_with_block { 'block called' }
301
+ end
302
+ ensure
303
+ ActiveMerchant::Base.statsd_remove_distribution :post_with_block, 'ActiveMerchant.Base.post_with_block'
304
+ end
305
+
306
+ def test_statsd_distribution_with_value
307
+ ActiveMerchant::UniqueGateway.statsd_distribution :ssl_post, 'ActiveMerchant.Gateway.ssl_post', 1
308
+
309
+ assert_statsd_distribution('ActiveMerchant.Gateway.ssl_post') do
310
+ ActiveMerchant::UniqueGateway.new.purchase(true)
311
+ end
312
+ ensure
313
+ ActiveMerchant::UniqueGateway.statsd_remove_distribution :ssl_post, 'ActiveMerchant.Gateway.ssl_post'
314
+ end
315
+
316
+ def test_statsd_distribution_with_value_and_options
317
+ ActiveMerchant::UniqueGateway.statsd_distribution :ssl_post, 'ActiveMerchant.Gateway.ssl_post', 1, sample_rate: 0.45
318
+
319
+ assert_statsd_distribution('ActiveMerchant.Gateway.ssl_post', sample_rate: 0.45) do
320
+ ActiveMerchant::UniqueGateway.new.purchase(true)
321
+ end
322
+ ensure
323
+ ActiveMerchant::UniqueGateway.statsd_remove_distribution :ssl_post, 'ActiveMerchant.Gateway.ssl_post'
324
+ end
325
+
268
326
  def test_instrumenting_class_method
269
327
  ActiveMerchant::Gateway.singleton_class.extend StatsD::Instrument
270
328
  ActiveMerchant::Gateway.singleton_class.statsd_count :sync, 'ActiveMerchant.Gateway.sync'
data/test/statsd_test.rb CHANGED
@@ -143,6 +143,31 @@ class StatsDTest < Minitest::Test
143
143
  assert_equal 42, metric.value
144
144
  end
145
145
 
146
+ def test_statsd_distribution_with_benchmarked_block_duration
147
+ StatsD::Instrument.stubs(:duration).returns(1.12)
148
+ metric = capture_statsd_call do
149
+ StatsD.distribution('values.foobar') { 'foo' }
150
+ end
151
+ assert_equal :d, metric.type
152
+ assert_equal 1120.0, metric.value
153
+ end
154
+
155
+ def test_statsd_distribution_with_block_and_options
156
+ StatsD::Instrument.stubs(:duration).returns(1.12)
157
+ metric = capture_statsd_call do
158
+ StatsD.distribution('values.foobar', :tags => ['test'], :sample_rate => 0.9) { 'foo' }
159
+ end
160
+ assert_equal 1120.0, metric.value
161
+ assert_equal 'values.foobar', metric.name
162
+ assert_equal 0.9, metric.sample_rate
163
+ assert_equal ['test'], metric.tags
164
+ end
165
+
166
+ def test_statsd_distribution_returns_return_value_of_block
167
+ return_value = StatsD.distribution('values.foobar') { 'sarah' }
168
+ assert_equal 'sarah', return_value
169
+ end
170
+
146
171
  def test_statsd_key_value
147
172
  result = nil
148
173
  metric = capture_statsd_call { result = StatsD.key_value('values.foobar', 42) }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: statsd-instrument
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.0.beta3
4
+ version: 2.3.0.beta4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jesse Storimer
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2018-07-04 00:00:00.000000000 Z
13
+ date: 2018-07-10 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake