statsd-instrument 2.9.2 → 3.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +16 -23
- data/.rubocop.yml +3 -13
- data/CHANGELOG.md +33 -0
- data/Gemfile +8 -0
- data/README.md +3 -3
- data/Rakefile +1 -1
- data/benchmark/send-metrics-to-dev-null-log +5 -2
- data/benchmark/send-metrics-to-local-udp-receiver +8 -6
- data/bin/rake +29 -0
- data/bin/rubocop +29 -0
- data/lib/statsd/instrument.rb +80 -144
- data/lib/statsd/instrument/assertions.rb +200 -208
- data/lib/statsd/instrument/capture_sink.rb +23 -19
- data/lib/statsd/instrument/client.rb +414 -320
- data/lib/statsd/instrument/datagram.rb +69 -65
- data/lib/statsd/instrument/datagram_builder.rb +81 -77
- data/lib/statsd/instrument/dogstatsd_datagram.rb +76 -72
- data/lib/statsd/instrument/dogstatsd_datagram_builder.rb +68 -64
- data/lib/statsd/instrument/environment.rb +79 -98
- data/lib/statsd/instrument/expectation.rb +96 -96
- data/lib/statsd/instrument/helpers.rb +10 -35
- data/lib/statsd/instrument/log_sink.rb +20 -16
- data/lib/statsd/instrument/matchers.rb +86 -71
- data/lib/statsd/instrument/null_sink.rb +12 -8
- data/lib/statsd/instrument/railtie.rb +11 -11
- data/lib/statsd/instrument/statsd_datagram_builder.rb +12 -8
- data/lib/statsd/instrument/strict.rb +12 -123
- data/lib/statsd/instrument/udp_sink.rb +50 -46
- data/lib/statsd/instrument/version.rb +1 -1
- data/statsd-instrument.gemspec +2 -8
- data/test/assertions_test.rb +46 -12
- data/test/capture_sink_test.rb +8 -8
- data/test/client_test.rb +62 -51
- data/test/datagram_builder_test.rb +29 -29
- data/test/datagram_test.rb +1 -1
- data/test/dogstatsd_datagram_builder_test.rb +28 -28
- data/test/environment_test.rb +10 -46
- data/test/helpers/rubocop_helper.rb +11 -8
- data/test/helpers_test.rb +5 -5
- data/test/integration_test.rb +10 -25
- data/test/log_sink_test.rb +2 -2
- data/test/matchers_test.rb +36 -36
- data/test/null_sink_test.rb +2 -2
- data/test/rubocop/metric_return_value_test.rb +3 -3
- data/test/rubocop/metric_value_keyword_argument_test.rb +1 -1
- data/test/rubocop/positional_arguments_test.rb +10 -10
- data/test/statsd_instrumentation_test.rb +97 -122
- data/test/statsd_test.rb +50 -75
- data/test/test_helper.rb +7 -5
- data/test/udp_sink_test.rb +8 -8
- metadata +7 -125
- data/.rubocop-https---shopify-github-io-ruby-style-guide-rubocop-yml +0 -1027
- data/benchmark/datagram-client +0 -40
- data/lib/statsd/instrument/backend.rb +0 -18
- data/lib/statsd/instrument/backends/capture_backend.rb +0 -32
- data/lib/statsd/instrument/backends/logger_backend.rb +0 -20
- data/lib/statsd/instrument/backends/null_backend.rb +0 -9
- data/lib/statsd/instrument/backends/udp_backend.rb +0 -152
- data/lib/statsd/instrument/legacy_client.rb +0 -301
- data/lib/statsd/instrument/metric.rb +0 -155
- data/test/assertions_on_legacy_client_test.rb +0 -344
- data/test/capture_backend_test.rb +0 -26
- data/test/compatibility/dogstatsd_datagram_compatibility_test.rb +0 -161
- data/test/deprecations_test.rb +0 -139
- data/test/logger_backend_test.rb +0 -22
- data/test/metric_test.rb +0 -47
- data/test/udp_backend_test.rb +0 -228
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 032db29a498269753044a778380369fc44d3f87b3143e051df69f1d8a480595a
|
4
|
+
data.tar.gz: 10d0ccc0cb4039d8846862e82283de004a7fb2b770648fb165281dd5e2d5f68c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 61b4c7cf6c84144767201dea7491cfa217dbd48b45821e6f1173eac8d45de6c9ddd9239ce50491839425d8cbe672f69c9ebfc9263916ec2b8a027fab43824f41
|
7
|
+
data.tar.gz: e726adb888e41bdfc8c9049c36b3df9b0f6ee2328a7a9e04f871652483dd57ade4ec04bd11e7010b275d77c733917c10441481c588c58bc9cb573b9364648dac
|
data/.github/workflows/ci.yml
CHANGED
@@ -4,24 +4,17 @@ on: push
|
|
4
4
|
|
5
5
|
jobs:
|
6
6
|
test:
|
7
|
-
name: Ruby ${{ matrix.ruby }} on
|
8
|
-
runs-on:
|
7
|
+
name: Ruby ${{ matrix.ruby }} on ubuntu-18.04
|
8
|
+
runs-on: ubuntu-18.04
|
9
9
|
strategy:
|
10
10
|
fail-fast: false
|
11
11
|
matrix:
|
12
|
-
# Windows started failing
|
13
|
-
|
14
|
-
|
15
|
-
#
|
16
|
-
|
17
|
-
#
|
18
|
-
# Temporarily disable Windows builds to unblock CI.
|
19
|
-
#
|
20
|
-
# platform: [ubuntu-18.04, windows-2019, macOS-10.14]
|
21
|
-
platform: [ubuntu-18.04, macOS-10.14]
|
22
|
-
ruby: [2.3, 2.4, 2.5, 2.6]
|
23
|
-
|
24
|
-
exclude:
|
12
|
+
# Windows on macOS builds started failing, so they are disabled for noew
|
13
|
+
|
14
|
+
ruby: [2.4, 2.5, 2.6, 2.7]
|
15
|
+
# platform: [windows-2019, macOS-10.14, ubuntu-18.04]
|
16
|
+
|
17
|
+
# exclude:
|
25
18
|
# The Windows environment does not support older Ruby versions. We only test against the latest version
|
26
19
|
# - platform: windows-2019
|
27
20
|
# ruby: 2.3
|
@@ -31,12 +24,12 @@ jobs:
|
|
31
24
|
# ruby: 2.5
|
32
25
|
|
33
26
|
# On macOS, we only test against the Ruby version macOS ships with (2.3)
|
34
|
-
- platform: macOS-10.14
|
35
|
-
|
36
|
-
- platform: macOS-10.14
|
37
|
-
|
38
|
-
- platform: macOS-10.14
|
39
|
-
|
27
|
+
# - platform: macOS-10.14
|
28
|
+
# ruby: 2.4
|
29
|
+
# - platform: macOS-10.14
|
30
|
+
# ruby: 2.5
|
31
|
+
# - platform: macOS-10.14
|
32
|
+
# ruby: 2.6
|
40
33
|
|
41
34
|
steps:
|
42
35
|
- uses: actions/checkout@v1
|
@@ -50,7 +43,7 @@ jobs:
|
|
50
43
|
run: gem install bundler && bundle install --jobs 4 --retry 3
|
51
44
|
|
52
45
|
- name: Run test suite
|
53
|
-
run:
|
46
|
+
run: rake test
|
54
47
|
|
55
48
|
- name: Run Rubocop
|
56
|
-
run:
|
49
|
+
run: bin/rubocop
|
data/.rubocop.yml
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
|
2
|
-
-
|
1
|
+
inherit_gem:
|
2
|
+
rubocop-shopify: rubocop.yml
|
3
3
|
|
4
4
|
require:
|
5
5
|
- ./lib/statsd/instrument/rubocop.rb
|
6
6
|
|
7
7
|
AllCops:
|
8
|
-
TargetRubyVersion: 2.
|
8
|
+
TargetRubyVersion: 2.4
|
9
9
|
UseCache: true
|
10
10
|
CacheRootDirectory: tmp/rubocop
|
11
11
|
Exclude:
|
@@ -16,16 +16,6 @@ Naming/FileName:
|
|
16
16
|
Exclude:
|
17
17
|
- lib/statsd-instrument.rb
|
18
18
|
|
19
|
-
Style/ClassAndModuleChildren:
|
20
|
-
Enabled: false # TODO: enable later
|
21
|
-
|
22
|
-
|
23
|
-
Style/MethodCallWithArgsParentheses:
|
24
|
-
Enabled: false # TODO: enable later
|
25
|
-
|
26
|
-
Lint/UnusedMethodArgument:
|
27
|
-
AllowUnusedKeywordArguments: true
|
28
|
-
|
29
19
|
# Enable our own cops on our own repo
|
30
20
|
|
31
21
|
StatsD/MetricReturnValue:
|
data/CHANGELOG.md
CHANGED
@@ -8,6 +8,39 @@ section below.
|
|
8
8
|
|
9
9
|
_Nothing yet_
|
10
10
|
|
11
|
+
## Version 3.0.2
|
12
|
+
|
13
|
+
- Properly handle no_prefix when using StatsD assertions.
|
14
|
+
|
15
|
+
## Version 3.0.1
|
16
|
+
|
17
|
+
- Fix metaprograming methods to not print keyword argument warnings on
|
18
|
+
Ruby 2.7.
|
19
|
+
- Fix the gemspec to no longer register `rake` and `rubocop` as executables.
|
20
|
+
|
21
|
+
## Version 3.0.0
|
22
|
+
|
23
|
+
This version makes the new client that was added in version 2.6+ the default
|
24
|
+
client, and removes the legacy client.
|
25
|
+
|
26
|
+
- All previously deprecated functionality has been removed (since version 2.5,
|
27
|
+
see below).
|
28
|
+
- Support for the StatSite implementation has been dropped.
|
29
|
+
- Support for Ruby version older than 2.4 has been dropped.
|
30
|
+
- The default implementation has been changed to DataDog. To use the standard
|
31
|
+
StatsD implementation (which was the default in v2), set the
|
32
|
+
`STATSD_IMPLEMENTATION` environment variable to `statsd`.
|
33
|
+
|
34
|
+
To upgrade, follow the following process:
|
35
|
+
|
36
|
+
1. Upgrade to version 2.9.2.
|
37
|
+
2. Switch to the new client by setting the `STATSD_USE_NEW_CLIENT` environment
|
38
|
+
variable to 1.
|
39
|
+
- You may want to use the Rubocop rules that ship with this library, and
|
40
|
+
strict mode to find and fix deprecated usage patterns. See below for more
|
41
|
+
information about strict mode and the available Rubocop rules.
|
42
|
+
3. Upgrade to version 3.0.0, and unset `STATSD_USE_NEW_CLIENT`.
|
43
|
+
|
11
44
|
## Version 2.9.2
|
12
45
|
|
13
46
|
- Allow providing a value as second positional argument to `assert_statsd_*`
|
data/Gemfile
CHANGED
@@ -3,5 +3,13 @@
|
|
3
3
|
source "https://rubygems.org"
|
4
4
|
gemspec
|
5
5
|
|
6
|
+
gem 'rake'
|
7
|
+
gem 'minitest'
|
8
|
+
gem 'rspec'
|
9
|
+
gem 'mocha'
|
10
|
+
gem 'yard'
|
11
|
+
gem 'rubocop', '>= 1.0'
|
12
|
+
gem 'rubocop-shopify', require: false
|
13
|
+
|
6
14
|
# benchmark-ips save! method is not part of a released version yet.
|
7
15
|
gem 'benchmark-ips', git: 'https://github.com/evanphx/benchmark-ips', branch: 'master'
|
data/README.md
CHANGED
@@ -20,7 +20,7 @@ The following environment variables are supported:
|
|
20
20
|
|
21
21
|
- `STATSD_ADDR`: (default `localhost:8125`) The address to send the StatsD UDP
|
22
22
|
datagrams to.
|
23
|
-
- `STATSD_IMPLEMENTATION`: (default: `
|
23
|
+
- `STATSD_IMPLEMENTATION`: (default: `datadog`). The StatsD implementation you
|
24
24
|
are using. `statsd`, `statsite` and `datadog` are supported. Some features
|
25
25
|
are only available on certain implementations,
|
26
26
|
- `STATSD_ENV`: The environment StatsD will run in. If this is not set
|
@@ -105,7 +105,7 @@ Because you are counting unique values, the results of using a sampling value le
|
|
105
105
|
Builds a histogram of numeric values.
|
106
106
|
``` ruby
|
107
107
|
|
108
|
-
StatsD.histogram('Order.value', order.value_in_usd.to_f tags: { source: 'POS' })
|
108
|
+
StatsD.histogram('Order.value', order.value_in_usd.to_f, tags: { source: 'POS' })
|
109
109
|
```
|
110
110
|
|
111
111
|
Because you are counting unique values, the results of using a sampling value less than 1.0 can lead to unexpected, hard to interpret results.
|
@@ -128,7 +128,7 @@ An event is a (title, text) tuple that can be used to correlate metrics with som
|
|
128
128
|
This is a good fit for instance to correlate response time variation with a deploy of the new code.
|
129
129
|
|
130
130
|
```ruby
|
131
|
-
StatsD.event('shipit.deploy', 'started'
|
131
|
+
StatsD.event('shipit.deploy', 'started')
|
132
132
|
```
|
133
133
|
|
134
134
|
*Note: This is only supported by the [datadog implementation](https://docs.datadoghq.com/guides/dogstatsd/#events).*
|
data/Rakefile
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
4
|
require 'bundler/setup'
|
5
|
+
require 'tmpdir'
|
5
6
|
require 'benchmark/ips'
|
6
7
|
|
7
8
|
revision = %x(git rev-parse HEAD).rstrip
|
@@ -25,8 +26,10 @@ report = Benchmark.ips do |bench|
|
|
25
26
|
StatsD.measure('StatsD.measure') { 1 + 1 }
|
26
27
|
StatsD.gauge('StatsD.gauge', 12.0, tags: ["foo:bar", "quc"])
|
27
28
|
StatsD.set('StatsD.set', 'value', tags: { foo: 'bar', baz: 'quc' })
|
28
|
-
StatsD.
|
29
|
-
|
29
|
+
if StatsD.singleton_client.datagram_builder_class == StatsD::Instrument::DogStatsDDatagramBuilder
|
30
|
+
StatsD.event('StasD.event', "12345")
|
31
|
+
StatsD.service_check("StatsD.service_check", "ok")
|
32
|
+
end
|
30
33
|
end
|
31
34
|
|
32
35
|
# Store the results in between runs
|
@@ -3,7 +3,9 @@
|
|
3
3
|
|
4
4
|
require 'bundler/setup'
|
5
5
|
require 'benchmark/ips'
|
6
|
+
require 'tmpdir'
|
6
7
|
require 'socket'
|
8
|
+
require 'statsd-instrument'
|
7
9
|
|
8
10
|
revision = %x(git rev-parse HEAD).rstrip
|
9
11
|
master_revision = %x(git rev-parse origin/master).rstrip
|
@@ -20,11 +22,11 @@ FileUtils.mkdir_p(File.dirname(intermediate_results_filename))
|
|
20
22
|
receiver = UDPSocket.new
|
21
23
|
receiver.bind('localhost', 0)
|
22
24
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
StatsD.singleton_client = StatsD::Instrument::Environment.new(
|
26
|
+
'STATSD_ADDR' => "#{receiver.addr[2]}:#{receiver.addr[1]}",
|
27
|
+
'STATSD_IMPLEMENTATION' => 'dogstatsd',
|
28
|
+
'STATSD_ENV' => 'production',
|
29
|
+
).client
|
28
30
|
|
29
31
|
report = Benchmark.ips do |bench|
|
30
32
|
bench.report("StatsD metrics to local UDP receiver (branch: #{branch}, sha: #{revision[0, 7]})") do
|
@@ -32,7 +34,7 @@ report = Benchmark.ips do |bench|
|
|
32
34
|
StatsD.measure('StatsD.measure') { 1 + 1 }
|
33
35
|
StatsD.gauge('StatsD.gauge', 12.0, tags: ["foo:bar", "quc"])
|
34
36
|
StatsD.set('StatsD.set', 'value', tags: { foo: 'bar', baz: 'quc' })
|
35
|
-
if StatsD.
|
37
|
+
if StatsD.singleton_client.datagram_builder_class == StatsD::Instrument::DogStatsDDatagramBuilder
|
36
38
|
StatsD.event('StasD.event', "12345")
|
37
39
|
StatsD.service_check("StatsD.service_check", "ok")
|
38
40
|
end
|
data/bin/rake
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
#
|
5
|
+
# This file was generated by Bundler.
|
6
|
+
#
|
7
|
+
# The application 'rake' is installed as part of a gem, and
|
8
|
+
# this file is here to facilitate running it.
|
9
|
+
#
|
10
|
+
|
11
|
+
require "pathname"
|
12
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
13
|
+
Pathname.new(__FILE__).realpath)
|
14
|
+
|
15
|
+
bundle_binstub = File.expand_path("../bundle", __FILE__)
|
16
|
+
|
17
|
+
if File.file?(bundle_binstub)
|
18
|
+
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
|
19
|
+
load(bundle_binstub)
|
20
|
+
else
|
21
|
+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
|
22
|
+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
require "rubygems"
|
27
|
+
require "bundler/setup"
|
28
|
+
|
29
|
+
load(Gem.bin_path("rake", "rake"))
|
data/bin/rubocop
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
#
|
5
|
+
# This file was generated by Bundler.
|
6
|
+
#
|
7
|
+
# The application 'rubocop' is installed as part of a gem, and
|
8
|
+
# this file is here to facilitate running it.
|
9
|
+
#
|
10
|
+
|
11
|
+
require "pathname"
|
12
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
13
|
+
Pathname.new(__FILE__).realpath)
|
14
|
+
|
15
|
+
bundle_binstub = File.expand_path("../bundle", __FILE__)
|
16
|
+
|
17
|
+
if File.file?(bundle_binstub)
|
18
|
+
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
|
19
|
+
load(bundle_binstub)
|
20
|
+
else
|
21
|
+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
|
22
|
+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
require "rubygems"
|
27
|
+
require "bundler/setup"
|
28
|
+
|
29
|
+
load(Gem.bin_path("rubocop", "rubocop"))
|
data/lib/statsd/instrument.rb
CHANGED
@@ -4,90 +4,14 @@ require 'socket'
|
|
4
4
|
require 'logger'
|
5
5
|
require 'forwardable'
|
6
6
|
|
7
|
-
# The StatsD module contains low-level metrics for collecting metrics and
|
7
|
+
# The `StatsD` module contains low-level metrics for collecting metrics and
|
8
|
+
# sending them to the backend.
|
8
9
|
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
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
|
-
#
|
16
|
-
# @!attribute backend
|
17
|
-
# The backend that is being used to emit the metrics.
|
18
|
-
# @return [StatsD::Instrument::Backend] the currently active backend. If there is no active backend
|
19
|
-
# yet, it will call {StatsD::Instrument::Environment#default_backend} to obtain a
|
20
|
-
# default backend for the environment.
|
21
|
-
# @see StatsD::Instrument::Environment#default_backend
|
22
|
-
# @deprecated
|
23
|
-
#
|
24
|
-
# @!attribute prefix
|
25
|
-
# The prefix to apply to metric names. This can be useful to group all the metrics
|
26
|
-
# for an application in a shared StatsD server.
|
27
|
-
#
|
28
|
-
# When using a prefix a dot will be included automatically to separate the prefix
|
29
|
-
# from the metric name.
|
30
|
-
#
|
31
|
-
# @return [String, nil] The prefix, or <tt>nil</tt> when no prefix is used
|
32
|
-
# @see StatsD::Instrument::Metric#name
|
33
|
-
# @deprecated
|
34
|
-
#
|
35
|
-
# @!attribute default_sample_rate
|
36
|
-
# The sample rate to use if the sample rate is unspecified for a metric call.
|
37
|
-
# @return [Float] Default is 1.0.
|
38
|
-
# @deprecated
|
39
|
-
#
|
40
|
-
# @!attribute logger
|
41
|
-
# The logger to use in case of any errors. The logger is also used as default logger
|
42
|
-
# for the LoggerBackend (although this can be overwritten).
|
43
|
-
# @see StatsD::Instrument::Backends::LoggerBackend
|
44
|
-
# @return [Logger]
|
45
|
-
#
|
46
|
-
# @!attribute default_tags
|
47
|
-
# The tags to apply to all metrics.
|
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)
|
85
|
-
#
|
86
|
-
# @see StatsD::Instrument <tt>StatsD::Instrument</tt> contains module to instrument
|
10
|
+
# @see .singleton_client Metric method calls on the `StatsD` singleton will
|
11
|
+
# be handled by the client assigned to `StatsD.singleton_client`.
|
12
|
+
# @see StatsD::Instrument `StatsD::Instrument` contains module to instrument
|
87
13
|
# existing methods with StatsD metrics
|
88
14
|
module StatsD
|
89
|
-
extend self
|
90
|
-
|
91
15
|
# The StatsD::Instrument module provides metaprogramming methods to instrument your methods with
|
92
16
|
# StatsD metrics. E.g., you can create counters on how often a method is called, how often it is
|
93
17
|
# successful, the duration of the methods call, etc.
|
@@ -103,11 +27,11 @@ module StatsD
|
|
103
27
|
end
|
104
28
|
end
|
105
29
|
|
30
|
+
# Generates a metric name for an instrumented method.
|
106
31
|
# @private
|
107
|
-
|
108
|
-
|
109
|
-
name
|
110
|
-
name
|
32
|
+
# @return [String]
|
33
|
+
def self.generate_metric_name(name, callee, *args)
|
34
|
+
name.respond_to?(:call) ? name.call(callee, args).gsub('::', '.') : name.gsub('::', '.')
|
111
35
|
end
|
112
36
|
|
113
37
|
# Even though this method is considered private, and is no longer used internally,
|
@@ -139,21 +63,12 @@ module StatsD
|
|
139
63
|
# callable to dynamically generate a metric name
|
140
64
|
# @param metric_options (see StatsD#measure)
|
141
65
|
# @return [void]
|
142
|
-
def statsd_measure(method, name,
|
143
|
-
as_dist: false, sample_rate: deprecated_sample_rate_arg, tags: deprecated_tags_arg,
|
144
|
-
prefix: nil, no_prefix: false, client: nil)
|
145
|
-
|
146
|
-
if as_dist
|
147
|
-
return statsd_distribution(method, name, # rubocop:disable StatsD/MetricPrefixArgument
|
148
|
-
sample_rate: sample_rate, tags: tags, prefix: prefix, no_prefix: no_prefix)
|
149
|
-
end
|
150
|
-
|
66
|
+
def statsd_measure(method, name, sample_rate: nil, tags: nil, no_prefix: false, client: nil)
|
151
67
|
add_to_method(method, name, :measure) do
|
152
68
|
define_method(method) do |*args, &block|
|
153
69
|
client ||= StatsD.singleton_client
|
154
|
-
|
155
|
-
|
156
|
-
client.measure(key, sample_rate: sample_rate, tags: tags, no_prefix: true) do
|
70
|
+
key = StatsD::Instrument.generate_metric_name(name, self, *args)
|
71
|
+
client.measure(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix) do
|
157
72
|
super(*args, &block)
|
158
73
|
end
|
159
74
|
end
|
@@ -168,16 +83,12 @@ module StatsD
|
|
168
83
|
# @param metric_options (see StatsD#measure)
|
169
84
|
# @return [void]
|
170
85
|
# @note Supported by the datadog implementation only (in beta)
|
171
|
-
def statsd_distribution(method, name,
|
172
|
-
sample_rate: deprecated_sample_rate_arg, tags: deprecated_tags_arg,
|
173
|
-
prefix: nil, no_prefix: false, client: nil)
|
174
|
-
|
86
|
+
def statsd_distribution(method, name, sample_rate: nil, tags: nil, no_prefix: false, client: nil)
|
175
87
|
add_to_method(method, name, :distribution) do
|
176
88
|
define_method(method) do |*args, &block|
|
177
89
|
client ||= StatsD.singleton_client
|
178
|
-
|
179
|
-
|
180
|
-
client.distribution(key, sample_rate: sample_rate, tags: tags, no_prefix: true) do
|
90
|
+
key = StatsD::Instrument.generate_metric_name(name, self, *args)
|
91
|
+
client.distribution(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix) do
|
181
92
|
super(*args, &block)
|
182
93
|
end
|
183
94
|
end
|
@@ -199,10 +110,7 @@ module StatsD
|
|
199
110
|
# @yieldreturn [Boolean] Return true iff the return value is considered a success, false otherwise.
|
200
111
|
# @return [void]
|
201
112
|
# @see #statsd_count_if
|
202
|
-
def statsd_count_success(method, name,
|
203
|
-
sample_rate: deprecated_sample_rate_arg, tags: deprecated_tags_arg,
|
204
|
-
prefix: nil, no_prefix: false, client: nil)
|
205
|
-
|
113
|
+
def statsd_count_success(method, name, sample_rate: nil, tags: nil, no_prefix: false, client: nil)
|
206
114
|
add_to_method(method, name, :count_success) do
|
207
115
|
define_method(method) do |*args, &block|
|
208
116
|
begin
|
@@ -222,10 +130,8 @@ module StatsD
|
|
222
130
|
ensure
|
223
131
|
client ||= StatsD.singleton_client
|
224
132
|
suffix = truthiness == false ? 'failure' : 'success'
|
225
|
-
|
226
|
-
|
227
|
-
client.increment("#{key}.#{suffix}",
|
228
|
-
sample_rate: sample_rate, tags: tags, no_prefix: true)
|
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)
|
229
135
|
end
|
230
136
|
end
|
231
137
|
end
|
@@ -243,10 +149,7 @@ module StatsD
|
|
243
149
|
# @yieldreturn (see #statsd_count_success)
|
244
150
|
# @return [void]
|
245
151
|
# @see #statsd_count_success
|
246
|
-
def statsd_count_if(method, name,
|
247
|
-
sample_rate: deprecated_sample_rate_arg, tags: deprecated_tags_arg,
|
248
|
-
prefix: nil, no_prefix: false, client: nil)
|
249
|
-
|
152
|
+
def statsd_count_if(method, name, sample_rate: nil, tags: nil, no_prefix: false, client: nil)
|
250
153
|
add_to_method(method, name, :count_if) do
|
251
154
|
define_method(method) do |*args, &block|
|
252
155
|
begin
|
@@ -266,9 +169,8 @@ module StatsD
|
|
266
169
|
ensure
|
267
170
|
if truthiness
|
268
171
|
client ||= StatsD.singleton_client
|
269
|
-
|
270
|
-
|
271
|
-
client.increment(key, sample_rate: sample_rate, tags: tags, no_prefix: true)
|
172
|
+
key = StatsD::Instrument.generate_metric_name(name, self, *args)
|
173
|
+
client.increment(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix)
|
272
174
|
end
|
273
175
|
end
|
274
176
|
end
|
@@ -284,16 +186,12 @@ module StatsD
|
|
284
186
|
# @param name (see #statsd_measure)
|
285
187
|
# @param metric_options (see #statsd_measure)
|
286
188
|
# @return [void]
|
287
|
-
def statsd_count(method, name,
|
288
|
-
sample_rate: deprecated_sample_rate_arg, tags: deprecated_tags_arg,
|
289
|
-
prefix: nil, no_prefix: false, client: nil)
|
290
|
-
|
189
|
+
def statsd_count(method, name, sample_rate: nil, tags: nil, no_prefix: false, client: nil)
|
291
190
|
add_to_method(method, name, :count) do
|
292
191
|
define_method(method) do |*args, &block|
|
293
192
|
client ||= StatsD.singleton_client
|
294
|
-
|
295
|
-
|
296
|
-
client.increment(key, sample_rate: sample_rate, tags: tags, no_prefix: true)
|
193
|
+
key = StatsD::Instrument.generate_metric_name(name, self, *args)
|
194
|
+
client.increment(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix)
|
297
195
|
super(*args, &block)
|
298
196
|
end
|
299
197
|
end
|
@@ -377,6 +275,9 @@ module StatsD
|
|
377
275
|
|
378
276
|
instrumentation_module.module_eval(&block)
|
379
277
|
instrumentation_module.send(method_scope, method)
|
278
|
+
if instrumentation_module.respond_to?(:ruby2_keywords, true)
|
279
|
+
instrumentation_module.send(:ruby2_keywords, method)
|
280
|
+
end
|
380
281
|
prepend(instrumentation_module) unless self < instrumentation_module
|
381
282
|
end
|
382
283
|
|
@@ -395,33 +296,68 @@ module StatsD
|
|
395
296
|
end
|
396
297
|
end
|
397
298
|
|
398
|
-
|
399
|
-
|
299
|
+
class << self
|
300
|
+
extend Forwardable
|
400
301
|
|
401
|
-
|
302
|
+
# The logger to use in case of any errors.
|
303
|
+
#
|
304
|
+
# @return [Logger]
|
305
|
+
# @see StatsD::Instrument::LogSink
|
306
|
+
attr_accessor :logger
|
402
307
|
|
403
|
-
|
404
|
-
|
405
|
-
|
308
|
+
# The StatsD client that handles method calls on the StatsD singleton.
|
309
|
+
#
|
310
|
+
# E.g. a call to `StatsD.increment` will be handled by this client.
|
311
|
+
#
|
312
|
+
# @return [StatsD::Instrument::Client]
|
313
|
+
attr_writer :singleton_client
|
406
314
|
|
407
|
-
|
408
|
-
@
|
409
|
-
|
315
|
+
# The StatsD client that handles method calls on the StatsD singleton
|
316
|
+
# @return [StatsD::Instrument::Client]
|
317
|
+
def singleton_client
|
318
|
+
@singleton_client ||= StatsD::Instrument::Environment.current.client
|
319
|
+
end
|
410
320
|
|
411
|
-
|
412
|
-
|
413
|
-
|
321
|
+
# @!method measure(name, value = nil, sample_rate: nil, tags: nil, &block)
|
322
|
+
# (see StatsD::Instrument::Client#measure)
|
323
|
+
#
|
324
|
+
# @!method increment(name, value = 1, sample_rate: nil, tags: nil)
|
325
|
+
# (see StatsD::Instrument::Client#increment)
|
326
|
+
#
|
327
|
+
# @!method gauge(name, value, sample_rate: nil, tags: nil)
|
328
|
+
# (see StatsD::Instrument::Client#gauge)
|
329
|
+
#
|
330
|
+
# @!method set(name, value, sample_rate: nil, tags: nil)
|
331
|
+
# (see StatsD::Instrument::Client#set)
|
332
|
+
#
|
333
|
+
# @!method histogram(name, value, sample_rate: nil, tags: nil)
|
334
|
+
# (see StatsD::Instrument::Client#histogram)
|
335
|
+
#
|
336
|
+
# @!method distribution(name, value = nil, sample_rate: nil, tags: nil, &block)
|
337
|
+
# (see StatsD::Instrument::Client#distribution)
|
338
|
+
#
|
339
|
+
# @!method event(title, text, tags: nil, hostname: nil, timestamp: nil, aggregation_key: nil, priority: nil, source_type_name: nil, alert_type: nil) # rubocop:disable Layout/LineLength
|
340
|
+
# (see StatsD::Instrument::Client#event)
|
341
|
+
#
|
342
|
+
# @!method service_check(name, status, tags: nil, hostname: nil, timestamp: nil, message: nil)
|
343
|
+
# (see StatsD::Instrument::Client#service_check)
|
414
344
|
|
415
|
-
|
416
|
-
|
417
|
-
|
345
|
+
def_delegators :singleton_client, :increment, :gauge, :set, :measure,
|
346
|
+
:histogram, :distribution, :event, :service_check
|
347
|
+
end
|
418
348
|
end
|
419
349
|
|
420
350
|
require 'statsd/instrument/version'
|
421
|
-
require 'statsd/instrument/metric'
|
422
|
-
require 'statsd/instrument/legacy_client'
|
423
|
-
require 'statsd/instrument/backend'
|
424
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'
|
425
361
|
require 'statsd/instrument/environment'
|
426
362
|
require 'statsd/instrument/helpers'
|
427
363
|
require 'statsd/instrument/assertions'
|