statsd-instrument 3.0.0 → 3.0.1
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 +3 -3
- data/.rubocop.yml +3 -13
- data/CHANGELOG.md +6 -0
- data/Gemfile +8 -0
- data/README.md +2 -2
- data/Rakefile +1 -1
- data/bin/rake +29 -0
- data/bin/rubocop +29 -0
- data/lib/statsd/instrument.rb +4 -1
- data/lib/statsd/instrument/assertions.rb +200 -196
- data/lib/statsd/instrument/capture_sink.rb +23 -19
- data/lib/statsd/instrument/client.rb +414 -410
- 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 +80 -77
- data/lib/statsd/instrument/expectation.rb +96 -92
- data/lib/statsd/instrument/helpers.rb +11 -7
- data/lib/statsd/instrument/log_sink.rb +20 -16
- data/lib/statsd/instrument/matchers.rb +86 -70
- data/lib/statsd/instrument/null_sink.rb +12 -8
- data/lib/statsd/instrument/railtie.rb +11 -7
- data/lib/statsd/instrument/statsd_datagram_builder.rb +12 -8
- 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 +12 -12
- data/test/capture_sink_test.rb +8 -8
- data/test/client_test.rb +54 -54
- 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 +9 -9
- data/test/helpers/rubocop_helper.rb +9 -6
- data/test/helpers_test.rb +5 -5
- data/test/integration_test.rb +1 -1
- 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/positional_arguments_test.rb +10 -10
- data/test/statsd_instrumentation_test.rb +66 -66
- data/test/statsd_test.rb +44 -44
- data/test/test_helper.rb +6 -4
- data/test/udp_sink_test.rb +8 -8
- metadata +7 -103
- data/.rubocop-https---shopify-github-io-ruby-style-guide-rubocop-yml +0 -1027
@@ -1,83 +1,87 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
#
|
6
|
-
#
|
7
|
-
class
|
8
|
-
|
3
|
+
module StatsD
|
4
|
+
module Instrument
|
5
|
+
# The Datagram class parses and inspects a StatsD datagrams
|
6
|
+
#
|
7
|
+
# @note This class is part of the new Client implementation that is intended
|
8
|
+
# to become the new default in the next major release of this library.
|
9
|
+
class Datagram
|
10
|
+
attr_reader :source
|
9
11
|
|
10
|
-
|
11
|
-
|
12
|
-
|
12
|
+
def initialize(source)
|
13
|
+
@source = source
|
14
|
+
end
|
13
15
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
16
|
+
# @return [Float] The sample rate at which this datagram was emitted, between 0 and 1.
|
17
|
+
def sample_rate
|
18
|
+
parsed_datagram[:sample_rate] ? Float(parsed_datagram[:sample_rate]) : 1.0
|
19
|
+
end
|
18
20
|
|
19
|
-
|
20
|
-
|
21
|
-
|
21
|
+
def type
|
22
|
+
@type ||= parsed_datagram[:type].to_sym
|
23
|
+
end
|
22
24
|
|
23
|
-
|
24
|
-
|
25
|
-
|
25
|
+
def name
|
26
|
+
parsed_datagram[:name]
|
27
|
+
end
|
26
28
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
29
|
+
def value
|
30
|
+
@value ||= case type
|
31
|
+
when :c
|
32
|
+
Integer(parsed_datagram[:value])
|
33
|
+
when :g, :h, :d, :kv, :ms
|
34
|
+
Float(parsed_datagram[:value])
|
35
|
+
when :s
|
36
|
+
String(parsed_datagram[:value])
|
37
|
+
else
|
38
|
+
parsed_datagram[:value]
|
39
|
+
end
|
40
|
+
end
|
39
41
|
|
40
|
-
|
41
|
-
|
42
|
-
|
42
|
+
def tags
|
43
|
+
@tags ||= parsed_datagram[:tags] ? parsed_datagram[:tags].split(',') : nil
|
44
|
+
end
|
43
45
|
|
44
|
-
|
45
|
-
|
46
|
-
|
46
|
+
def inspect
|
47
|
+
"#<#{self.class.name}:\"#{@source}\">"
|
48
|
+
end
|
47
49
|
|
48
|
-
|
49
|
-
|
50
|
-
|
50
|
+
def hash
|
51
|
+
source.hash
|
52
|
+
end
|
51
53
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
54
|
+
def eql?(other)
|
55
|
+
case other
|
56
|
+
when StatsD::Instrument::Datagram
|
57
|
+
source == other.source
|
58
|
+
when String
|
59
|
+
source == other
|
60
|
+
else
|
61
|
+
false
|
62
|
+
end
|
63
|
+
end
|
62
64
|
|
63
|
-
|
65
|
+
alias_method :==, :eql?
|
64
66
|
|
65
|
-
|
67
|
+
private
|
66
68
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
69
|
+
PARSER = %r{
|
70
|
+
\A
|
71
|
+
(?<name>[^\:\|\@]+)\:(?<value>[^\:\|\@]+)\|(?<type>c|ms|g|s|h|d)
|
72
|
+
(?:\|\@(?<sample_rate>\d*(?:\.\d*)?))?
|
73
|
+
(?:\|\#(?<tags>(?:[^\|,]+(?:,[^\|,]+)*)))?
|
74
|
+
\n? # In some implementations, the datagram may include a trailing newline.
|
75
|
+
\z
|
76
|
+
}x
|
75
77
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
78
|
+
def parsed_datagram
|
79
|
+
@parsed ||= if (match_info = PARSER.match(@source))
|
80
|
+
match_info
|
81
|
+
else
|
82
|
+
raise ArgumentError, "Invalid StatsD datagram: #{@source}"
|
83
|
+
end
|
84
|
+
end
|
81
85
|
end
|
82
86
|
end
|
83
87
|
end
|
@@ -1,101 +1,105 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
class
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
3
|
+
module StatsD
|
4
|
+
module Instrument
|
5
|
+
# @note This class is part of the new Client implementation that is intended
|
6
|
+
# to become the new default in the next major release of this library.
|
7
|
+
class DatagramBuilder
|
8
|
+
unless Regexp.method_defined?(:match?) # for ruby 2.3
|
9
|
+
module RubyBackports
|
10
|
+
refine Regexp do
|
11
|
+
def match?(str)
|
12
|
+
match(str) != nil
|
13
|
+
end
|
14
|
+
end
|
11
15
|
end
|
12
|
-
end
|
13
|
-
end
|
14
16
|
|
15
|
-
|
16
|
-
|
17
|
+
using(RubyBackports)
|
18
|
+
end
|
17
19
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
20
|
+
def self.unsupported_datagram_types(*types)
|
21
|
+
types.each do |type|
|
22
|
+
define_method(type) do |_, _, _, _|
|
23
|
+
raise NotImplementedError, "Type #{type} metrics are not supported by #{self.class.name}."
|
24
|
+
end
|
25
|
+
end
|
22
26
|
end
|
23
|
-
end
|
24
|
-
end
|
25
27
|
|
26
|
-
|
27
|
-
|
28
|
-
|
28
|
+
def self.datagram_class
|
29
|
+
StatsD::Instrument::Datagram
|
30
|
+
end
|
29
31
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
32
|
+
def initialize(prefix: nil, default_tags: nil)
|
33
|
+
@prefix = prefix.nil? ? "" : "#{normalize_name(prefix)}."
|
34
|
+
@default_tags = normalize_tags(default_tags)
|
35
|
+
end
|
34
36
|
|
35
|
-
|
36
|
-
|
37
|
-
|
37
|
+
def c(name, value, sample_rate, tags)
|
38
|
+
generate_generic_datagram(name, value, 'c', sample_rate, tags)
|
39
|
+
end
|
38
40
|
|
39
|
-
|
40
|
-
|
41
|
-
|
41
|
+
def g(name, value, sample_rate, tags)
|
42
|
+
generate_generic_datagram(name, value, 'g', sample_rate, tags)
|
43
|
+
end
|
42
44
|
|
43
|
-
|
44
|
-
|
45
|
-
|
45
|
+
def ms(name, value, sample_rate, tags)
|
46
|
+
generate_generic_datagram(name, value, 'ms', sample_rate, tags)
|
47
|
+
end
|
46
48
|
|
47
|
-
|
48
|
-
|
49
|
-
|
49
|
+
def s(name, value, sample_rate, tags)
|
50
|
+
generate_generic_datagram(name, value, 's', sample_rate, tags)
|
51
|
+
end
|
50
52
|
|
51
|
-
|
52
|
-
|
53
|
-
|
53
|
+
def h(name, value, sample_rate, tags)
|
54
|
+
generate_generic_datagram(name, value, 'h', sample_rate, tags)
|
55
|
+
end
|
54
56
|
|
55
|
-
|
56
|
-
|
57
|
-
|
57
|
+
def d(name, value, sample_rate, tags)
|
58
|
+
generate_generic_datagram(name, value, 'd', sample_rate, tags)
|
59
|
+
end
|
58
60
|
|
59
|
-
|
60
|
-
|
61
|
-
|
61
|
+
def kv(name, value, sample_rate, tags)
|
62
|
+
generate_generic_datagram(name, value, 'kv', sample_rate, tags)
|
63
|
+
end
|
62
64
|
|
63
|
-
|
64
|
-
|
65
|
-
|
65
|
+
def latency_metric_type
|
66
|
+
:ms
|
67
|
+
end
|
66
68
|
|
67
|
-
|
69
|
+
protected
|
68
70
|
|
69
|
-
|
71
|
+
attr_reader :prefix, :default_tags
|
70
72
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
73
|
+
# Utility function to convert tags to the canonical form.
|
74
|
+
#
|
75
|
+
# - Tags specified as key value pairs will be converted into an array
|
76
|
+
# - Tags are normalized to remove unsupported characters
|
77
|
+
#
|
78
|
+
# @param tags [Array<String>, Hash<String, String>, nil] Tags specified in any form.
|
79
|
+
# @return [Array<String>, nil] the list of tags in canonical form.
|
80
|
+
def normalize_tags(tags)
|
81
|
+
return [] unless tags
|
82
|
+
tags = tags.map { |k, v| "#{k}:#{v}" } if tags.is_a?(Hash)
|
81
83
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
84
|
+
# Fast path when no string replacement is needed
|
85
|
+
return tags unless tags.any? { |tag| /[|,]/.match?(tag) }
|
86
|
+
tags.map { |tag| tag.tr('|,', '') }
|
87
|
+
end
|
86
88
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
89
|
+
# Utility function to remove invalid characters from a StatsD metric name
|
90
|
+
def normalize_name(name)
|
91
|
+
# Fast path when no normalization is needed to avoid copying the string
|
92
|
+
return name unless /[:|@]/.match?(name)
|
93
|
+
name.tr(':|@', '_')
|
94
|
+
end
|
93
95
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
96
|
+
def generate_generic_datagram(name, value, type, sample_rate, tags)
|
97
|
+
tags = normalize_tags(tags) + default_tags
|
98
|
+
datagram = +"#{@prefix}#{normalize_name(name)}:#{value}|#{type}"
|
99
|
+
datagram << "|@#{sample_rate}" if sample_rate && sample_rate < 1
|
100
|
+
datagram << "|##{tags.join(',')}" unless tags.empty?
|
101
|
+
datagram
|
102
|
+
end
|
103
|
+
end
|
100
104
|
end
|
101
105
|
end
|
@@ -1,88 +1,92 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
#
|
6
|
-
#
|
7
|
-
class
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
3
|
+
module StatsD
|
4
|
+
module Instrument
|
5
|
+
# The Datagram class parses and inspects a StatsD datagrams
|
6
|
+
#
|
7
|
+
# @note This class is part of the new Client implementation that is intended
|
8
|
+
# to become the new default in the next major release of this library.
|
9
|
+
class DogStatsDDatagram < StatsD::Instrument::Datagram
|
10
|
+
def name
|
11
|
+
@name ||= case type
|
12
|
+
when :_e then parsed_datagram[:name].gsub('\n', "\n")
|
13
|
+
else super
|
14
|
+
end
|
15
|
+
end
|
14
16
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
17
|
+
def value
|
18
|
+
@value ||= case type
|
19
|
+
when :_sc then Integer(parsed_datagram[:value])
|
20
|
+
when :_e then parsed_datagram[:value].gsub('\n', "\n")
|
21
|
+
else super
|
22
|
+
end
|
23
|
+
end
|
22
24
|
|
23
|
-
|
24
|
-
|
25
|
-
|
25
|
+
def hostname
|
26
|
+
parsed_datagram[:hostname]
|
27
|
+
end
|
26
28
|
|
27
|
-
|
28
|
-
|
29
|
-
|
29
|
+
def timestamp
|
30
|
+
Time.at(Integer(parsed_datagram[:timestamp])).utc
|
31
|
+
end
|
30
32
|
|
31
|
-
|
32
|
-
|
33
|
-
|
33
|
+
def aggregation_key
|
34
|
+
parsed_datagram[:aggregation_key]
|
35
|
+
end
|
34
36
|
|
35
|
-
|
36
|
-
|
37
|
-
|
37
|
+
def source_type_name
|
38
|
+
parsed_datagram[:source_type_name]
|
39
|
+
end
|
38
40
|
|
39
|
-
|
40
|
-
|
41
|
-
|
41
|
+
def priority
|
42
|
+
parsed_datagram[:priority]
|
43
|
+
end
|
42
44
|
|
43
|
-
|
44
|
-
|
45
|
-
|
45
|
+
def alert_type
|
46
|
+
parsed_datagram[:alert_type]
|
47
|
+
end
|
46
48
|
|
47
|
-
|
48
|
-
|
49
|
-
|
49
|
+
def message
|
50
|
+
parsed_datagram[:message]
|
51
|
+
end
|
50
52
|
|
51
|
-
|
53
|
+
protected
|
52
54
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
55
|
+
def parsed_datagram
|
56
|
+
@parsed ||= if (match_info = PARSER.match(@source))
|
57
|
+
match_info
|
58
|
+
else
|
59
|
+
raise ArgumentError, "Invalid DogStatsD datagram: #{@source}"
|
60
|
+
end
|
61
|
+
end
|
60
62
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
63
|
+
SERVICE_CHECK_PARSER = %r{
|
64
|
+
\A
|
65
|
+
(?<type>_sc)\|(?<name>[^\|]+)\|(?<value>\d+)
|
66
|
+
(?:\|h:(?<hostname>[^\|]+))?
|
67
|
+
(?:\|d:(?<timestamp>\d+))?
|
68
|
+
(?:\|\#(?<tags>(?:[^\|,]+(?:,[^\|,]+)*)))?
|
69
|
+
(?:\|m:(?<message>[^\|]+))?
|
70
|
+
\n? # In some implementations, the datagram may include a trailing newline.
|
71
|
+
\z
|
72
|
+
}x
|
71
73
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
74
|
+
# |k:my-key|p:low|s:source|t:success|
|
75
|
+
EVENT_PARSER = %r{
|
76
|
+
\A
|
77
|
+
(?<type>_e)\{\d+\,\d+\}:(?<name>[^\|]+)\|(?<value>[^\|]+)
|
78
|
+
(?:\|h:(?<hostname>[^\|]+))?
|
79
|
+
(?:\|d:(?<timestamp>\d+))?
|
80
|
+
(?:\|k:(?<aggregation_key>[^\|]+))?
|
81
|
+
(?:\|p:(?<priority>[^\|]+))?
|
82
|
+
(?:\|s:(?<source_type_name>[^\|]+))?
|
83
|
+
(?:\|t:(?<alert_type>[^\|]+))?
|
84
|
+
(?:\|\#(?<tags>(?:[^\|,]+(?:,[^\|,]+)*)))?
|
85
|
+
\n? # In some implementations, the datagram may include a trailing newline.
|
86
|
+
\z
|
87
|
+
}x
|
86
88
|
|
87
|
-
|
89
|
+
PARSER = Regexp.union(StatsD::Instrument::Datagram::PARSER, SERVICE_CHECK_PARSER, EVENT_PARSER)
|
90
|
+
end
|
91
|
+
end
|
88
92
|
end
|