statsd-instrument 2.9.0 → 3.0.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.
- 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
data/benchmark/datagram-client
DELETED
@@ -1,40 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# frozen_string_literal: true
|
3
|
-
|
4
|
-
require 'bundler/setup'
|
5
|
-
require 'benchmark/ips'
|
6
|
-
require 'socket'
|
7
|
-
|
8
|
-
# Set up an UDP listener to which we can send StatsD packets
|
9
|
-
legacy_receiver = UDPSocket.new
|
10
|
-
legacy_receiver.bind('localhost', 0)
|
11
|
-
|
12
|
-
ENV['ENV'] = "production"
|
13
|
-
ENV['STATSD_ADDR'] = "#{legacy_receiver.addr[2]}:#{legacy_receiver.addr[1]}"
|
14
|
-
ENV['STATSD_IMPLEMENTATION'] ||= 'datadog'
|
15
|
-
|
16
|
-
require 'statsd-instrument'
|
17
|
-
|
18
|
-
legacy_client = StatsD
|
19
|
-
|
20
|
-
# Set up an UDP listener to which we can send StatsD packets
|
21
|
-
new_client_receiver = UDPSocket.new
|
22
|
-
new_client_receiver.bind('localhost', 0)
|
23
|
-
|
24
|
-
udp_sink = StatsD::Instrument::UDPSink.new(new_client_receiver.addr[2], new_client_receiver.addr[1])
|
25
|
-
new_client = StatsD::Instrument::Client.new(sink: udp_sink, default_sample_rate: StatsD.default_sample_rate)
|
26
|
-
|
27
|
-
Benchmark.ips do |bench|
|
28
|
-
bench.report("Legacy client (sample rate: #{StatsD.default_sample_rate})") do
|
29
|
-
legacy_client.increment('StatsD.increment')
|
30
|
-
end
|
31
|
-
|
32
|
-
bench.report("New client (sample rate: #{StatsD.default_sample_rate})") do
|
33
|
-
new_client.increment('StatsD.increment')
|
34
|
-
end
|
35
|
-
|
36
|
-
bench.compare!
|
37
|
-
end
|
38
|
-
|
39
|
-
legacy_receiver.close
|
40
|
-
new_client_receiver.close
|
@@ -1,18 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# This abstract class specifies the interface a backend implementation should conform to.
|
4
|
-
# @abstract
|
5
|
-
class StatsD::Instrument::Backend
|
6
|
-
# Collects a metric.
|
7
|
-
#
|
8
|
-
# @param metric [StatsD::Instrument::Metric] The metric to collect
|
9
|
-
# @return [void]
|
10
|
-
def collect_metric(_metric)
|
11
|
-
raise NotImplementedError, "Use a concrete backend implementation"
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
require 'statsd/instrument/backends/logger_backend'
|
16
|
-
require 'statsd/instrument/backends/null_backend'
|
17
|
-
require 'statsd/instrument/backends/capture_backend'
|
18
|
-
require 'statsd/instrument/backends/udp_backend'
|
@@ -1,32 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module StatsD::Instrument::Backends
|
4
|
-
# The capture backend is used to capture the metrics that are collected, so you can
|
5
|
-
# run assertions on them.
|
6
|
-
#
|
7
|
-
# @!attribute collected_metrics [r]
|
8
|
-
# @return [Array<StatsD::Instrument::Metric>] The list of metrics that were collected.
|
9
|
-
# @see StatsD::Instrument::Assertions
|
10
|
-
class CaptureBackend < StatsD::Instrument::Backend
|
11
|
-
attr_reader :collected_metrics
|
12
|
-
attr_accessor :parent
|
13
|
-
|
14
|
-
def initialize
|
15
|
-
reset
|
16
|
-
end
|
17
|
-
|
18
|
-
# Adds a metric to the ist of collected metrics.
|
19
|
-
# @param metric [StatsD::Instrument::Metric] The metric to collect.
|
20
|
-
# @return [void]
|
21
|
-
def collect_metric(metric)
|
22
|
-
parent&.collect_metric(metric)
|
23
|
-
@collected_metrics << metric
|
24
|
-
end
|
25
|
-
|
26
|
-
# Resets the list of collected metrics to an empty list.
|
27
|
-
# @return [void]
|
28
|
-
def reset
|
29
|
-
@collected_metrics = []
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module StatsD::Instrument::Backends
|
4
|
-
# The logger backend simply logs every metric to a logger
|
5
|
-
# @!attribute logger
|
6
|
-
# @return [Logger]
|
7
|
-
class LoggerBackend < StatsD::Instrument::Backend
|
8
|
-
attr_accessor :logger
|
9
|
-
|
10
|
-
def initialize(logger)
|
11
|
-
@logger = logger
|
12
|
-
end
|
13
|
-
|
14
|
-
# @param metric [StatsD::Instrument::Metric]
|
15
|
-
# @return [void]
|
16
|
-
def collect_metric(metric)
|
17
|
-
logger.info("[StatsD] #{metric}")
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
@@ -1,9 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module StatsD::Instrument::Backends
|
4
|
-
# The null backend does nothing when receiving a metric, effectively disabling the gem completely.
|
5
|
-
class NullBackend < StatsD::Instrument::Backend
|
6
|
-
def collect_metric(metric)
|
7
|
-
end
|
8
|
-
end
|
9
|
-
end
|
@@ -1,152 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'monitor'
|
4
|
-
|
5
|
-
module StatsD::Instrument::Backends
|
6
|
-
class UDPBackend < StatsD::Instrument::Backend
|
7
|
-
BASE_SUPPORTED_METRIC_TYPES = { c: true, ms: true, g: true, s: true }
|
8
|
-
|
9
|
-
class DogStatsDProtocol
|
10
|
-
SUPPORTED_METRIC_TYPES = BASE_SUPPORTED_METRIC_TYPES.merge(h: true, _e: true, _sc: true, d: true)
|
11
|
-
|
12
|
-
SERVICE_CHECK_STATUSES = { ok: 0, warning: 1, critical: 2, unknown: 3 }
|
13
|
-
|
14
|
-
def generate_packet(metric)
|
15
|
-
packet = +""
|
16
|
-
|
17
|
-
if metric.type == :_e
|
18
|
-
escaped_title = metric.name.gsub("\n", "\\n")
|
19
|
-
escaped_text = metric.value.gsub("\n", "\\n")
|
20
|
-
|
21
|
-
packet << "_e{#{escaped_title.size},#{escaped_text.size}}:#{escaped_title}|#{escaped_text}"
|
22
|
-
packet << "|h:#{metric.metadata[:hostname]}" if metric.metadata[:hostname]
|
23
|
-
packet << "|d:#{metric.metadata[:timestamp].to_i}" if metric.metadata[:timestamp]
|
24
|
-
packet << "|k:#{metric.metadata[:aggregation_key]}" if metric.metadata[:aggregation_key]
|
25
|
-
packet << "|p:#{metric.metadata[:priority]}" if metric.metadata[:priority]
|
26
|
-
packet << "|s:#{metric.metadata[:source_type_name]}" if metric.metadata[:source_type_name]
|
27
|
-
packet << "|t:#{metric.metadata[:alert_type]}" if metric.metadata[:alert_type]
|
28
|
-
packet << "|##{metric.tags.join(',')}" if metric.tags
|
29
|
-
|
30
|
-
elsif metric.type == :_sc
|
31
|
-
status = metric.value.is_a?(Integer) ? metric.value : SERVICE_CHECK_STATUSES.fetch(metric.value.to_sym)
|
32
|
-
|
33
|
-
packet << "_sc|#{metric.name}|#{status}"
|
34
|
-
packet << "|h:#{metric.metadata[:hostname]}" if metric.metadata[:hostname]
|
35
|
-
packet << "|d:#{metric.metadata[:timestamp].to_i}" if metric.metadata[:timestamp]
|
36
|
-
packet << "|##{metric.tags.join(',')}" if metric.tags
|
37
|
-
packet << "|m:#{metric.metadata[:message]}" if metric.metadata[:message]
|
38
|
-
|
39
|
-
else
|
40
|
-
packet << "#{metric.name}:#{metric.value}|#{metric.type}"
|
41
|
-
packet << "|@#{metric.sample_rate}" if metric.sample_rate < 1
|
42
|
-
packet << "|##{metric.tags.join(',')}" if metric.tags
|
43
|
-
end
|
44
|
-
|
45
|
-
packet
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
class StatsiteStatsDProtocol
|
50
|
-
SUPPORTED_METRIC_TYPES = BASE_SUPPORTED_METRIC_TYPES.merge(kv: true)
|
51
|
-
|
52
|
-
def generate_packet(metric)
|
53
|
-
packet = +"#{metric.name}:#{metric.value}|#{metric.type}"
|
54
|
-
packet << "|@#{metric.sample_rate}" unless metric.sample_rate == 1
|
55
|
-
packet << "\n"
|
56
|
-
packet
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
class StatsDProtocol
|
61
|
-
SUPPORTED_METRIC_TYPES = BASE_SUPPORTED_METRIC_TYPES
|
62
|
-
|
63
|
-
def generate_packet(metric)
|
64
|
-
packet = +"#{metric.name}:#{metric.value}|#{metric.type}"
|
65
|
-
packet << "|@#{metric.sample_rate}" if metric.sample_rate < 1
|
66
|
-
packet
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
DEFAULT_IMPLEMENTATION = :statsd
|
71
|
-
|
72
|
-
include MonitorMixin
|
73
|
-
|
74
|
-
attr_reader :host, :port, :implementation
|
75
|
-
|
76
|
-
def initialize(server = nil, implementation = nil)
|
77
|
-
super()
|
78
|
-
self.server = server || "localhost:8125"
|
79
|
-
self.implementation = (implementation || DEFAULT_IMPLEMENTATION).to_sym
|
80
|
-
end
|
81
|
-
|
82
|
-
def implementation=(value)
|
83
|
-
@packet_factory = case value
|
84
|
-
when :datadog
|
85
|
-
DogStatsDProtocol.new
|
86
|
-
when :statsite
|
87
|
-
StatsiteStatsDProtocol.new
|
88
|
-
else
|
89
|
-
StatsDProtocol.new
|
90
|
-
end
|
91
|
-
@implementation = value
|
92
|
-
end
|
93
|
-
|
94
|
-
def collect_metric(metric)
|
95
|
-
unless @packet_factory.class::SUPPORTED_METRIC_TYPES[metric.type]
|
96
|
-
StatsD.logger.warn("[StatsD] Metric type #{metric.type.inspect} is not supported " \
|
97
|
-
"on #{implementation} implementation.")
|
98
|
-
return false
|
99
|
-
end
|
100
|
-
|
101
|
-
if metric.sample_rate < 1.0 && rand > metric.sample_rate
|
102
|
-
return false
|
103
|
-
end
|
104
|
-
|
105
|
-
write_packet(@packet_factory.generate_packet(metric))
|
106
|
-
end
|
107
|
-
|
108
|
-
def server=(connection_string)
|
109
|
-
@host, @port = connection_string.split(':', 2)
|
110
|
-
@port = @port.to_i
|
111
|
-
invalidate_socket
|
112
|
-
end
|
113
|
-
|
114
|
-
def host=(host)
|
115
|
-
@host = host
|
116
|
-
invalidate_socket
|
117
|
-
end
|
118
|
-
|
119
|
-
def port=(port)
|
120
|
-
@port = port
|
121
|
-
invalidate_socket
|
122
|
-
end
|
123
|
-
|
124
|
-
def socket
|
125
|
-
if @socket.nil?
|
126
|
-
@socket = UDPSocket.new
|
127
|
-
@socket.connect(host, port)
|
128
|
-
end
|
129
|
-
@socket
|
130
|
-
end
|
131
|
-
|
132
|
-
def write_packet(command)
|
133
|
-
synchronize do
|
134
|
-
socket.send(command, 0) > 0
|
135
|
-
end
|
136
|
-
rescue ThreadError
|
137
|
-
# In cases where a TERM or KILL signal has been sent, and we send stats as
|
138
|
-
# part of a signal handler, locks cannot be acquired, so we do our best
|
139
|
-
# to try and send the command without a lock.
|
140
|
-
socket.send(command, 0) > 0
|
141
|
-
rescue SocketError, IOError, SystemCallError => e
|
142
|
-
StatsD.logger.error("[StatsD] #{e.class.name}: #{e.message}")
|
143
|
-
invalidate_socket
|
144
|
-
end
|
145
|
-
|
146
|
-
def invalidate_socket
|
147
|
-
synchronize do
|
148
|
-
@socket = nil
|
149
|
-
end
|
150
|
-
end
|
151
|
-
end
|
152
|
-
end
|
@@ -1,301 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class StatsD::Instrument::LegacyClient
|
4
|
-
def self.singleton
|
5
|
-
@singleton ||= new
|
6
|
-
end
|
7
|
-
|
8
|
-
attr_accessor :default_sample_rate, :prefix
|
9
|
-
attr_writer :backend
|
10
|
-
attr_reader :default_tags
|
11
|
-
|
12
|
-
def default_tags=(tags)
|
13
|
-
@default_tags = StatsD::Instrument::Metric.normalize_tags(tags)
|
14
|
-
end
|
15
|
-
|
16
|
-
def backend
|
17
|
-
@backend ||= StatsD::Instrument::Environment.default_backend
|
18
|
-
end
|
19
|
-
|
20
|
-
# @!method measure(name, value = nil, sample_rate: nil, tags: nil, &block)
|
21
|
-
#
|
22
|
-
# Emits a timing metric
|
23
|
-
#
|
24
|
-
# @param [String] key The name of the metric.
|
25
|
-
# @param sample_rate (see #increment)
|
26
|
-
# @param tags (see #increment)
|
27
|
-
#
|
28
|
-
# @example Providing a value directly
|
29
|
-
# start = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
|
30
|
-
# do_something
|
31
|
-
# stop = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
|
32
|
-
# http_response = StatsD.measure('HTTP.call.duration', stop - start)
|
33
|
-
#
|
34
|
-
# @example Providing a block to measure the duration of its execution
|
35
|
-
# http_response = StatsD.measure('HTTP.call.duration') do
|
36
|
-
# Net::HTTP.get(url)
|
37
|
-
# end
|
38
|
-
#
|
39
|
-
# @overload measure(key, value, sample_rate: nil, tags: nil)
|
40
|
-
# Emits a timing metric, by providing a duration in milliseconds.
|
41
|
-
#
|
42
|
-
# @param [Float] value The measured duration in milliseconds
|
43
|
-
# @return [void]
|
44
|
-
#
|
45
|
-
# @overload measure(key, sample_rate: nil, tags: nil, &block)
|
46
|
-
# Emits a timing metric, after measuring the execution duration of the
|
47
|
-
# block passed to this method.
|
48
|
-
#
|
49
|
-
# @yield `StatsD.measure` will yield the block and measure the duration. After the block
|
50
|
-
# returns, the duration in millisecond will be emitted as metric.
|
51
|
-
# @return The value that was returned by the block passed through.
|
52
|
-
def measure(
|
53
|
-
key, value_arg = nil, deprecated_sample_rate_arg = nil, deprecated_tags_arg = nil,
|
54
|
-
value: value_arg, sample_rate: deprecated_sample_rate_arg, tags: deprecated_tags_arg,
|
55
|
-
prefix: self.prefix, no_prefix: false, as_dist: false,
|
56
|
-
&block
|
57
|
-
)
|
58
|
-
# TODO: in the next version, hardcode this to :ms when the as_dist argument is dropped.
|
59
|
-
type = as_dist ? :d : :ms
|
60
|
-
prefix = nil if no_prefix
|
61
|
-
if block_given?
|
62
|
-
measure_latency(type, key, sample_rate: sample_rate, tags: tags, prefix: prefix, &block)
|
63
|
-
else
|
64
|
-
collect_metric(type, key, value, sample_rate: sample_rate, tags: tags, prefix: prefix)
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
# @!method increment(name, value = 1, sample_rate: nil, tags: nil)
|
69
|
-
#
|
70
|
-
# Emits a counter metric.
|
71
|
-
#
|
72
|
-
# @param key [String] The name of the metric.
|
73
|
-
# @param value [Integer] The value to increment the counter by.
|
74
|
-
#
|
75
|
-
# You should not compensate for the sample rate using the counter increment. E.g., if
|
76
|
-
# your sample rate is 0.01, you should <b>not</b> use 100 as increment to compensate for it.
|
77
|
-
# The sample rate is part of the packet that is being sent to the server, and the server
|
78
|
-
# should know how to handle it.
|
79
|
-
#
|
80
|
-
# @param sample_rate [Float] (default: `StatsD.default_sample_rate`) The rate at which to sample
|
81
|
-
# this metric call. This value should be between 0 and 1. This value can be used to reduce
|
82
|
-
# the amount of network I/O (and CPU cycles) used for very frequent metrics.
|
83
|
-
#
|
84
|
-
# - A value of `0.1` means that only 1 out of 10 calls will be emitted; the other 9 will
|
85
|
-
# be short-circuited.
|
86
|
-
# - When set to `1`, every metric will be emitted.
|
87
|
-
# - If this parameter is not set, the default sample rate for this client will be used.
|
88
|
-
# @param tags [Array<String>, Hash<Symbol, String>] The tags to associate with this measurement.
|
89
|
-
# They can be provided as an array of strings, or a hash of key/value pairs.
|
90
|
-
# _Note:_ Tags are not supported by all implementations.
|
91
|
-
# @return [void]
|
92
|
-
def increment(
|
93
|
-
key, value_arg = 1, deprecated_sample_rate_arg = nil, deprecated_tags_arg = nil,
|
94
|
-
value: value_arg, sample_rate: deprecated_sample_rate_arg, tags: deprecated_tags_arg,
|
95
|
-
prefix: self.prefix, no_prefix: false
|
96
|
-
)
|
97
|
-
prefix = nil if no_prefix
|
98
|
-
collect_metric(:c, key, value, sample_rate: sample_rate, tags: tags, prefix: prefix)
|
99
|
-
end
|
100
|
-
|
101
|
-
# @!method gauge(name, value, sample_rate: nil, tags: nil)
|
102
|
-
#
|
103
|
-
# Emits a gauge metric.
|
104
|
-
#
|
105
|
-
# @param key The name of the metric.
|
106
|
-
# @param value [Numeric] The current value to record.
|
107
|
-
# @param sample_rate (see #increment)
|
108
|
-
# @param tags (see #increment)
|
109
|
-
# @return [void]
|
110
|
-
def gauge(
|
111
|
-
key, value_arg = nil, deprecated_sample_rate_arg = nil, deprecated_tags_arg = nil,
|
112
|
-
value: value_arg, sample_rate: deprecated_sample_rate_arg, tags: deprecated_tags_arg,
|
113
|
-
prefix: self.prefix, no_prefix: false
|
114
|
-
)
|
115
|
-
prefix = nil if no_prefix
|
116
|
-
collect_metric(:g, key, value, sample_rate: sample_rate, tags: tags, prefix: prefix)
|
117
|
-
end
|
118
|
-
|
119
|
-
# @!method set(name, value, sample_rate: nil, tags: nil)
|
120
|
-
#
|
121
|
-
# Emits a set metric, which counts the number of distinct values that have occurred.
|
122
|
-
#
|
123
|
-
# @example Counting the number of unique visitors
|
124
|
-
# StatsD.set('visitors.unique', Current.user.id)
|
125
|
-
#
|
126
|
-
# @param key [String] The name of the metric.
|
127
|
-
# @param value [Numeric] The value to record.
|
128
|
-
# @param sample_rate (see #increment)
|
129
|
-
# @param tags (see #increment)
|
130
|
-
# @return [void]
|
131
|
-
def set(
|
132
|
-
key, value_arg = nil, deprecated_sample_rate_arg = nil, deprecated_tags_arg = nil,
|
133
|
-
value: value_arg, sample_rate: deprecated_sample_rate_arg, tags: deprecated_tags_arg,
|
134
|
-
prefix: self.prefix, no_prefix: false
|
135
|
-
)
|
136
|
-
prefix = nil if no_prefix
|
137
|
-
collect_metric(:s, key, value, sample_rate: sample_rate, tags: tags, prefix: prefix)
|
138
|
-
end
|
139
|
-
|
140
|
-
# @!method histogram(name, value, sample_rate: nil, tags: nil)
|
141
|
-
#
|
142
|
-
# Emits a histogram metric.
|
143
|
-
#
|
144
|
-
# @param key The name of the metric.
|
145
|
-
# @param value [Numeric] The value to record.
|
146
|
-
# @param sample_rate (see #increment)
|
147
|
-
# @param tags (see #increment)
|
148
|
-
# @return (see #collect_metric)
|
149
|
-
# @note Supported by the datadog implementation only.
|
150
|
-
def histogram(
|
151
|
-
key, value_arg = nil, deprecated_sample_rate_arg = nil, deprecated_tags_arg = nil,
|
152
|
-
value: value_arg, sample_rate: deprecated_sample_rate_arg, tags: deprecated_tags_arg,
|
153
|
-
prefix: self.prefix, no_prefix: false
|
154
|
-
)
|
155
|
-
prefix = nil if no_prefix
|
156
|
-
collect_metric(:h, key, value, sample_rate: sample_rate, tags: tags, prefix: prefix)
|
157
|
-
end
|
158
|
-
|
159
|
-
# @!method distribution(name, value = nil, sample_rate: nil, tags: nil, &block)
|
160
|
-
#
|
161
|
-
# Emits a distribution metric.
|
162
|
-
#
|
163
|
-
# @param [String] key The name of the metric.
|
164
|
-
# @param sample_rate (see #increment)
|
165
|
-
# @param tags (see #increment)
|
166
|
-
#
|
167
|
-
# @note Supported by the datadog implementation only.
|
168
|
-
# @example
|
169
|
-
# http_response = StatsD.distribution('HTTP.call.duration') do
|
170
|
-
# Net::HTTP.get(url)
|
171
|
-
# end
|
172
|
-
#
|
173
|
-
# @overload distribution(name, value, sample_rate: nil, tags: nil)
|
174
|
-
#
|
175
|
-
# Emits a distribution metric, given a provided value to record.
|
176
|
-
#
|
177
|
-
# @param [Numeric] value The value to record.
|
178
|
-
# @return [void]
|
179
|
-
#
|
180
|
-
# @overload distribution(key, metric_options = {}, &block)
|
181
|
-
#
|
182
|
-
# Emits a distribution metric for the duration of the provided block, in milliseconds.
|
183
|
-
#
|
184
|
-
# @yield `StatsD.distribution` will yield the block and measure the duration. After
|
185
|
-
# the block returns, the duration in millisecond will be emitted as metric.
|
186
|
-
# @return The value that was returned by the block passed through.
|
187
|
-
def distribution(
|
188
|
-
key, value_arg = nil, deprecated_sample_rate_arg = nil, deprecated_tags_arg = nil,
|
189
|
-
value: value_arg, sample_rate: deprecated_sample_rate_arg, tags: deprecated_tags_arg,
|
190
|
-
prefix: self.prefix, no_prefix: false,
|
191
|
-
&block
|
192
|
-
)
|
193
|
-
prefix = nil if no_prefix
|
194
|
-
if block_given?
|
195
|
-
measure_latency(:d, key, sample_rate: sample_rate, tags: tags, prefix: prefix, &block)
|
196
|
-
else
|
197
|
-
collect_metric(:d, key, value, sample_rate: sample_rate, tags: tags, prefix: prefix)
|
198
|
-
end
|
199
|
-
end
|
200
|
-
|
201
|
-
# @!method key_value(name, value)
|
202
|
-
#
|
203
|
-
# Emits a key/value metric.
|
204
|
-
#
|
205
|
-
# @param key [String] The name of the metric.
|
206
|
-
# @param value [Numeric] The value to record.
|
207
|
-
# @return [void]
|
208
|
-
#
|
209
|
-
# @note Supported by the statsite implementation only.
|
210
|
-
def key_value(
|
211
|
-
key, value_arg = nil, deprecated_sample_rate_arg = nil,
|
212
|
-
value: value_arg, sample_rate: deprecated_sample_rate_arg, no_prefix: false
|
213
|
-
)
|
214
|
-
prefix = nil if no_prefix
|
215
|
-
collect_metric(:kv, key, value, sample_rate: sample_rate, prefix: prefix)
|
216
|
-
end
|
217
|
-
|
218
|
-
# @!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
|
219
|
-
#
|
220
|
-
# Emits an event.
|
221
|
-
#
|
222
|
-
# @param title [String] Title of the event. A configured prefix may be applied to this title.
|
223
|
-
# @param text [String] Body of the event. Can contain newlines.
|
224
|
-
# @param [String] hostname The hostname to associate with the event.
|
225
|
-
# @param [Time] timestamp The moment the status of the service was checked. Defaults to now.
|
226
|
-
# @param [String] aggregation_key A key to aggregate similar events into groups.
|
227
|
-
# @param [String] priority The event's priority, either `"low"` or `"normal"` (default).
|
228
|
-
# @param [String] source_type_name The source type.
|
229
|
-
# @param [String] alert_type The type of alert. Either `"info"` (default), `"warning"`, `"error"`, or `"success"`.
|
230
|
-
# @param tags (see #increment)
|
231
|
-
# @return [void]
|
232
|
-
#
|
233
|
-
# @note Supported by the Datadog implementation only.
|
234
|
-
def event(
|
235
|
-
title, text,
|
236
|
-
deprecated_sample_rate_arg = nil, deprecated_tags_arg = nil,
|
237
|
-
sample_rate: deprecated_sample_rate_arg, tags: deprecated_tags_arg,
|
238
|
-
prefix: self.prefix, no_prefix: false,
|
239
|
-
hostname: nil, date_happened: nil, timestamp: date_happened,
|
240
|
-
aggregation_key: nil, priority: nil, source_type_name: nil, alert_type: nil,
|
241
|
-
**_ignored
|
242
|
-
)
|
243
|
-
prefix = nil if no_prefix
|
244
|
-
collect_metric(:_e, title, text, sample_rate: sample_rate, tags: tags, prefix: prefix, metadata: {
|
245
|
-
hostname: hostname, timestamp: timestamp, aggregation_key: aggregation_key,
|
246
|
-
priority: priority, source_type_name: source_type_name, alert_type: alert_type
|
247
|
-
})
|
248
|
-
end
|
249
|
-
|
250
|
-
# @!method service_check(name, status, tags: nil, hostname: nil, timestamp: nil, message: nil)
|
251
|
-
#
|
252
|
-
# Emits a service check.
|
253
|
-
#
|
254
|
-
# @param [String] name Name of the service. A configured prefix may be applied to this title.
|
255
|
-
# @param [Symbol] status Current status of the service. Either `:ok`, `:warning`, `:critical`, or `:unknown`.
|
256
|
-
# @param [String] hostname The hostname to associate with the event.
|
257
|
-
# @param [Time] timestamp The moment the status of the service was checked. Defaults to now.
|
258
|
-
# @param [String] message A message that describes the current status.
|
259
|
-
# @param tags (see #increment)
|
260
|
-
# @return [void]
|
261
|
-
#
|
262
|
-
# @note Supported by the Datadog implementation only.
|
263
|
-
def service_check(
|
264
|
-
name, status,
|
265
|
-
deprecated_sample_rate_arg = nil, deprecated_tags_arg = nil,
|
266
|
-
sample_rate: deprecated_sample_rate_arg, tags: deprecated_tags_arg,
|
267
|
-
prefix: self.prefix, no_prefix: false,
|
268
|
-
hostname: nil, timestamp: nil, message: nil, **_ignored
|
269
|
-
)
|
270
|
-
prefix = nil if no_prefix
|
271
|
-
collect_metric(:_sc, name, status, sample_rate: sample_rate, prefix: prefix, tags: tags, metadata: {
|
272
|
-
hostname: hostname, timestamp: timestamp, message: message
|
273
|
-
})
|
274
|
-
end
|
275
|
-
|
276
|
-
protected
|
277
|
-
|
278
|
-
def measure_latency(type, key, sample_rate:, tags:, prefix:)
|
279
|
-
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
280
|
-
begin
|
281
|
-
yield
|
282
|
-
ensure
|
283
|
-
# Ensure catches both a raised exception and a return in the invoked block
|
284
|
-
value = 1000.0 * (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start)
|
285
|
-
collect_metric(type, key, value, sample_rate: sample_rate, tags: tags, prefix: prefix)
|
286
|
-
end
|
287
|
-
end
|
288
|
-
|
289
|
-
# Instantiates a metric, and sends it to the backend for further processing.
|
290
|
-
# @param options (see StatsD::Instrument::Metric#initialize)
|
291
|
-
# @return [void]
|
292
|
-
def collect_metric(type, name, value, sample_rate:, tags: nil, prefix:, metadata: nil)
|
293
|
-
sample_rate ||= default_sample_rate
|
294
|
-
name = "#{prefix}.#{name}" if prefix
|
295
|
-
|
296
|
-
metric = StatsD::Instrument::Metric.new(type: type, name: name, value: value,
|
297
|
-
sample_rate: sample_rate, tags: tags, metadata: metadata)
|
298
|
-
backend.collect_metric(metric)
|
299
|
-
metric
|
300
|
-
end
|
301
|
-
end
|