statsd-instrument 2.6.0 → 2.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +79 -9
  3. data/benchmark/datagram-client +0 -1
  4. data/benchmark/send-metrics-to-dev-null-log +0 -1
  5. data/lib/statsd/instrument.rb +66 -292
  6. data/lib/statsd/instrument/assertions.rb +83 -93
  7. data/lib/statsd/instrument/client.rb +2 -2
  8. data/lib/statsd/instrument/datagram.rb +12 -3
  9. data/lib/statsd/instrument/environment.rb +9 -3
  10. data/lib/statsd/instrument/expectation.rb +93 -0
  11. data/lib/statsd/instrument/helpers.rb +29 -11
  12. data/lib/statsd/instrument/legacy_client.rb +301 -0
  13. data/lib/statsd/instrument/metric.rb +8 -8
  14. data/lib/statsd/instrument/rubocop.rb +18 -0
  15. data/lib/statsd/instrument/rubocop/singleton_configuration.rb +53 -0
  16. data/lib/statsd/instrument/version.rb +1 -1
  17. data/test/assertions_on_legacy_client_test.rb +376 -0
  18. data/test/assertions_test.rb +105 -39
  19. data/test/capture_sink_test.rb +0 -2
  20. data/test/client_test.rb +0 -2
  21. data/test/compatibility/dogstatsd_datagram_compatibility_test.rb +0 -1
  22. data/test/datagram_builder_test.rb +1 -3
  23. data/test/datagram_test.rb +14 -0
  24. data/test/dogstatsd_datagram_builder_test.rb +0 -2
  25. data/test/environment_test.rb +1 -1
  26. data/test/helpers_test.rb +17 -0
  27. data/test/log_sink_test.rb +0 -2
  28. data/test/logger_backend_test.rb +2 -2
  29. data/test/metric_test.rb +2 -2
  30. data/test/null_sink_test.rb +0 -2
  31. data/test/rubocop/singleton_configuration_test.rb +43 -0
  32. data/test/statsd_datagram_builder_test.rb +0 -2
  33. data/test/statsd_instrumentation_test.rb +4 -6
  34. data/test/statsd_test.rb +1 -1
  35. data/test/test_helper.rb +0 -2
  36. data/test/udp_backend_test.rb +1 -1
  37. data/test/udp_sink_test.rb +0 -2
  38. metadata +11 -3
  39. data/lib/statsd/instrument/metric_expectation.rb +0 -82
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0e0581a0deec7cbdfae1ca0b03d336b3e08cf1d14599087678f1a6a6ec35b321
4
- data.tar.gz: f10c0f102d13bb71a8365a1cc9707c848af954adb02eea3b8bbb95a9bbc2f0b7
3
+ metadata.gz: 3929918b255f6be82e1fa81b2f996dbef0f51cabc3fa374c0386bb37cd53f58f
4
+ data.tar.gz: 322b93e4298c2a5bb2a32934d2c8d6ffb580248a229d5956053ea02aafa0934e
5
5
  SHA512:
6
- metadata.gz: 0fc4c808a257a72bf0d86edb1e9ec9ae6c700f80408f83d574e466e1ce7cf2d627aee5e71dc5fdbb998850395881421cba97172db446fc102d04314546d55f60
7
- data.tar.gz: 748ffe0481def020345c33029a630b266f033d84dcf80429316e0ec95a7394b82e20f82e9ee568efa3f455ac853fcbb3fec99243da482f8af3967069dffae13e
6
+ metadata.gz: b7a72b5b53f07719d207a91d37b30eb78d13f6c06f26566f8450c4e14b50bc9095498c19c12cb440484292f31e9a890d97ab478c4fced766ff55f0c2b32b8ea8
7
+ data.tar.gz: fb2d2484b3dd9c6533fd6c445ab7871400d3fa5e06545303ef373f3d566ad0dcf1638a40228f84fb321b0a52c60e687a119e13b08664f385aaa03695604f339e
@@ -8,6 +8,76 @@ section below.
8
8
 
9
9
  _Nothing yet!_
10
10
 
11
+ ## Version 2.7.0
12
+
13
+ This release allows you to switch the StatsD singleton to use the new, more
14
+ performant client. By setting `STATSD_USE_NEW_CLIENT` as environment variable
15
+ methods called on the StatsD singleton (e.g. `StatsD.increment`) will be
16
+ delegated to an instance of the new client, configured using environment
17
+ variables.
18
+
19
+ The new client should be mostly compatible with the legacy client, but some
20
+ deprecated functionality will no longer work. See the changelog for version
21
+ 2.6.0 and 2.5.0 for more information about the deprecations, and how to find
22
+ and fix deprecations in your code base.
23
+
24
+ - The old legacy client that was implemented on the `StatsD` singleton has
25
+ been moved to `StatsD::LegacyClient.singleton`. By default, all method
26
+ calls on the `StatsD` singleton will be delegated to this legacy client.
27
+ - By setting `STATSD_USE_NEW_CLIENT` as environment variable, these method
28
+ calls will be delegated to an instance of the new client instead. This
29
+ client is configured using the xisting `STATD_*` environment variables,
30
+ like `STATSD_ADDR` and `STATSD_IMPLEMENTATION`.
31
+ - You can also assign a custom client to `StatsD.singleton_client`.
32
+
33
+ The `assert_statsd_*` methods have been reworked to support asserting StatsD
34
+ datagrams coming from a legacy client, or from a new client.
35
+
36
+ - By default, the assertion methods will capture metrics emitted from
37
+ `StatsD.singleton_client`.
38
+ - You can provide a `client` argument if you want to assert metrics being
39
+ emitted by a different client. E.g.
40
+
41
+ ``` ruby
42
+ assert_statsd_increment('foo', client: my_custom_client) do
43
+ my_custom_client.increment('foo')
44
+ end
45
+ ```
46
+
47
+ - You can also capture metrics yourself, and then run assertions on them
48
+ by providing a `datagrams` argument:
49
+
50
+ ``` ruby
51
+ datagrams = my_custom_client.capture do
52
+ my_custom_client.increment('foo')
53
+ end
54
+ assert_statsd_increment('foo', datagrams: datagrams)
55
+ ```
56
+
57
+ This makes it easy to run multiple assertions on the set of metrics that
58
+ was emitted without having to nest calls.
59
+
60
+ - **⚠️ DEPRECATION**: The following methods to configure the legacy client
61
+ are deprecated:
62
+
63
+ - `Statsd.backend`
64
+ - `StatsD.default_sample_rate`
65
+ - `StatsD.default_tags`
66
+ - `StatsD.prefix`
67
+
68
+ We recommend configuring StatsD using environment variables, which will be
69
+ picked up by the new client as well. You can also instantiate a new client
70
+ yourself; you can provide similar configuration options to
71
+ `StatsD::Instrument::Client.new`.
72
+
73
+ You can use the following Rubocop invocation to find any occurrences of
74
+ these deprecated method calls:
75
+
76
+ ``` sh
77
+ rubocop --require `bundle show statsd-instrument`/lib/statsd/instrument/rubocop.rb \
78
+ --only StatsD/SingletonConfiguration
79
+ ```
80
+
11
81
  ## Version 2.6.0
12
82
 
13
83
  This release contains a new `StatsD::Instrument::Client` class, which is
@@ -49,7 +119,7 @@ deprecated patterns. See below for more info.
49
119
  - **⚠️ DEPRECATION**: Using the `as_dist: true` argument for `StatsD.measure`
50
120
  and `statsd_measure` methods is deprecated. This argument was only available
51
121
  for internal use, but was exposed in the public API. It is unlikely that you
52
- are usijng this argumenr, but you can check to make sure using this Rubocop
122
+ are using this argument, but you can check to make sure using this Rubocop
53
123
  invocation:
54
124
 
55
125
  ``` sh
@@ -104,9 +174,9 @@ deprecated patterns. See below for more info.
104
174
  emitted. However, this would hide the the exception from the assertion message,
105
175
  complicating debugging efforts.
106
176
 
107
- Now, we fail the test because an unexpected exception occured inside the block.
177
+ Now, we fail the test because an unexpected exception occurred inside the block.
108
178
  This means that the following test will fail:
109
-
179
+ s
110
180
  ``` ruby
111
181
  assert_raises(RuntimeError) do
112
182
  assert_statsd_increment('foo') do
@@ -137,7 +207,7 @@ deprecated patterns. See below for more info.
137
207
  the metaprogramming method was evaluated would not be respected. This
138
208
  unfortunately is quite common when you set the StatsD prefix inside an
139
209
  initializer. This issue is now addressed: the prefix is evaluated at the
140
- mopment the metric is emitted, not when the metaprogramming method is being
210
+ moment the metric is emitted, not when the metaprogramming method is being
141
211
  evaluated. (#202)
142
212
 
143
213
  ## Version 2.5.0
@@ -223,7 +293,7 @@ deprecated patterns. See below for more info.
223
293
  in production because enabling it comes with a performance penalty.
224
294
 
225
295
  - **Performance improvements 🎉**: Several internal changes have made the
226
- library run singificantly faster. The changes:
296
+ library run significantly faster. The changes:
227
297
 
228
298
  - Improve performance of duration calculations. (#168)
229
299
  - Early exit when no changes are needed to bring tags and metric names to
@@ -261,7 +331,7 @@ deprecated patterns. See below for more info.
261
331
 
262
332
  ## Version 2.3.5
263
333
 
264
- - Re-add `StatsD::Instrument.duration`, which was accidentally removed since verison 2.5.3 (#157)
334
+ - Re-add `StatsD::Instrument.duration`, which was accidentally removed since version 2.5.3 (#157)
265
335
 
266
336
  ## Version 2.3.4
267
337
 
@@ -301,7 +371,7 @@ average metric considerably.
301
371
 
302
372
  ### Version 2.3.0
303
373
 
304
- - No changes from `beta6`, distribtions are GA at DataDog so making the distribution changes GA in gem
374
+ - No changes from `beta6`, distributions are GA at DataDog so making the distribution changes GA in gem
305
375
 
306
376
  ### Version 2.3.0.beta6
307
377
 
@@ -327,7 +397,7 @@ average metric considerably.
327
397
 
328
398
  ### Version 2.3.0.beta
329
399
 
330
- - Add support for beta, datadog specifc distribution metrics
400
+ - Add support for beta, Datadog specific distribution metrics
331
401
  - Invalidate socket on connectivity issues
332
402
 
333
403
  ### Version 2.2.1
@@ -336,7 +406,7 @@ average metric considerably.
336
406
 
337
407
  ### Version 2.2.0
338
408
 
339
- - Add support for two new datadog specific metric types: events and service checks.
409
+ - Add support for two new Datadog specific metric types: events and service checks.
340
410
 
341
411
  ### Version 2.1.3
342
412
 
@@ -14,7 +14,6 @@ ENV['STATSD_ADDR'] = "#{legacy_receiver.addr[2]}:#{legacy_receiver.addr[1]}"
14
14
  ENV['STATSD_IMPLEMENTATION'] ||= 'datadog'
15
15
 
16
16
  require 'statsd-instrument'
17
- require 'statsd/instrument/client'
18
17
 
19
18
  legacy_client = StatsD
20
19
 
@@ -3,7 +3,6 @@
3
3
 
4
4
  require 'bundler/setup'
5
5
  require 'benchmark/ips'
6
- require 'logger'
7
6
 
8
7
  revision = %x(git rev-parse HEAD).rstrip
9
8
  master_revision = %x(git rev-parse origin/master).rstrip
@@ -2,15 +2,24 @@
2
2
 
3
3
  require 'socket'
4
4
  require 'logger'
5
+ require 'forwardable'
5
6
 
6
7
  # The StatsD module contains low-level metrics for collecting metrics and sending them to the backend.
7
8
  #
9
+ # @!attribute client
10
+ # @return [StatsD::Instrument::Backend] The client that will handle singleton method calls in the next
11
+ # major version of this library.
12
+ # @note This new Client implementation is intended to become the new default in
13
+ # the next major release of this library. While this class may already be functional,
14
+ # we provide no guarantees about the API and the behavior may change.
15
+ #
8
16
  # @!attribute backend
9
17
  # The backend that is being used to emit the metrics.
10
18
  # @return [StatsD::Instrument::Backend] the currently active backend. If there is no active backend
11
19
  # yet, it will call {StatsD::Instrument::Environment#default_backend} to obtain a
12
20
  # default backend for the environment.
13
21
  # @see StatsD::Instrument::Environment#default_backend
22
+ # @deprecated
14
23
  #
15
24
  # @!attribute prefix
16
25
  # The prefix to apply to metric names. This can be useful to group all the metrics
@@ -21,10 +30,12 @@ require 'logger'
21
30
  #
22
31
  # @return [String, nil] The prefix, or <tt>nil</tt> when no prefix is used
23
32
  # @see StatsD::Instrument::Metric#name
33
+ # @deprecated
24
34
  #
25
35
  # @!attribute default_sample_rate
26
36
  # The sample rate to use if the sample rate is unspecified for a metric call.
27
37
  # @return [Float] Default is 1.0.
38
+ # @deprecated
28
39
  #
29
40
  # @!attribute logger
30
41
  # The logger to use in case of any errors. The logger is also used as default logger
@@ -35,9 +46,45 @@ require 'logger'
35
46
  # @!attribute default_tags
36
47
  # The tags to apply to all metrics.
37
48
  # @return [Array<String>, Hash<String, String>, nil] The default tags, or <tt>nil</tt> when no default tags is used
49
+ # @deprecated
50
+ #
51
+ # @!attribute legacy_singleton_client
52
+ # @nodoc
53
+ # @deprecated
54
+ #
55
+ # @!attribute singleton_client
56
+ # @nodoc
57
+ # @deprecated
58
+ #
59
+ # @!method measure(name, value = nil, sample_rate: nil, tags: nil, &block)
60
+ # (see StatsD::Instrument::LegacyClient#measure)
61
+ #
62
+ # @!method increment(name, value = 1, sample_rate: nil, tags: nil)
63
+ # (see StatsD::Instrument::LegacyClient#increment)
64
+ #
65
+ # @!method gauge(name, value, sample_rate: nil, tags: nil)
66
+ # (see StatsD::Instrument::LegacyClient#gauge)
67
+ #
68
+ # @!method set(name, value, sample_rate: nil, tags: nil)
69
+ # (see StatsD::Instrument::LegacyClient#set)
70
+ #
71
+ # @!method histogram(name, value, sample_rate: nil, tags: nil)
72
+ # (see StatsD::Instrument::LegacyClient#histogram)
73
+ #
74
+ # @!method distribution(name, value = nil, sample_rate: nil, tags: nil, &block)
75
+ # (see StatsD::Instrument::LegacyClient#distribution)
76
+ #
77
+ # @!method key_value(name, value)
78
+ # (see StatsD::Instrument::LegacyClient#key_value)
79
+ #
80
+ # @!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
81
+ # (see StatsD::Instrument::LegacyClient#event)
82
+ #
83
+ # @!method service_check(name, status, tags: nil, hostname: nil, timestamp: nil, message: nil)
84
+ # (see StatsD::Instrument::LegacyClient#service_check)
38
85
  #
39
86
  # @see StatsD::Instrument <tt>StatsD::Instrument</tt> contains module to instrument
40
- # existing methods with StatsD metrics.
87
+ # existing methods with StatsD metrics
41
88
  module StatsD
42
89
  extend self
43
90
 
@@ -333,314 +380,41 @@ module StatsD
333
380
  end
334
381
  end
335
382
 
336
- attr_accessor :logger, :default_sample_rate, :prefix
337
- attr_writer :backend, :client
338
- attr_reader :default_tags
339
-
340
- def default_tags=(tags)
341
- @default_tags = StatsD::Instrument::Metric.normalize_tags(tags)
342
- end
343
-
344
- def backend
345
- @backend ||= StatsD::Instrument::Environment.default_backend
346
- end
383
+ attr_accessor :logger
384
+ attr_writer :client, :singleton_client
347
385
 
348
- def client
349
- @client ||= begin
350
- require 'statsd/instrument/client'
351
- StatsD::Instrument::Environment.from_env.default_client
352
- end
353
- end
386
+ extend Forwardable
354
387
 
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.
376
- #
377
- # @param [Float] value The measured duration in milliseconds
378
- # @return [void]
379
- #
380
- # @overload measure(key, sample_rate: nil, tags: nil, &block)
381
- # Emits a timing metric, after measuring the execution duration of the
382
- # block passed to this method.
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.
387
- def measure(
388
- key, value_arg = nil, deprecated_sample_rate_arg = nil, deprecated_tags_arg = nil,
389
- value: value_arg, sample_rate: deprecated_sample_rate_arg, tags: deprecated_tags_arg,
390
- prefix: StatsD.prefix, no_prefix: false, as_dist: false,
391
- &block
392
- )
393
- # TODO: in the next version, hardcode this to :ms when the as_dist argument is dropped.
394
- type = as_dist ? :d : :ms
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
399
- collect_metric(type, key, value, sample_rate: sample_rate, tags: tags, prefix: prefix)
400
- end
388
+ def legacy_singleton_client
389
+ StatsD::Instrument::LegacyClient.singleton
401
390
  end
402
391
 
403
- # @!method increment(name, value = 1, sample_rate: nil, tags: nil)
404
- # Emits a counter metric.
405
- #
406
- # @param key [String] The name of the metric.
407
- # @param value [Integer] The value to increment the counter by.
408
- #
409
- # You should not compensate for the sample rate using the counter increment. E.g., if
410
- # your sample rate is 0.01, you should <b>not</b> use 100 as increment to compensate for it.
411
- # The sample rate is part of the packet that is being sent to the server, and the server
412
- # should know how to handle it.
413
- #
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.
425
- # @return [void]
426
- def increment(
427
- key, value_arg = 1, deprecated_sample_rate_arg = nil, deprecated_tags_arg = nil,
428
- value: value_arg, sample_rate: deprecated_sample_rate_arg, tags: deprecated_tags_arg,
429
- prefix: StatsD.prefix, no_prefix: false
430
- )
431
- prefix = nil if no_prefix
432
- collect_metric(:c, key, value, sample_rate: sample_rate, tags: tags, prefix: prefix)
392
+ def singleton_client
393
+ @singleton_client ||= StatsD::Instrument::Environment.from_env.client
433
394
  end
434
395
 
435
- # @!method gauge(name, value, sample_rate: nil, tags: nil)
436
- #
437
- # Emits a gauge metric.
438
- #
439
- # @param key The name of the metric.
440
- # @param value [Numeric] The current value to record.
441
- # @param sample_rate (see #increment)
442
- # @param tags (see #increment)
443
- # @return [void]
444
- def gauge(
445
- key, value_arg = nil, deprecated_sample_rate_arg = nil, deprecated_tags_arg = nil,
446
- value: value_arg, sample_rate: deprecated_sample_rate_arg, tags: deprecated_tags_arg,
447
- prefix: StatsD.prefix, no_prefix: false
448
- )
449
- prefix = nil if no_prefix
450
- collect_metric(:g, key, value, sample_rate: sample_rate, tags: tags, prefix: prefix)
451
- end
452
-
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
- #
460
- # @param key [String] The name of the metric.
461
- # @param value [Numeric] The value to record.
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)
482
- # @return (see #collect_metric)
483
- # @note Supported by the datadog implementation only.
484
- def histogram(
485
- key, value_arg = nil, deprecated_sample_rate_arg = nil, deprecated_tags_arg = nil,
486
- value: value_arg, sample_rate: deprecated_sample_rate_arg, tags: deprecated_tags_arg,
487
- prefix: StatsD.prefix, no_prefix: false
488
- )
489
- prefix = nil if no_prefix
490
- collect_metric(:h, key, value, sample_rate: sample_rate, tags: tags, prefix: prefix)
491
- end
492
-
493
- # @!method distribution(name, value = nil, sample_rate: nil, tags: nil, &block)
494
- #
495
- # Emits a distribution metric.
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]
513
- #
514
- # @overload distribution(key, metric_options = {}, &block)
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.
521
- def distribution(
522
- key, value_arg = nil, deprecated_sample_rate_arg = nil, deprecated_tags_arg = nil,
523
- value: value_arg, sample_rate: deprecated_sample_rate_arg, tags: deprecated_tags_arg,
524
- prefix: StatsD.prefix, no_prefix: false,
525
- &block
526
- )
527
- prefix = nil if no_prefix
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
533
- end
534
-
535
- # @!method key_value(name, value)
536
- #
537
- # Emits a key/value metric.
538
- #
539
- # @param key [String] The name of the metric.
540
- # @param value [Numeric] The value to record.
541
- # @return [void]
542
- #
543
- # @note Supported by the statsite implementation only.
544
- def key_value(
545
- key, value_arg = nil, deprecated_sample_rate_arg = nil,
546
- value: value_arg, sample_rate: deprecated_sample_rate_arg, no_prefix: false
547
- )
548
- prefix = nil if no_prefix
549
- collect_metric(:kv, key, value, sample_rate: sample_rate, prefix: prefix)
550
- end
551
-
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.
568
- def event(
569
- title, text,
570
- deprecated_sample_rate_arg = nil, deprecated_tags_arg = nil,
571
- sample_rate: deprecated_sample_rate_arg, tags: deprecated_tags_arg,
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
576
- )
577
- prefix = nil if no_prefix
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
- })
582
- end
583
-
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.
597
- def service_check(
598
- name, status,
599
- deprecated_sample_rate_arg = nil, deprecated_tags_arg = nil,
600
- sample_rate: deprecated_sample_rate_arg, tags: deprecated_tags_arg,
601
- prefix: StatsD.prefix, no_prefix: false,
602
- hostname: nil, timestamp: nil, message: nil, **_ignored
603
- )
604
- prefix = nil if no_prefix
605
- collect_metric(:_sc, name, status, sample_rate: sample_rate, prefix: prefix, tags: tags, metadata: {
606
- hostname: hostname, timestamp: timestamp, message: message
607
- })
396
+ def client
397
+ @client ||= StatsD::Instrument::Environment.from_env.default_client
608
398
  end
609
399
 
610
- private
400
+ # Singleton methods will be delegated to the singleton client.
401
+ def_delegators :singleton_client, :increment, :gauge, :set, :measure,
402
+ :histogram, :distribution, :key_value, :event, :service_check
611
403
 
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
-
623
- # Instantiates a metric, and sends it to the backend for further processing.
624
- # @param options (see StatsD::Instrument::Metric#initialize)
625
- # @return [void]
626
- def collect_metric(type, name, value, sample_rate:, tags: nil, prefix:, metadata: nil)
627
- sample_rate ||= default_sample_rate
628
- name = "#{prefix}.#{name}" if prefix
629
-
630
- metric = StatsD::Instrument::Metric.new(type: type, name: name, value: value,
631
- sample_rate: sample_rate, tags: tags, metadata: metadata)
632
- backend.collect_metric(metric)
633
- metric # TODO: return `nil` in the next major version
634
- end
404
+ # Deprecated methods will be delegated to the legacy client
405
+ def_delegators :legacy_singleton_client, :default_tags, :default_tags=,
406
+ :default_sample_rate, :default_sample_rate=, :prefix, :prefix=, :backend, :backend=
635
407
  end
636
408
 
637
409
  require 'statsd/instrument/version'
638
410
  require 'statsd/instrument/metric'
411
+ require 'statsd/instrument/legacy_client'
639
412
  require 'statsd/instrument/backend'
413
+ require 'statsd/instrument/client'
640
414
  require 'statsd/instrument/environment'
641
415
  require 'statsd/instrument/helpers'
642
416
  require 'statsd/instrument/assertions'
643
- require 'statsd/instrument/metric_expectation'
417
+ require 'statsd/instrument/expectation'
644
418
  require 'statsd/instrument/matchers' if defined?(::RSpec)
645
419
  require 'statsd/instrument/railtie' if defined?(::Rails::Railtie)
646
420
  require 'statsd/instrument/strict' if ENV['STATSD_STRICT_MODE']