statsd-instrument 2.5.1 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop-https---shopify-github-io-ruby-style-guide-rubocop-yml +1 -1
  3. data/.rubocop.yml +11 -6
  4. data/.yardopts +5 -0
  5. data/CHANGELOG.md +75 -6
  6. data/README.md +54 -46
  7. data/benchmark/datagram-client +41 -0
  8. data/lib/statsd/instrument/assertions.rb +168 -57
  9. data/lib/statsd/instrument/backends/udp_backend.rb +20 -29
  10. data/lib/statsd/instrument/capture_sink.rb +27 -0
  11. data/lib/statsd/instrument/client.rb +313 -0
  12. data/lib/statsd/instrument/datagram.rb +75 -0
  13. data/lib/statsd/instrument/datagram_builder.rb +101 -0
  14. data/lib/statsd/instrument/dogstatsd_datagram_builder.rb +71 -0
  15. data/lib/statsd/instrument/environment.rb +106 -29
  16. data/lib/statsd/instrument/log_sink.rb +24 -0
  17. data/lib/statsd/instrument/null_sink.rb +13 -0
  18. data/lib/statsd/instrument/rubocop/measure_as_dist_argument.rb +39 -0
  19. data/lib/statsd/instrument/rubocop/metaprogramming_positional_arguments.rb +6 -10
  20. data/lib/statsd/instrument/rubocop/metric_prefix_argument.rb +37 -0
  21. data/lib/statsd/instrument/rubocop/metric_return_value.rb +7 -6
  22. data/lib/statsd/instrument/rubocop/metric_value_keyword_argument.rb +11 -20
  23. data/lib/statsd/instrument/rubocop/positional_arguments.rb +13 -13
  24. data/lib/statsd/instrument/rubocop/splat_arguments.rb +8 -14
  25. data/lib/statsd/instrument/rubocop.rb +64 -0
  26. data/lib/statsd/instrument/statsd_datagram_builder.rb +14 -0
  27. data/lib/statsd/instrument/strict.rb +112 -22
  28. data/lib/statsd/instrument/udp_sink.rb +62 -0
  29. data/lib/statsd/instrument/version.rb +1 -1
  30. data/lib/statsd/instrument.rb +191 -100
  31. data/test/assertions_test.rb +139 -176
  32. data/test/capture_sink_test.rb +44 -0
  33. data/test/client_test.rb +164 -0
  34. data/test/compatibility/dogstatsd_datagram_compatibility_test.rb +162 -0
  35. data/test/datagram_builder_test.rb +120 -0
  36. data/test/deprecations_test.rb +56 -10
  37. data/test/dogstatsd_datagram_builder_test.rb +32 -0
  38. data/test/environment_test.rb +73 -7
  39. data/test/log_sink_test.rb +37 -0
  40. data/test/null_sink_test.rb +13 -0
  41. data/test/rubocop/measure_as_dist_argument_test.rb +44 -0
  42. data/test/rubocop/metaprogramming_positional_arguments_test.rb +1 -1
  43. data/test/rubocop/metric_prefix_argument_test.rb +38 -0
  44. data/test/rubocop/metric_return_value_test.rb +1 -1
  45. data/test/rubocop/metric_value_keyword_argument_test.rb +1 -1
  46. data/test/rubocop/positional_arguments_test.rb +1 -1
  47. data/test/rubocop/splat_arguments_test.rb +1 -1
  48. data/test/statsd_datagram_builder_test.rb +22 -0
  49. data/test/statsd_instrumentation_test.rb +0 -24
  50. data/test/statsd_test.rb +0 -23
  51. data/test/test_helper.rb +0 -2
  52. data/test/udp_backend_test.rb +25 -8
  53. data/test/udp_sink_test.rb +85 -0
  54. metadata +38 -2
@@ -29,14 +29,13 @@ require 'logger'
29
29
  # @!attribute logger
30
30
  # The logger to use in case of any errors. The logger is also used as default logger
31
31
  # for the LoggerBackend (although this can be overwritten).
32
+ # @see StatsD::Instrument::Backends::LoggerBackend
33
+ # @return [Logger]
32
34
  #
33
35
  # @!attribute default_tags
34
36
  # The tags to apply to all metrics.
35
37
  # @return [Array<String>, Hash<String, String>, nil] The default tags, or <tt>nil</tt> when no default tags is used
36
38
  #
37
- # @see StatsD::Instrument::Backends::LoggerBackend
38
- # @return [Logger]
39
- #
40
39
  # @see StatsD::Instrument <tt>StatsD::Instrument</tt> contains module to instrument
41
40
  # existing methods with StatsD metrics.
42
41
  module StatsD
@@ -98,7 +97,7 @@ module StatsD
98
97
  define_method(method) do |*args, &block|
99
98
  key = StatsD::Instrument.generate_metric_name(name, self, *args)
100
99
  prefix ||= StatsD.prefix
101
- StatsD.measure(
100
+ StatsD.measure( # rubocop:disable StatsD/MeasureAsDistArgument, StatsD/MetricPrefixArgument
102
101
  key, sample_rate: sample_rate, tags: tags, prefix: prefix, no_prefix: no_prefix, as_dist: as_dist
103
102
  ) do
104
103
  super(*args, &block)
@@ -122,7 +121,9 @@ module StatsD
122
121
  define_method(method) do |*args, &block|
123
122
  key = StatsD::Instrument.generate_metric_name(name, self, *args)
124
123
  prefix ||= StatsD.prefix
125
- StatsD.distribution(key, sample_rate: sample_rate, tags: tags, prefix: prefix, no_prefix: no_prefix) do
124
+ StatsD.distribution( # rubocop:disable StatsD/MetricPrefixArgument
125
+ key, sample_rate: sample_rate, tags: tags, prefix: prefix, no_prefix: no_prefix
126
+ ) do
126
127
  super(*args, &block)
127
128
  end
128
129
  end
@@ -167,7 +168,8 @@ module StatsD
167
168
  suffix = truthiness == false ? 'failure' : 'success'
168
169
  key = "#{StatsD::Instrument.generate_metric_name(name, self, *args)}.#{suffix}"
169
170
  prefix ||= StatsD.prefix
170
- StatsD.increment(key, sample_rate: sample_rate, tags: tags, prefix: prefix, no_prefix: no_prefix)
171
+ StatsD.increment(key, prefix: prefix, # rubocop:disable StatsD/MetricPrefixArgument
172
+ sample_rate: sample_rate, tags: tags, no_prefix: no_prefix)
171
173
  end
172
174
  end
173
175
  end
@@ -208,7 +210,8 @@ module StatsD
208
210
  if truthiness
209
211
  key = StatsD::Instrument.generate_metric_name(name, self, *args)
210
212
  prefix ||= StatsD.prefix
211
- StatsD.increment(key, sample_rate: sample_rate, tags: tags, prefix: prefix, no_prefix: no_prefix)
213
+ StatsD.increment(key, prefix: prefix, # rubocop:disable StatsD/MetricPrefixArgument
214
+ sample_rate: sample_rate, tags: tags, no_prefix: no_prefix)
212
215
  end
213
216
  end
214
217
  end
@@ -231,7 +234,8 @@ module StatsD
231
234
  define_method(method) do |*args, &block|
232
235
  key = StatsD::Instrument.generate_metric_name(name, self, *args)
233
236
  prefix ||= StatsD.prefix
234
- StatsD.increment(key, sample_rate: sample_rate, tags: tags, prefix: prefix, no_prefix: no_prefix)
237
+ StatsD.increment(key, prefix: prefix, # rubocop:disable StatsD/MetricPrefixArgument
238
+ sample_rate: sample_rate, tags: tags, no_prefix: no_prefix)
235
239
  super(*args, &block)
236
240
  end
237
241
  end
@@ -330,7 +334,7 @@ module StatsD
330
334
  end
331
335
 
332
336
  attr_accessor :logger, :default_sample_rate, :prefix
333
- attr_writer :backend
337
+ attr_writer :backend, :client
334
338
  attr_reader :default_tags
335
339
 
336
340
  def default_tags=(tags)
@@ -341,54 +345,64 @@ module StatsD
341
345
  @backend ||= StatsD::Instrument::Environment.default_backend
342
346
  end
343
347
 
344
- # Emits a duration metric.
348
+ def client
349
+ @client ||= begin
350
+ require 'statsd/instrument/client'
351
+ StatsD::Instrument::Environment.from_env.default_client
352
+ end
353
+ end
354
+
355
+ # @!method measure(name, value = nil, sample_rate: nil, tags: nil, &block)
356
+ #
357
+ # Emits a timing metric
358
+ #
359
+ # @param [String] key The name of the metric.
360
+ # @param sample_rate (see #increment)
361
+ # @param tags (see #increment)
362
+ #
363
+ # @example Providing a value directly
364
+ # start = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
365
+ # do_something
366
+ # stop = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
367
+ # http_response = StatsD.measure('HTTP.call.duration', stop - start)
368
+ #
369
+ # @example Providing a block to measure the duration of its execution
370
+ # http_response = StatsD.measure('HTTP.call.duration') do
371
+ # Net::HTTP.get(url)
372
+ # end
373
+ #
374
+ # @overload measure(key, value, sample_rate: nil, tags: nil)
375
+ # Emits a timing metric, by providing a duration in milliseconds.
345
376
  #
346
- # @overload measure(key, value, metric_options = {})
347
- # Emits a measure metric, by providing a duration in milliseconds.
348
- # @param key [String] The name of the metric.
349
- # @param value [Float] The measured duration in milliseconds
350
- # @param metric_options [Hash] Options for the metric
351
- # the key :as_dist will submit the value as a distribution instead of a timing
352
- # (only supported by DataDog's implementation)
353
- # @return [StatsD::Instrument::Metric] The metric that was sent to the backend.
377
+ # @param [Float] value The measured duration in milliseconds
378
+ # @return [void]
354
379
  #
355
- # @overload measure(key, metric_options = {}, &block)
356
- # Emits a measure metric, after measuring the execution duration of the
380
+ # @overload measure(key, sample_rate: nil, tags: nil, &block)
381
+ # Emits a timing metric, after measuring the execution duration of the
357
382
  # block passed to this method.
358
- # @param key [String] The name of the metric.
359
- # @param metric_options [Hash] Options for the metric
360
- # the key :as_dist sets the metric type to a 'distribution' instead of a 'timing'
361
- # (only supported by DataDog's implementation)
362
- # @yield The method will yield the block that was passed to this method to measure its duration.
363
- # @return The value that was returns by the block passed to this method.
364
- #
365
- # @example
366
- # http_response = StatsD.measure('HTTP.call.duration') do
367
- # HTTP.get(url)
368
- # end
383
+ #
384
+ # @yield `StatsD.measure` will yield the block and measure the duration. After the block
385
+ # returns, the duration in millisecond will be emitted as metric.
386
+ # @return The value that was returned by the block passed through.
369
387
  def measure(
370
388
  key, value_arg = nil, deprecated_sample_rate_arg = nil, deprecated_tags_arg = nil,
371
389
  value: value_arg, sample_rate: deprecated_sample_rate_arg, tags: deprecated_tags_arg,
372
390
  prefix: StatsD.prefix, no_prefix: false, as_dist: false,
373
391
  &block
374
392
  )
375
- prefix = nil if no_prefix
393
+ # TODO: in the next version, hardcode this to :ms when the as_dist argument is dropped.
376
394
  type = as_dist ? :d : :ms
377
- unless block_given?
378
- return collect_metric(type, key, value, sample_rate: sample_rate, tags: tags, prefix: prefix, &block)
379
- end
380
-
381
- start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
382
- begin
383
- block.call
384
- ensure
385
- # Ensure catches both a raised exception and a return in the invoked block
386
- value = 1000.0 * (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start)
395
+ prefix = nil if no_prefix
396
+ if block_given?
397
+ measure_latency(type, key, sample_rate: sample_rate, tags: tags, prefix: prefix, &block)
398
+ else
387
399
  collect_metric(type, key, value, sample_rate: sample_rate, tags: tags, prefix: prefix)
388
400
  end
389
401
  end
390
402
 
403
+ # @!method increment(name, value = 1, sample_rate: nil, tags: nil)
391
404
  # Emits a counter metric.
405
+ #
392
406
  # @param key [String] The name of the metric.
393
407
  # @param value [Integer] The value to increment the counter by.
394
408
  #
@@ -397,7 +411,17 @@ module StatsD
397
411
  # The sample rate is part of the packet that is being sent to the server, and the server
398
412
  # should know how to handle it.
399
413
  #
400
- # @param metric_options [Hash] (default: {}) Metric options
414
+ # @param sample_rate [Float] (default: `StatsD.default_sample_rate`) The rate at which to sample
415
+ # this metric call. This value should be between 0 and 1. This value can be used to reduce
416
+ # the amount of network I/O (and CPU cycles) used for very frequent metrics.
417
+ #
418
+ # - A value of `0.1` means that only 1 out of 10 calls will be emitted; the other 9 will
419
+ # be short-circuited.
420
+ # - When set to `1`, every metric will be emitted.
421
+ # - If this parameter is not set, the default sample rate for this client will be used.
422
+ # @param tags [Array<String>, Hash<Symbol, String>] The tags to associate with this measurement.
423
+ # They can be provided as an array of strings, or a hash of key/value pairs.
424
+ # _Note:_ Tags are not supported by all implementations.
401
425
  # @return [void]
402
426
  def increment(
403
427
  key, value_arg = 1, deprecated_sample_rate_arg = nil, deprecated_tags_arg = nil,
@@ -408,11 +432,15 @@ module StatsD
408
432
  collect_metric(:c, key, value, sample_rate: sample_rate, tags: tags, prefix: prefix)
409
433
  end
410
434
 
435
+ # @!method gauge(name, value, sample_rate: nil, tags: nil)
436
+ #
411
437
  # Emits a gauge metric.
412
- # @param key [String] The name of the metric.
438
+ #
439
+ # @param key The name of the metric.
413
440
  # @param value [Numeric] The current value to record.
414
- # @param metric_options [Hash] (default: {}) Metric options
415
- # @return (see #collect_metric)
441
+ # @param sample_rate (see #increment)
442
+ # @param tags (see #increment)
443
+ # @return [void]
416
444
  def gauge(
417
445
  key, value_arg = nil, deprecated_sample_rate_arg = nil, deprecated_tags_arg = nil,
418
446
  value: value_arg, sample_rate: deprecated_sample_rate_arg, tags: deprecated_tags_arg,
@@ -422,10 +450,35 @@ module StatsD
422
450
  collect_metric(:g, key, value, sample_rate: sample_rate, tags: tags, prefix: prefix)
423
451
  end
424
452
 
425
- # Emits a histogram metric.
453
+ # @!method set(name, value, sample_rate: nil, tags: nil)
454
+ #
455
+ # Emits a set metric, which counts the number of distinct values that have occurred.
456
+ #
457
+ # @example Couning the number of unique visitors
458
+ # StatsD.set('visitors.unique', Current.user.id)
459
+ #
426
460
  # @param key [String] The name of the metric.
427
461
  # @param value [Numeric] The value to record.
428
- # @param metric_options [Hash] (default: {}) Metric options
462
+ # @param sample_rate (see #increment)
463
+ # @param tags (see #increment)
464
+ # @return [void]
465
+ def set(
466
+ key, value_arg = nil, deprecated_sample_rate_arg = nil, deprecated_tags_arg = nil,
467
+ value: value_arg, sample_rate: deprecated_sample_rate_arg, tags: deprecated_tags_arg,
468
+ prefix: StatsD.prefix, no_prefix: false
469
+ )
470
+ prefix = nil if no_prefix
471
+ collect_metric(:s, key, value, sample_rate: sample_rate, tags: tags, prefix: prefix)
472
+ end
473
+
474
+ # @!method histogram(name, value, sample_rate: nil, tags: nil)
475
+ #
476
+ # Emits a histogram metric.
477
+ #
478
+ # @param key The name of the metric.
479
+ # @param value [Numeric] The value to record.
480
+ # @param sample_rate (see #increment)
481
+ # @param tags (see #increment)
429
482
  # @return (see #collect_metric)
430
483
  # @note Supported by the datadog implementation only.
431
484
  def histogram(
@@ -437,26 +490,34 @@ module StatsD
437
490
  collect_metric(:h, key, value, sample_rate: sample_rate, tags: tags, prefix: prefix)
438
491
  end
439
492
 
493
+ # @!method distribution(name, value = nil, sample_rate: nil, tags: nil, &block)
494
+ #
440
495
  # Emits a distribution metric.
441
- # @param key [String] The name of the metric.
442
- # @param value [Numeric] The value to record.
443
- # @param metric_options [Hash] (default: {}) Metric options
444
- # @return (see #collect_metric)
445
- # @note Supported by the datadog implementation only (in beta)
496
+ #
497
+ # @param [String] key The name of the metric.
498
+ # @param sample_rate (see #increment)
499
+ # @param tags (see #increment)
500
+ #
501
+ # @note Supported by the datadog implementation only.
502
+ # @example
503
+ # http_response = StatsD.distribution('HTTP.call.duration') do
504
+ # Net::HTTP.get(url)
505
+ # end
506
+ #
507
+ # @overload distribution(name, value, sample_rate: nil, tags: nil)
508
+ #
509
+ # Emits a distribution metric, given a provided value to record.
510
+ #
511
+ # @param [Numeric] value The value to record.
512
+ # @return [void]
446
513
  #
447
514
  # @overload distribution(key, metric_options = {}, &block)
448
- # Emits a distribution metric, after measuring the execution duration of the
449
- # block passed to this method.
450
- # @param key [String] The name of the metric.
451
- # @param metric_options [Hash] Options for the metric
452
- # @yield The method will yield the block that was passed to this method to measure its duration.
453
- # @return The value that was returns by the block passed to this method.
454
- # @note Supported by the datadog implementation only.
455
- #
456
- # @example
457
- # http_response = StatsD.distribution('HTTP.call.duration') do
458
- # HTTP.get(url)
459
- # end
515
+ #
516
+ # Emits a distribution metric for the duration of the provided block, in milliseconds.
517
+ #
518
+ # @yield `StatsD.distribution` will yield the block and measure the duration. After
519
+ # the block returns, the duration in millisecond will be emitted as metric.
520
+ # @return The value that was returned by the block passed through.
460
521
  def distribution(
461
522
  key, value_arg = nil, deprecated_sample_rate_arg = nil, deprecated_tags_arg = nil,
462
523
  value: value_arg, sample_rate: deprecated_sample_rate_arg, tags: deprecated_tags_arg,
@@ -464,14 +525,21 @@ module StatsD
464
525
  &block
465
526
  )
466
527
  prefix = nil if no_prefix
467
- measure(key, value, as_dist: true, sample_rate: sample_rate, tags: tags, prefix: prefix, &block)
528
+ if block_given?
529
+ measure_latency(:d, key, sample_rate: sample_rate, tags: tags, prefix: prefix, &block)
530
+ else
531
+ collect_metric(:d, key, value, sample_rate: sample_rate, tags: tags, prefix: prefix)
532
+ end
468
533
  end
469
534
 
535
+ # @!method key_value(name, value)
536
+ #
470
537
  # Emits a key/value metric.
538
+ #
471
539
  # @param key [String] The name of the metric.
472
540
  # @param value [Numeric] The value to record.
473
- # @param metric_options [Hash] (default: {}) Metric options
474
- # @return (see #collect_metric)
541
+ # @return [void]
542
+ #
475
543
  # @note Supported by the statsite implementation only.
476
544
  def key_value(
477
545
  key, value_arg = nil, deprecated_sample_rate_arg = nil,
@@ -481,55 +549,77 @@ module StatsD
481
549
  collect_metric(:kv, key, value, sample_rate: sample_rate, prefix: prefix)
482
550
  end
483
551
 
484
- # Emits a set metric.
485
- # @param key [String] The name of the metric.
486
- # @param value [Numeric] The value to record.
487
- # @param metric_options [Hash] (default: {}) Metric options
488
- # @return (see #collect_metric)
489
- # @note Supported by the datadog implementation only.
490
- def set(
491
- key, value_arg = nil, deprecated_sample_rate_arg = nil, deprecated_tags_arg = nil,
492
- value: value_arg, sample_rate: deprecated_sample_rate_arg, tags: deprecated_tags_arg,
493
- prefix: StatsD.prefix, no_prefix: false
494
- )
495
- prefix = nil if no_prefix
496
- collect_metric(:s, key, value, sample_rate: sample_rate, tags: tags, prefix: prefix)
497
- end
498
-
499
- # Emits an event metric.
500
- # @param title [String] Title of the event.
501
- # @param text [String] Body of the event.
502
- # @param metric_options [Hash] (default: {}) Metric options
503
- # @return (see #collect_metric)
504
- # @note Supported by the datadog implementation only.
552
+ # @!method event(title, text, tags: nil, hostname: nil, timestamp: nil, aggregation_key: nil, priority: nil, source_type_name: nil, alert_type: nil) # rubocop:disable Metrics/LineLength
553
+ #
554
+ # Emits an event.
555
+ #
556
+ # @param title [String] Title of the event. A configured prefix may be applied to this title.
557
+ # @param text [String] Body of the event. Can contain newlines.
558
+ # @param [String] hostname The hostname to associate with the event.
559
+ # @param [Time] timestamp The moment the status of the service was checkes. Defaults to now.
560
+ # @param [String] aggregation_key A key to aggregate similar events into groups.
561
+ # @param [String] priority The event's priority, either `"low"` or `"normal"` (default).
562
+ # @param [String] source_type_name The source type.
563
+ # @param [String] alert_type The type of alert. Either `"info"` (default), `"warning"`, `"error"`, or `"success"`.
564
+ # @param tags (see #increment)
565
+ # @return [void]
566
+ #
567
+ # @note Supported by the Datadog implementation only.
505
568
  def event(
506
569
  title, text,
507
570
  deprecated_sample_rate_arg = nil, deprecated_tags_arg = nil,
508
571
  sample_rate: deprecated_sample_rate_arg, tags: deprecated_tags_arg,
509
- prefix: StatsD.prefix, no_prefix: false, **metadata
572
+ prefix: StatsD.prefix, no_prefix: false,
573
+ hostname: nil, date_happened: nil, timestamp: date_happened,
574
+ aggregation_key: nil, priority: nil, source_type_name: nil, alert_type: nil,
575
+ **_ignored
510
576
  )
511
577
  prefix = nil if no_prefix
512
- collect_metric(:_e, title, text, sample_rate: sample_rate, tags: tags, prefix: prefix, metadata: metadata)
578
+ collect_metric(:_e, title, text, sample_rate: sample_rate, tags: tags, prefix: prefix, metadata: {
579
+ hostname: hostname, timestamp: timestamp, aggregation_key: aggregation_key,
580
+ priority: priority, source_type_name: source_type_name, alert_type: alert_type
581
+ })
513
582
  end
514
583
 
515
- # Emits a service check metric.
516
- # @param title [String] Title of the event.
517
- # @param text [String] Body of the event.
518
- # @param metric_options [Hash] (default: {}) Metric options
519
- # @return (see #collect_metric)
520
- # @note Supported by the datadog implementation only.
584
+ # @!method service_check(name, status, tags: nil, hostname: nil, timestamp: nil, message: nil)
585
+ #
586
+ # Emits a service check.
587
+ #
588
+ # @param [String] name Name of the service. A configured prefix may be applied to this title.
589
+ # @param [Symbol] status Current status of the service. Either `:ok`, `:warning`, `:critical`, or `:unknown`.
590
+ # @param [String] hostname The hostname to associate with the event.
591
+ # @param [Time] timestamp The moment the status of the service was checkes. Defaults to now.
592
+ # @param [String] message A message that describes the current status.
593
+ # @param tags (see #increment)
594
+ # @return [void]
595
+ #
596
+ # @note Supported by the Datadog implementation only.
521
597
  def service_check(
522
598
  name, status,
523
599
  deprecated_sample_rate_arg = nil, deprecated_tags_arg = nil,
524
600
  sample_rate: deprecated_sample_rate_arg, tags: deprecated_tags_arg,
525
- prefix: StatsD.prefix, no_prefix: false, **metadata
601
+ prefix: StatsD.prefix, no_prefix: false,
602
+ hostname: nil, timestamp: nil, message: nil, **_ignored
526
603
  )
527
604
  prefix = nil if no_prefix
528
- collect_metric(:_sc, name, status, sample_rate: sample_rate, tags: tags, prefix: prefix, metadata: metadata)
605
+ collect_metric(:_sc, name, status, sample_rate: sample_rate, prefix: prefix, tags: tags, metadata: {
606
+ hostname: hostname, timestamp: timestamp, message: message
607
+ })
529
608
  end
530
609
 
531
610
  private
532
611
 
612
+ def measure_latency(type, key, sample_rate:, tags:, prefix:)
613
+ start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
614
+ begin
615
+ yield
616
+ ensure
617
+ # Ensure catches both a raised exception and a return in the invoked block
618
+ value = 1000.0 * (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start)
619
+ collect_metric(type, key, value, sample_rate: sample_rate, tags: tags, prefix: prefix)
620
+ end
621
+ end
622
+
533
623
  # Instantiates a metric, and sends it to the backend for further processing.
534
624
  # @param options (see StatsD::Instrument::Metric#initialize)
535
625
  # @return [void]
@@ -552,4 +642,5 @@ require 'statsd/instrument/helpers'
552
642
  require 'statsd/instrument/assertions'
553
643
  require 'statsd/instrument/metric_expectation'
554
644
  require 'statsd/instrument/matchers' if defined?(::RSpec)
555
- require 'statsd/instrument/railtie' if defined?(Rails)
645
+ require 'statsd/instrument/railtie' if defined?(::Rails::Railtie)
646
+ require 'statsd/instrument/strict' if ENV['STATSD_STRICT_MODE']