statsd-instrument 2.9.0 → 3.0.0
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 +14 -21
- data/.rubocop-https---shopify-github-io-ruby-style-guide-rubocop-yml +15 -15
- data/CHANGELOG.md +44 -0
- data/benchmark/send-metrics-to-dev-null-log +5 -2
- data/benchmark/send-metrics-to-local-udp-receiver +8 -6
- data/lib/statsd/instrument.rb +79 -146
- data/lib/statsd/instrument/assertions.rb +12 -24
- data/lib/statsd/instrument/client.rb +125 -35
- data/lib/statsd/instrument/environment.rb +1 -23
- data/lib/statsd/instrument/expectation.rb +12 -16
- data/lib/statsd/instrument/helpers.rb +1 -30
- data/lib/statsd/instrument/matchers.rb +0 -1
- data/lib/statsd/instrument/railtie.rb +0 -4
- data/lib/statsd/instrument/strict.rb +12 -123
- data/lib/statsd/instrument/version.rb +1 -1
- data/test/assertions_test.rb +21 -9
- data/test/client_test.rb +11 -0
- data/test/environment_test.rb +1 -37
- data/test/integration_test.rb +9 -24
- data/test/statsd_instrumentation_test.rb +25 -50
- data/test/statsd_test.rb +6 -31
- data/test/test_helper.rb +1 -1
- metadata +2 -24
- 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
@@ -71,8 +71,8 @@ module StatsD::Instrument::Assertions
|
|
71
71
|
# @return [void]
|
72
72
|
# @raise [Minitest::Assertion] If an exception occurs, or if the metric did
|
73
73
|
# not occur as specified during the execution the block.
|
74
|
-
def assert_statsd_increment(metric_name, datagrams: nil, client: nil, **options, &block)
|
75
|
-
expectation = StatsD::Instrument::Expectation.increment(metric_name, **options)
|
74
|
+
def assert_statsd_increment(metric_name, value = nil, datagrams: nil, client: nil, **options, &block)
|
75
|
+
expectation = StatsD::Instrument::Expectation.increment(metric_name, value, **options)
|
76
76
|
assert_statsd_expectation(expectation, datagrams: datagrams, client: client, &block)
|
77
77
|
end
|
78
78
|
|
@@ -83,8 +83,8 @@ module StatsD::Instrument::Assertions
|
|
83
83
|
# @yield (see #assert_statsd_increment)
|
84
84
|
# @return [void]
|
85
85
|
# @raise (see #assert_statsd_increment)
|
86
|
-
def assert_statsd_measure(metric_name, datagrams: nil, client: nil, **options, &block)
|
87
|
-
expectation = StatsD::Instrument::Expectation.measure(metric_name, **options)
|
86
|
+
def assert_statsd_measure(metric_name, value = nil, datagrams: nil, client: nil, **options, &block)
|
87
|
+
expectation = StatsD::Instrument::Expectation.measure(metric_name, value, **options)
|
88
88
|
assert_statsd_expectation(expectation, datagrams: datagrams, client: client, &block)
|
89
89
|
end
|
90
90
|
|
@@ -95,8 +95,8 @@ module StatsD::Instrument::Assertions
|
|
95
95
|
# @yield (see #assert_statsd_increment)
|
96
96
|
# @return [void]
|
97
97
|
# @raise (see #assert_statsd_increment)
|
98
|
-
def assert_statsd_gauge(metric_name, datagrams: nil, client: nil, **options, &block)
|
99
|
-
expectation = StatsD::Instrument::Expectation.gauge(metric_name, **options)
|
98
|
+
def assert_statsd_gauge(metric_name, value = nil, datagrams: nil, client: nil, **options, &block)
|
99
|
+
expectation = StatsD::Instrument::Expectation.gauge(metric_name, value, **options)
|
100
100
|
assert_statsd_expectation(expectation, datagrams: datagrams, client: client, &block)
|
101
101
|
end
|
102
102
|
|
@@ -107,8 +107,8 @@ module StatsD::Instrument::Assertions
|
|
107
107
|
# @yield (see #assert_statsd_increment)
|
108
108
|
# @return [void]
|
109
109
|
# @raise (see #assert_statsd_increment)
|
110
|
-
def assert_statsd_histogram(metric_name, datagrams: nil, client: nil, **options, &block)
|
111
|
-
expectation = StatsD::Instrument::Expectation.histogram(metric_name, **options)
|
110
|
+
def assert_statsd_histogram(metric_name, value = nil, datagrams: nil, client: nil, **options, &block)
|
111
|
+
expectation = StatsD::Instrument::Expectation.histogram(metric_name, value, **options)
|
112
112
|
assert_statsd_expectation(expectation, datagrams: datagrams, client: client, &block)
|
113
113
|
end
|
114
114
|
|
@@ -119,8 +119,8 @@ module StatsD::Instrument::Assertions
|
|
119
119
|
# @yield (see #assert_statsd_increment)
|
120
120
|
# @return [void]
|
121
121
|
# @raise (see #assert_statsd_increment)
|
122
|
-
def assert_statsd_distribution(metric_name, datagrams: nil, client: nil, **options, &block)
|
123
|
-
expectation = StatsD::Instrument::Expectation.distribution(metric_name, **options)
|
122
|
+
def assert_statsd_distribution(metric_name, value = nil, datagrams: nil, client: nil, **options, &block)
|
123
|
+
expectation = StatsD::Instrument::Expectation.distribution(metric_name, value, **options)
|
124
124
|
assert_statsd_expectation(expectation, datagrams: datagrams, client: client, &block)
|
125
125
|
end
|
126
126
|
|
@@ -131,20 +131,8 @@ module StatsD::Instrument::Assertions
|
|
131
131
|
# @yield (see #assert_statsd_increment)
|
132
132
|
# @return [void]
|
133
133
|
# @raise (see #assert_statsd_increment)
|
134
|
-
def assert_statsd_set(metric_name, datagrams: nil, client: nil, **options, &block)
|
135
|
-
expectation = StatsD::Instrument::Expectation.set(metric_name, **options)
|
136
|
-
assert_statsd_expectation(expectation, datagrams: datagrams, client: client, &block)
|
137
|
-
end
|
138
|
-
|
139
|
-
# Asserts that a given key/value metric occurred inside the provided block.
|
140
|
-
#
|
141
|
-
# @param metric_name (see #assert_statsd_increment)
|
142
|
-
# @param options (see #assert_statsd_increment)
|
143
|
-
# @yield (see #assert_statsd_increment)
|
144
|
-
# @return [void]
|
145
|
-
# @raise (see #assert_statsd_increment)
|
146
|
-
def assert_statsd_key_value(metric_name, datagrams: nil, client: nil, **options, &block)
|
147
|
-
expectation = StatsD::Instrument::Expectation.key_value(metric_name, **options)
|
134
|
+
def assert_statsd_set(metric_name, value = nil, datagrams: nil, client: nil, **options, &block)
|
135
|
+
expectation = StatsD::Instrument::Expectation.set(metric_name, value, **options)
|
148
136
|
assert_statsd_expectation(expectation, datagrams: datagrams, client: client, &block)
|
149
137
|
end
|
150
138
|
|
@@ -1,22 +1,27 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
require 'statsd/instrument/datagram_builder'
|
6
|
-
require 'statsd/instrument/statsd_datagram_builder'
|
7
|
-
require 'statsd/instrument/dogstatsd_datagram_builder'
|
8
|
-
require 'statsd/instrument/null_sink'
|
9
|
-
require 'statsd/instrument/udp_sink'
|
10
|
-
require 'statsd/instrument/capture_sink'
|
11
|
-
require 'statsd/instrument/log_sink'
|
12
|
-
|
13
|
-
# The Client is the main interface for using StatsD.
|
3
|
+
# The Client is the main interface for using StatsD. It defines the metric
|
4
|
+
# methods that you would normally call from your application.
|
14
5
|
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
6
|
+
# The client set to {StatsD.singleton_client} will handle all metric calls made
|
7
|
+
# against the StatsD singleton, e.g. `StatsD.increment`.
|
8
|
+
#
|
9
|
+
# We recommend that the configuration of the StatsD setup is provided through
|
10
|
+
# environment variables
|
11
|
+
#
|
12
|
+
# You are encouraged to instantiate multiple clients, and instantiate variants
|
13
|
+
# of an existing clients using {#clone_with_options}. We recommend instantiating
|
14
|
+
# a separate client for every logical component of your application using
|
15
|
+
# `clone_with_options`, and setting a different metric `prefix`.
|
16
|
+
#
|
17
|
+
# @see StatsD.singleton_client
|
18
|
+
# @see #clone_with_options
|
18
19
|
class StatsD::Instrument::Client
|
19
20
|
class << self
|
21
|
+
# Instantiates a StatsD::Instrument::Client using configuration values provided in
|
22
|
+
# environment variables.
|
23
|
+
#
|
24
|
+
# @see StatsD::Instrument::Environment
|
20
25
|
def from_env(
|
21
26
|
env = StatsD::Instrument::Environment.current,
|
22
27
|
prefix: env.statsd_prefix,
|
@@ -36,7 +41,14 @@ class StatsD::Instrument::Client
|
|
36
41
|
)
|
37
42
|
end
|
38
43
|
|
44
|
+
# Finds the right DatagramBuilder class for a given implementation.
|
39
45
|
# @private
|
46
|
+
# @param [Symbol, String] implementation The name of the implementation, e.g.
|
47
|
+
# `"statsd"` or `:datadog`.
|
48
|
+
# @return [Class] The subclass of {StatsD::Instrument::DatagramBuilder}
|
49
|
+
# builder to use to generate UDP datagrams for the given implementation.
|
50
|
+
# @raise `NotImplementedError` if the implementation is not recognized or
|
51
|
+
# supported.
|
40
52
|
def datagram_builder_class_for_implementation(implementation)
|
41
53
|
case implementation.to_s
|
42
54
|
when 'statsd'
|
@@ -49,8 +61,87 @@ class StatsD::Instrument::Client
|
|
49
61
|
end
|
50
62
|
end
|
51
63
|
|
52
|
-
|
64
|
+
# The class to use to build StatsD datagrams. To build the actual datagrams,
|
65
|
+
# the class will be instantiated, potentially multiple times, by the client.
|
66
|
+
#
|
67
|
+
# @return [Class] A subclass of {StatsD::Instrument::DatagramBuilder}
|
68
|
+
# @see .datagram_builder_class_for_implementation
|
69
|
+
attr_reader :datagram_builder_class
|
70
|
+
|
71
|
+
# The sink to send UDP datagrams to.
|
72
|
+
#
|
73
|
+
# This can be set to any object that responds to the following methods:
|
74
|
+
#
|
75
|
+
# - `sample?` which should return true if the metric should be sampled, i.e.
|
76
|
+
# actually sent to the sink.
|
77
|
+
# - `#<<` which takes a UDP datagram as string to emit the datagram. This
|
78
|
+
# method will only be called if `sample?` returned `true`.
|
79
|
+
#
|
80
|
+
# Generally, you should use an instance of one of the following classes that
|
81
|
+
# ship with this library:
|
82
|
+
#
|
83
|
+
# - {StatsD::Instrument::UDPSink} A sink that will actually emit the provided
|
84
|
+
# datagrams over UDP.
|
85
|
+
# - {StatsD::Instrument::NullSink} A sink that will simply swallow every
|
86
|
+
# datagram. This sink is for use when testing your application.
|
87
|
+
# - {StatsD::Instrument::LogSink} A sink that log all provided datagrams to
|
88
|
+
# a Logger, normally {StatsD.logger}.
|
89
|
+
#
|
90
|
+
# @return [#sample?, #<<]
|
91
|
+
attr_reader :sink
|
92
|
+
|
93
|
+
# The prefix to prepend to the metric names that are emitted through this
|
94
|
+
# client, using a dot (`.`) as namespace separator. E.g. when the prefix is
|
95
|
+
# set to `foo`, and you emit a metric named `bar`, the metric name will be
|
96
|
+
# `foo.bar`.
|
97
|
+
#
|
98
|
+
# Generally all the metrics you emit to the same StatsD server will share a
|
99
|
+
# single, global namespace. If you are emitting metrics from multiple
|
100
|
+
# applications, using a prefix is recommended to prevent metric name
|
101
|
+
# collisions.
|
102
|
+
#
|
103
|
+
# You can also leave this value to be `nil` if you don't want to prefix your
|
104
|
+
# metric names.
|
105
|
+
#
|
106
|
+
# @return [String, nil]
|
107
|
+
#
|
108
|
+
# @note The `prefix` can be overriden by any metric call by setting the
|
109
|
+
# `no_prefix` keyword argument to `true`. We recommend against doing this,
|
110
|
+
# but this behavior is retained for backwards compatibility.
|
111
|
+
# Rather, when you feel the need to do this, we recommend instantiating
|
112
|
+
# a new client without prefix (using {#clone_with_options}), and using it
|
113
|
+
# to emit the metric.
|
114
|
+
attr_reader :prefix
|
115
|
+
|
116
|
+
# The tags to apply to all the metrics emitted through this client.
|
117
|
+
#
|
118
|
+
# The tags can be supplied in normal form: an array of strings. You can also
|
119
|
+
# provide a hash, which will be turned into normal form by concatanting the
|
120
|
+
# key and the value using a colon. To not use any default tags, set to `nil`.
|
121
|
+
# Note that other components of your StatsD metric pipeline may also add tags
|
122
|
+
# to metrics. E.g. the DataDog agent may add add tags like `hostname`.
|
123
|
+
#
|
124
|
+
# We generally recommend to not use default tags, or use them sparingly.
|
125
|
+
# Adding tags to every metric easily introduces carninality explosions, which
|
126
|
+
# will make metrics less precise due to the lossy nature of aggregation. It
|
127
|
+
# also makes your infrastructure more expsnive to run, and the user interface
|
128
|
+
# of your metric explorer less responsive.
|
129
|
+
#
|
130
|
+
# @return [Array<String>, Hash, nil]
|
131
|
+
attr_reader :default_tags
|
53
132
|
|
133
|
+
# The default sample rate to use for metrics that are emitted without a
|
134
|
+
# sample rate set. This should be a value between 0 (never emit a metric) and
|
135
|
+
# 1.0 (always emit). If it is not set, the default value 1.0 is used.
|
136
|
+
#
|
137
|
+
# We generally recommend setting sample rates on individual metrics based
|
138
|
+
# on their frequency, rather than changing the default sample rate.
|
139
|
+
#
|
140
|
+
# @return [Float] (default: 1.0) A value between 0.0 and 1.0.
|
141
|
+
attr_reader :default_sample_rate
|
142
|
+
|
143
|
+
# Instantiates a new client.
|
144
|
+
# @see .from_env to instantiate a client using environment variables.
|
54
145
|
def initialize(
|
55
146
|
prefix: nil,
|
56
147
|
default_sample_rate: 1.0,
|
@@ -226,18 +317,15 @@ class StatsD::Instrument::Client
|
|
226
317
|
end
|
227
318
|
end
|
228
319
|
|
229
|
-
# Emits a service check.
|
230
|
-
#
|
231
|
-
#
|
232
|
-
# @param
|
233
|
-
# @param
|
234
|
-
#
|
235
|
-
# @param
|
236
|
-
# @param
|
237
|
-
# @param
|
238
|
-
# @param [String] source_type_name The source type of the event.
|
239
|
-
# @param [String] alert_type Either "error", "warning", "info" (default) or "success".
|
240
|
-
# @param [Array, Hash] tags Tags to associate with the event.
|
320
|
+
# Emits a service check. Services Checks allow you to characterize the status
|
321
|
+
# of a service in order to monitor it within Datadog.
|
322
|
+
#
|
323
|
+
# @param name (see StatsD::Instrument::DogStatsDDatagramBuilder#_sc)
|
324
|
+
# @param status (see StatsD::Instrument::DogStatsDDatagramBuilder#_sc)
|
325
|
+
# @param timestamp (see StatsD::Instrument::DogStatsDDatagramBuilder#_sc)
|
326
|
+
# @param hostname (see StatsD::Instrument::DogStatsDDatagramBuilder#_sc)
|
327
|
+
# @param tags (see StatsD::Instrument::DogStatsDDatagramBuilder#_sc)
|
328
|
+
# @param message (see StatsD::Instrument::DogStatsDDatagramBuilder#_sc)
|
241
329
|
# @return [void]
|
242
330
|
#
|
243
331
|
# @note Supported by the Datadog implementation only.
|
@@ -246,15 +334,17 @@ class StatsD::Instrument::Client
|
|
246
334
|
timestamp: timestamp, hostname: hostname, tags: tags, message: message))
|
247
335
|
end
|
248
336
|
|
249
|
-
# Emits an event.
|
337
|
+
# Emits an event. An event represents any record of activity noteworthy for engineers.
|
250
338
|
#
|
251
|
-
# @param
|
252
|
-
# @param
|
253
|
-
# @param
|
254
|
-
#
|
255
|
-
# @param
|
256
|
-
# @param
|
257
|
-
# @param
|
339
|
+
# @param title (see StatsD::Instrument::DogStatsDDatagramBuilder#_e)
|
340
|
+
# @param text (see StatsD::Instrument::DogStatsDDatagramBuilder#_e)
|
341
|
+
# @param timestamp (see StatsD::Instrument::DogStatsDDatagramBuilder#_e)
|
342
|
+
# @param hostname (see StatsD::Instrument::DogStatsDDatagramBuilder#_e)
|
343
|
+
# @param aggregation_key (see StatsD::Instrument::DogStatsDDatagramBuilder#_e)
|
344
|
+
# @param priority (see StatsD::Instrument::DogStatsDDatagramBuilder#_e)
|
345
|
+
# @param source_type_name (see StatsD::Instrument::DogStatsDDatagramBuilder#_e)
|
346
|
+
# @param alert_type (see StatsD::Instrument::DogStatsDDatagramBuilder#_e)
|
347
|
+
# @param tags (see StatsD::Instrument::DogStatsDDatagramBuilder#_e)
|
258
348
|
# @return [void]
|
259
349
|
#
|
260
350
|
# @note Supported by the Datadog implementation only.
|
@@ -19,21 +19,6 @@ class StatsD::Instrument::Environment
|
|
19
19
|
current.environment
|
20
20
|
end
|
21
21
|
|
22
|
-
# Instantiates a default backend for the current environment.
|
23
|
-
#
|
24
|
-
# @return [StatsD::Instrument::Backend]
|
25
|
-
# @see #environment
|
26
|
-
def default_backend
|
27
|
-
case environment
|
28
|
-
when 'production', 'staging'
|
29
|
-
StatsD::Instrument::Backends::UDPBackend.new(current.statsd_addr, current.statsd_implementation)
|
30
|
-
when 'test'
|
31
|
-
StatsD::Instrument::Backends::NullBackend.new
|
32
|
-
else
|
33
|
-
StatsD::Instrument::Backends::LoggerBackend.new(StatsD.logger)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
22
|
# Sets default values for sample rate and logger.
|
38
23
|
#
|
39
24
|
# - Default sample rate is set to the value in the STATSD_SAMPLE_RATE environment variable,
|
@@ -45,9 +30,6 @@ class StatsD::Instrument::Environment
|
|
45
30
|
#
|
46
31
|
# @return [void]
|
47
32
|
def setup
|
48
|
-
StatsD.prefix = current.statsd_prefix
|
49
|
-
StatsD.default_tags = current.statsd_default_tags
|
50
|
-
StatsD.default_sample_rate = current.statsd_sample_rate
|
51
33
|
StatsD.logger = Logger.new($stderr)
|
52
34
|
end
|
53
35
|
end
|
@@ -96,11 +78,7 @@ class StatsD::Instrument::Environment
|
|
96
78
|
end
|
97
79
|
|
98
80
|
def client
|
99
|
-
|
100
|
-
StatsD::Instrument::Client.from_env(self)
|
101
|
-
else
|
102
|
-
StatsD::Instrument::LegacyClient.singleton
|
103
|
-
end
|
81
|
+
StatsD::Instrument::Client.from_env(self)
|
104
82
|
end
|
105
83
|
|
106
84
|
def default_sink_for_environment
|
@@ -3,32 +3,28 @@
|
|
3
3
|
# @private
|
4
4
|
class StatsD::Instrument::Expectation
|
5
5
|
class << self
|
6
|
-
def increment(name, **options)
|
7
|
-
new(type: :c, name: name, **options)
|
6
|
+
def increment(name, value = nil, **options)
|
7
|
+
new(type: :c, name: name, value: value, **options)
|
8
8
|
end
|
9
9
|
|
10
|
-
def measure(name, **options)
|
11
|
-
new(type: :ms, name: name, **options)
|
10
|
+
def measure(name, value = nil, **options)
|
11
|
+
new(type: :ms, name: name, value: value, **options)
|
12
12
|
end
|
13
13
|
|
14
|
-
def gauge(name, **options)
|
15
|
-
new(type: :g, name: name, **options)
|
14
|
+
def gauge(name, value = nil, **options)
|
15
|
+
new(type: :g, name: name, value: value, **options)
|
16
16
|
end
|
17
17
|
|
18
|
-
def set(name, **options)
|
19
|
-
new(type: :s, name: name, **options)
|
18
|
+
def set(name, value = nil, **options)
|
19
|
+
new(type: :s, name: name, value: value, **options)
|
20
20
|
end
|
21
21
|
|
22
|
-
def
|
23
|
-
new(type: :
|
22
|
+
def distribution(name, value = nil, **options)
|
23
|
+
new(type: :d, name: name, value: value, **options)
|
24
24
|
end
|
25
25
|
|
26
|
-
def
|
27
|
-
new(type: :
|
28
|
-
end
|
29
|
-
|
30
|
-
def histogram(name, **options)
|
31
|
-
new(type: :h, name: name, **options)
|
26
|
+
def histogram(name, value = nil, **options)
|
27
|
+
new(type: :h, name: name, value: value, **options)
|
32
28
|
end
|
33
29
|
end
|
34
30
|
|
@@ -3,38 +3,9 @@
|
|
3
3
|
module StatsD::Instrument::Helpers
|
4
4
|
def capture_statsd_datagrams(client: nil, &block)
|
5
5
|
client ||= StatsD.singleton_client
|
6
|
-
|
7
|
-
when StatsD.legacy_singleton_client
|
8
|
-
capture_statsd_metrics_on_legacy_client(&block)
|
9
|
-
when StatsD::Instrument::Client
|
10
|
-
client.capture(&block)
|
11
|
-
else
|
12
|
-
raise ArgumentError, "Don't know how to capture StatsD datagrams from #{client.inspect}!"
|
13
|
-
end
|
6
|
+
client.capture(&block)
|
14
7
|
end
|
15
8
|
|
16
9
|
# For backwards compatibility
|
17
10
|
alias_method :capture_statsd_calls, :capture_statsd_datagrams
|
18
|
-
|
19
|
-
def capture_statsd_metrics_on_legacy_client(&block)
|
20
|
-
capture_backend = StatsD::Instrument::Backends::CaptureBackend.new
|
21
|
-
with_capture_backend(capture_backend, &block)
|
22
|
-
capture_backend.collected_metrics
|
23
|
-
end
|
24
|
-
|
25
|
-
private
|
26
|
-
|
27
|
-
def with_capture_backend(backend)
|
28
|
-
if StatsD.legacy_singleton_client.backend.is_a?(StatsD::Instrument::Backends::CaptureBackend)
|
29
|
-
backend.parent = StatsD.legacy_singleton_client.backend
|
30
|
-
end
|
31
|
-
|
32
|
-
old_backend = StatsD.legacy_singleton_client.backend
|
33
|
-
begin
|
34
|
-
StatsD.legacy_singleton_client.backend = backend
|
35
|
-
yield
|
36
|
-
ensure
|
37
|
-
StatsD.legacy_singleton_client.backend = old_backend
|
38
|
-
end
|
39
|
-
end
|
40
11
|
end
|
@@ -8,8 +8,4 @@ class StatsD::Instrument::Railtie < Rails::Railtie
|
|
8
8
|
initializer 'statsd-instrument.use_rails_logger' do
|
9
9
|
::StatsD.logger = Rails.logger
|
10
10
|
end
|
11
|
-
|
12
|
-
initializer 'statsd-instrument.setup_backend', after: 'statsd-instrument.use_rails_logger' do
|
13
|
-
::StatsD.backend = ::StatsD::Instrument::Environment.default_backend
|
14
|
-
end
|
15
11
|
end
|
@@ -55,9 +55,7 @@ module StatsD
|
|
55
55
|
super
|
56
56
|
end
|
57
57
|
|
58
|
-
def service_check(name, status, tags: nil, no_prefix: false,
|
59
|
-
hostname: nil, timestamp: nil, message: nil)
|
60
|
-
|
58
|
+
def service_check(name, status, tags: nil, no_prefix: false, hostname: nil, timestamp: nil, message: nil)
|
61
59
|
super
|
62
60
|
end
|
63
61
|
|
@@ -81,11 +79,6 @@ module StatsD
|
|
81
79
|
super
|
82
80
|
end
|
83
81
|
|
84
|
-
def key_value(*)
|
85
|
-
raise NotImplementedError, "The key_value metric type will be removed " \
|
86
|
-
"from the next major version of statsd-instrument"
|
87
|
-
end
|
88
|
-
|
89
82
|
private
|
90
83
|
|
91
84
|
def check_block_or_numeric_value(value)
|
@@ -106,133 +99,30 @@ module StatsD
|
|
106
99
|
end
|
107
100
|
end
|
108
101
|
|
109
|
-
module VoidCollectMetric
|
110
|
-
protected
|
111
|
-
|
112
|
-
def collect_metric(type, name, value, sample_rate:, tags: nil, prefix:, metadata: nil)
|
113
|
-
super
|
114
|
-
StatsD::Instrument::VOID
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
102
|
module StrictMetaprogramming
|
119
|
-
def statsd_measure(method, name, sample_rate: nil, tags: nil,
|
120
|
-
no_prefix: false, client: StatsD.singleton_client)
|
121
|
-
|
103
|
+
def statsd_measure(method, name, sample_rate: nil, tags: nil, no_prefix: false, client: nil)
|
122
104
|
check_method_and_metric_name(method, name)
|
123
|
-
|
124
|
-
# Unfortunately, we have to inline the new method implementation because we have to fix the
|
125
|
-
# Stats.measure call to not use the `as_dist` and `prefix` arguments.
|
126
|
-
add_to_method(method, name, :measure) do
|
127
|
-
define_method(method) do |*args, &block|
|
128
|
-
key = StatsD::Instrument.generate_metric_name(nil, name, self, *args)
|
129
|
-
client.measure(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix) do
|
130
|
-
super(*args, &block)
|
131
|
-
end
|
132
|
-
end
|
133
|
-
end
|
105
|
+
super
|
134
106
|
end
|
135
107
|
|
136
|
-
def statsd_distribution(method, name, sample_rate: nil, tags: nil,
|
137
|
-
no_prefix: false, client: StatsD.singleton_client)
|
138
|
-
|
108
|
+
def statsd_distribution(method, name, sample_rate: nil, tags: nil, no_prefix: false, client: nil)
|
139
109
|
check_method_and_metric_name(method, name)
|
140
|
-
|
141
|
-
# Unfortunately, we have to inline the new method implementation because we have to fix the
|
142
|
-
# Stats.distribution call to not use the `prefix` argument.
|
143
|
-
|
144
|
-
add_to_method(method, name, :distribution) do
|
145
|
-
define_method(method) do |*args, &block|
|
146
|
-
key = StatsD::Instrument.generate_metric_name(nil, name, self, *args)
|
147
|
-
client.distribution(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix) do
|
148
|
-
super(*args, &block)
|
149
|
-
end
|
150
|
-
end
|
151
|
-
end
|
110
|
+
super
|
152
111
|
end
|
153
112
|
|
154
|
-
def statsd_count_success(method, name, sample_rate: nil, tags: nil,
|
155
|
-
no_prefix: false, client: StatsD.singleton_client)
|
156
|
-
|
113
|
+
def statsd_count_success(method, name, sample_rate: nil, tags: nil, no_prefix: false, client: nil)
|
157
114
|
check_method_and_metric_name(method, name)
|
158
|
-
|
159
|
-
# Unfortunately, we have to inline the new method implementation because we have to fix the
|
160
|
-
# Stats.increment call to not use the `prefix` argument.
|
161
|
-
|
162
|
-
add_to_method(method, name, :count_success) do
|
163
|
-
define_method(method) do |*args, &block|
|
164
|
-
begin
|
165
|
-
truthiness = result = super(*args, &block)
|
166
|
-
rescue
|
167
|
-
truthiness = false
|
168
|
-
raise
|
169
|
-
else
|
170
|
-
if block_given?
|
171
|
-
begin
|
172
|
-
truthiness = yield(result)
|
173
|
-
rescue
|
174
|
-
truthiness = false
|
175
|
-
end
|
176
|
-
end
|
177
|
-
result
|
178
|
-
ensure
|
179
|
-
suffix = truthiness == false ? 'failure' : 'success'
|
180
|
-
key = "#{StatsD::Instrument.generate_metric_name(nil, name, self, *args)}.#{suffix}"
|
181
|
-
client.increment(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix)
|
182
|
-
end
|
183
|
-
end
|
184
|
-
end
|
115
|
+
super
|
185
116
|
end
|
186
117
|
|
187
|
-
def statsd_count_if(method, name, sample_rate: nil, tags: nil,
|
188
|
-
no_prefix: false, client: StatsD.singleton_client)
|
189
|
-
|
118
|
+
def statsd_count_if(method, name, sample_rate: nil, tags: nil, no_prefix: false, client: nil)
|
190
119
|
check_method_and_metric_name(method, name)
|
191
|
-
|
192
|
-
# Unfortunately, we have to inline the new method implementation because we have to fix the
|
193
|
-
# Stats.increment call to not use the `prefix` argument.
|
194
|
-
|
195
|
-
add_to_method(method, name, :count_if) do
|
196
|
-
define_method(method) do |*args, &block|
|
197
|
-
begin
|
198
|
-
truthiness = result = super(*args, &block)
|
199
|
-
rescue
|
200
|
-
truthiness = false
|
201
|
-
raise
|
202
|
-
else
|
203
|
-
if block_given?
|
204
|
-
begin
|
205
|
-
truthiness = yield(result)
|
206
|
-
rescue
|
207
|
-
truthiness = false
|
208
|
-
end
|
209
|
-
end
|
210
|
-
result
|
211
|
-
ensure
|
212
|
-
if truthiness
|
213
|
-
key = StatsD::Instrument.generate_metric_name(nil, name, self, *args)
|
214
|
-
client.increment(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix)
|
215
|
-
end
|
216
|
-
end
|
217
|
-
end
|
218
|
-
end
|
120
|
+
super
|
219
121
|
end
|
220
122
|
|
221
|
-
def statsd_count(method, name, sample_rate: nil, tags: nil,
|
222
|
-
no_prefix: false, client: StatsD.singleton_client)
|
223
|
-
|
123
|
+
def statsd_count(method, name, sample_rate: nil, tags: nil, no_prefix: false, client: nil)
|
224
124
|
check_method_and_metric_name(method, name)
|
225
|
-
|
226
|
-
# Unfortunately, we have to inline the new method implementation because we have to fix the
|
227
|
-
# Stats.increment call to not use the `prefix` argument.
|
228
|
-
|
229
|
-
add_to_method(method, name, :count) do
|
230
|
-
define_method(method) do |*args, &block|
|
231
|
-
key = StatsD::Instrument.generate_metric_name(nil, name, self, *args)
|
232
|
-
client.increment(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix)
|
233
|
-
super(*args, &block)
|
234
|
-
end
|
235
|
-
end
|
125
|
+
super
|
236
126
|
end
|
237
127
|
|
238
128
|
private
|
@@ -250,6 +140,5 @@ module StatsD
|
|
250
140
|
end
|
251
141
|
end
|
252
142
|
|
253
|
-
StatsD.
|
254
|
-
StatsD::Instrument::LegacyClient.prepend(StatsD::Instrument::VoidCollectMetric)
|
143
|
+
StatsD::Instrument::Client.prepend(StatsD::Instrument::Strict)
|
255
144
|
StatsD::Instrument.prepend(StatsD::Instrument::StrictMetaprogramming)
|