statsd-instrument 4.0.0 → 4.1.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/CODEOWNERS +1 -1
- data/.github/workflows/benchmark.yml +1 -1
- data/.github/workflows/lint.yml +1 -1
- data/CHANGELOG.md +12 -0
- data/README.md +43 -0
- data/lib/statsd/instrument/aggregator.rb +14 -4
- data/lib/statsd/instrument/client.rb +11 -2
- data/lib/statsd/instrument/compiled_metric.rb +9 -9
- data/lib/statsd/instrument/datagram.rb +3 -3
- data/lib/statsd/instrument/datagram_builder.rb +20 -14
- data/lib/statsd/instrument/dogstatsd_datagram.rb +11 -11
- data/lib/statsd/instrument/dogstatsd_datagram_builder.rb +1 -1
- data/lib/statsd/instrument/environment.rb +4 -0
- data/lib/statsd/instrument/sanitization.rb +34 -0
- data/lib/statsd/instrument/version.rb +1 -1
- data/lib/statsd/instrument.rb +1 -0
- data/test/aggregator_test.rb +98 -0
- data/test/client_test.rb +31 -0
- data/test/datagram_builder_test.rb +16 -0
- data/test/environment_test.rb +13 -0
- data/test/newline_normalization_test.rb +262 -0
- data/test/rubocop/singleton_configuration_test.rb +1 -3
- data/test/sanitization_test.rb +49 -0
- metadata +158 -152
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b823f54e400ae4601e27f794788499896555a0d6cd6aec04fe35b95202b870d2
|
|
4
|
+
data.tar.gz: ae4db5a2b0bcd24d62ece908a60d5bf4e4cc9aee3812ee286bccb31d00edd3d7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 635ac1cbb333c0e82e96fee3e224ceeb2795621b8137c4fca5a5065c2664899857f054c8e5c18e09f32aeace847b470a6613664c917e343972bbc26461688fd1
|
|
7
|
+
data.tar.gz: cd510fb973122ac97b7978649a0fd4ec806e371dd202589c710a58ff8de58d582c6fa8b3ef85b8295c944cc9072926b7552a081a003b75943320bd7ec1819152
|
data/.github/CODEOWNERS
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
* @
|
|
1
|
+
* @Shopify/statsd-instrument-write
|
data/.github/workflows/lint.yml
CHANGED
data/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,18 @@ section below.
|
|
|
6
6
|
|
|
7
7
|
## Unreleased changes
|
|
8
8
|
|
|
9
|
+
## Version 4.1.0
|
|
10
|
+
|
|
11
|
+
- [#430](https://github.com/Shopify/statsd-instrument/pull/430) - Add an opt-in mode that skips the tag sort during aggregation, for StatsD servers that combine metrics with the same
|
|
12
|
+
tags in a different order. Set `STATSD_AGGREGATION_SORT_TAGS=false` or pass `aggregation_sort_tags: false` to
|
|
13
|
+
`Client.new`. Tags are still sorted by default.
|
|
14
|
+
- [#430](https://github.com/Shopify/statsd-instrument/pull/430) - Use `Symbol#name` for Symbol keys and values of hash tags where Ruby provides it (Ruby 3.0 and later), so the
|
|
15
|
+
datagram builder does not allocate a copy of each one.
|
|
16
|
+
|
|
17
|
+
## Version 4.0.1
|
|
18
|
+
|
|
19
|
+
- Normalize ASCII whitespace in metric names and prefixes, and newlines (`\n` and `\r`) in tags and set values, including aggregated and compiled metrics.
|
|
20
|
+
|
|
9
21
|
## Version 4.0.0
|
|
10
22
|
|
|
11
23
|
- Make the internal aggregator interface positional-only and injectable through `Client`, with fixed-arity `increment`, `gauge`, and `aggregate_timing` entrypoints for native aggregation backends. Public Client metric methods retain their keyword API.
|
data/README.md
CHANGED
|
@@ -105,15 +105,58 @@ To enable metric aggregation, set the following environment variables:
|
|
|
105
105
|
- `STATSD_ENABLE_AGGREGATION`: Set this to `true` to enable the experimental aggregation feature. Aggregation is disabled by default.
|
|
106
106
|
- `STATSD_AGGREGATION_INTERVAL`: Specifies the interval (in seconds) at which aggregated metrics are flushed and sent to the StatsD server.
|
|
107
107
|
For example, setting this to `2` will aggregate and send metrics every 2 seconds. Two seconds is also the default value if this environment variable is not set.
|
|
108
|
+
- `STATSD_AGGREGATION_SORT_TAGS`: Set this to `false` to skip the tag sort during aggregation. Tags are sorted by default.
|
|
109
|
+
See [Skipping the tag sort](#skipping-the-tag-sort).
|
|
108
110
|
|
|
109
111
|
Please note that since aggregation is an experimental feature, it should be used with caution in production environments.
|
|
110
112
|
|
|
111
113
|
> [!WARNING]
|
|
112
114
|
> This feature is only compatible with Datadog Agent's version >=6.25.0 && <7.0.0 or Agent's versions >=7.25.0.
|
|
113
115
|
|
|
116
|
+
#### Skipping the tag sort
|
|
117
|
+
|
|
118
|
+
By default, the aggregator sorts the tags of every metric it receives. The sort makes two calls with the same tags
|
|
119
|
+
in a different order aggregate into one sample:
|
|
120
|
+
|
|
121
|
+
```ruby
|
|
122
|
+
StatsD.increment("my.counter", tags: ["a:1", "b:2"])
|
|
123
|
+
StatsD.increment("my.counter", tags: ["b:2", "a:1"])
|
|
124
|
+
# my.counter:2|c|#a:1,b:2
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
The sort runs on every metric call. If your StatsD server combines metrics that have the same tags in a different
|
|
128
|
+
order, as the Datadog Agent does, the server already does this work and you can skip the sort in the client. Set
|
|
129
|
+
`STATSD_AGGREGATION_SORT_TAGS=false`, or pass `aggregation_sort_tags: false` when you create a client yourself:
|
|
130
|
+
|
|
131
|
+
```ruby
|
|
132
|
+
StatsD::Instrument::Client.new(sink: sink, enable_aggregation: true, aggregation_sort_tags: false)
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Without the sort:
|
|
136
|
+
|
|
137
|
+
- The library sends tags in the order you provide them. For hash tags, this is the insertion order of the hash.
|
|
138
|
+
- Calls with the same tags in a different order aggregate separately, and the library sends one sample for each order:
|
|
139
|
+
```
|
|
140
|
+
my.counter:1|c|#a:1,b:2
|
|
141
|
+
my.counter:1|c|#b:2,a:1
|
|
142
|
+
```
|
|
143
|
+
The server adds these together, so counter, histogram, distribution and timing results do not change. The library
|
|
144
|
+
sends more data when your code emits the same tags in many different orders.
|
|
145
|
+
- A gauge that receives the same tags in more than one order during one flush interval sends one sample for each order.
|
|
146
|
+
The server keeps the last sample it receives, which is not always the value you set last. Use one tag order for each
|
|
147
|
+
gauge to avoid this.
|
|
148
|
+
- The aggregator does not change the tag array you pass. With the sort, the aggregator sorts that array in place.
|
|
149
|
+
|
|
150
|
+
Do not skip the sort if your StatsD server treats the same tags in a different order as different metrics.
|
|
151
|
+
|
|
114
152
|
## StatsD keys
|
|
115
153
|
|
|
116
154
|
StatsD keys look like 'admin.logins.api.success'. Dots are used as namespace separators.
|
|
155
|
+
Metric names and prefixes normalize `:`, `|`, `@`, and ASCII whitespace (spaces,
|
|
156
|
+
tabs, and line breaks) to `_`, one underscore per character.
|
|
157
|
+
Newlines (`\n`, `\r`) in set values are also replaced with `_`; in array/hash tags,
|
|
158
|
+
newlines are removed alongside `|` and `,`. Other tag/value whitespace is preserved.
|
|
159
|
+
Normalization is silent.
|
|
117
160
|
|
|
118
161
|
## Usage
|
|
119
162
|
|
|
@@ -98,13 +98,16 @@ module StatsD
|
|
|
98
98
|
# @param default_tags [Array<String>] The tags to add to all metrics.
|
|
99
99
|
# @param flush_interval [Float] The interval at which to flush the aggregated metrics.
|
|
100
100
|
# @param max_values [Integer] The maximum number of values to aggregate before flushing.
|
|
101
|
+
# @param sort_tags [Boolean] Whether to sort tags before aggregating. When false, the same tags
|
|
102
|
+
# in a different order aggregate separately, and the StatsD server has to combine them.
|
|
101
103
|
def initialize(
|
|
102
104
|
sink,
|
|
103
105
|
datagram_builder_class,
|
|
104
106
|
prefix,
|
|
105
107
|
default_tags,
|
|
106
108
|
flush_interval: 5.0,
|
|
107
|
-
max_values: DEFAULT_MAX_CONTEXT_SIZE
|
|
109
|
+
max_values: DEFAULT_MAX_CONTEXT_SIZE,
|
|
110
|
+
sort_tags: true
|
|
108
111
|
)
|
|
109
112
|
@sink = sink
|
|
110
113
|
@datagram_builder_class = datagram_builder_class
|
|
@@ -115,6 +118,7 @@ module StatsD
|
|
|
115
118
|
false: nil,
|
|
116
119
|
}
|
|
117
120
|
@max_values = max_values
|
|
121
|
+
@sort_tags = sort_tags
|
|
118
122
|
|
|
119
123
|
# Mutex protects the aggregation_state and flush_thread from concurrent access
|
|
120
124
|
@aggregation_state_mutex = Mutex.new
|
|
@@ -154,7 +158,7 @@ module StatsD
|
|
|
154
158
|
return
|
|
155
159
|
end
|
|
156
160
|
|
|
157
|
-
tags = tags_sorted(tags)
|
|
161
|
+
tags = @sort_tags ? tags_sorted(tags) : tags_unsorted(tags)
|
|
158
162
|
key = packet_key(name, tags, no_prefix, COUNT, sample_rate: sample_rate)
|
|
159
163
|
|
|
160
164
|
@aggregation_state_mutex.synchronize do
|
|
@@ -232,7 +236,7 @@ module StatsD
|
|
|
232
236
|
return
|
|
233
237
|
end
|
|
234
238
|
|
|
235
|
-
tags = tags_sorted(tags)
|
|
239
|
+
tags = @sort_tags ? tags_sorted(tags) : tags_unsorted(tags)
|
|
236
240
|
key = packet_key(name, tags, no_prefix, type, sample_rate: sample_rate)
|
|
237
241
|
|
|
238
242
|
aggregation_state = nil
|
|
@@ -255,7 +259,7 @@ module StatsD
|
|
|
255
259
|
return
|
|
256
260
|
end
|
|
257
261
|
|
|
258
|
-
tags = tags_sorted(tags)
|
|
262
|
+
tags = @sort_tags ? tags_sorted(tags) : tags_unsorted(tags)
|
|
259
263
|
key = packet_key(name, tags, no_prefix, GAUGE)
|
|
260
264
|
|
|
261
265
|
@aggregation_state_mutex.synchronize do
|
|
@@ -334,6 +338,12 @@ module StatsD
|
|
|
334
338
|
datagram_builder(no_prefix: false).normalize_tags(tags)
|
|
335
339
|
end
|
|
336
340
|
|
|
341
|
+
def tags_unsorted(tags)
|
|
342
|
+
return "" if tags.nil? || tags.empty?
|
|
343
|
+
|
|
344
|
+
datagram_builder(no_prefix: false).normalize_tags(tags)
|
|
345
|
+
end
|
|
346
|
+
|
|
337
347
|
def packet_key(name, tags = "".b, no_prefix = false, type = COUNT, sample_rate: CONST_SAMPLE_RATE)
|
|
338
348
|
AggregationKey.new(
|
|
339
349
|
DatagramBuilder.normalize_string(name),
|
|
@@ -46,6 +46,7 @@ module StatsD
|
|
|
46
46
|
datagram_builder_class: datagram_builder_class,
|
|
47
47
|
enable_aggregation: env.experimental_aggregation_enabled?,
|
|
48
48
|
aggregation_flush_interval: env.aggregation_interval,
|
|
49
|
+
aggregation_sort_tags: env.aggregation_sort_tags?,
|
|
49
50
|
aggregator: aggregator,
|
|
50
51
|
)
|
|
51
52
|
end
|
|
@@ -160,6 +161,10 @@ module StatsD
|
|
|
160
161
|
# +flush+. Calls without tags receive a shared frozen empty array. Other tag
|
|
161
162
|
# values are forwarded as-is; custom backends may require arrays.
|
|
162
163
|
#
|
|
164
|
+
# @param aggregation_sort_tags [Boolean] Whether the built-in aggregator sorts
|
|
165
|
+
# tags before aggregating. Set to false to skip the sort when the StatsD server
|
|
166
|
+
# combines metrics that have the same tags in a different order. Ignored when
|
|
167
|
+
# an external aggregator is injected; the backend decides how to handle tags.
|
|
163
168
|
# @param aggregator [#increment, #gauge, #aggregate_timing, nil]
|
|
164
169
|
# Optional external aggregation backend.
|
|
165
170
|
# @see .from_env to instantiate a client using environment variables.
|
|
@@ -173,6 +178,7 @@ module StatsD
|
|
|
173
178
|
enable_aggregation: false,
|
|
174
179
|
aggregation_flush_interval: 2.0,
|
|
175
180
|
aggregation_max_context_size: StatsD::Instrument::Aggregator::DEFAULT_MAX_CONTEXT_SIZE,
|
|
181
|
+
aggregation_sort_tags: true,
|
|
176
182
|
aggregator: nil
|
|
177
183
|
)
|
|
178
184
|
@sink = sink
|
|
@@ -186,6 +192,7 @@ module StatsD
|
|
|
186
192
|
@injected_aggregator = aggregator
|
|
187
193
|
@enable_aggregation = enable_aggregation || !aggregator.nil?
|
|
188
194
|
@aggregation_flush_interval = aggregation_flush_interval
|
|
195
|
+
@aggregation_sort_tags = aggregation_sort_tags
|
|
189
196
|
if @enable_aggregation
|
|
190
197
|
@aggregator = aggregator ||
|
|
191
198
|
Aggregator.new(
|
|
@@ -195,6 +202,7 @@ module StatsD
|
|
|
195
202
|
default_tags,
|
|
196
203
|
flush_interval: @aggregation_flush_interval,
|
|
197
204
|
max_values: aggregation_max_context_size,
|
|
205
|
+
sort_tags: @aggregation_sort_tags,
|
|
198
206
|
)
|
|
199
207
|
end
|
|
200
208
|
end
|
|
@@ -213,8 +221,8 @@ module StatsD
|
|
|
213
221
|
#
|
|
214
222
|
# - We recommend using `snake_case.metric_names` as naming scheme.
|
|
215
223
|
# - A `.` should be used for namespacing, e.g. `foo.bar.baz`
|
|
216
|
-
# - A metric name should not include
|
|
217
|
-
# The library
|
|
224
|
+
# - A metric name should not include `|`, `@`, `:`, or ASCII whitespace.
|
|
225
|
+
# The library converts each such character to `_`, including in prefixes.
|
|
218
226
|
#
|
|
219
227
|
# @param value [Integer] (default: 1) The value to increment the counter by.
|
|
220
228
|
#
|
|
@@ -640,6 +648,7 @@ module StatsD
|
|
|
640
648
|
datagram_builder_class == NO_CHANGE ? @datagram_builder_class : datagram_builder_class,
|
|
641
649
|
enable_aggregation: @enable_aggregation,
|
|
642
650
|
aggregation_flush_interval: @aggregation_flush_interval,
|
|
651
|
+
aggregation_sort_tags: @aggregation_sort_tags,
|
|
643
652
|
aggregator: aggregator == NO_CHANGE ? @injected_aggregator : aggregator,
|
|
644
653
|
)
|
|
645
654
|
end
|
|
@@ -70,7 +70,8 @@ module StatsD
|
|
|
70
70
|
# Create a new class for this specific metric
|
|
71
71
|
# Using classes instead of instances for better YJIT optimization
|
|
72
72
|
metric_class = tap do
|
|
73
|
-
|
|
73
|
+
# Own a frozen name without freezing a mutable string from the caller.
|
|
74
|
+
@name = -DatagramBlueprintBuilder.normalize_name(name)
|
|
74
75
|
@datagram_blueprint = datagram_blueprint
|
|
75
76
|
@tag_combination_cache = {}
|
|
76
77
|
@max_cache_size = max_cache_size
|
|
@@ -300,9 +301,10 @@ module StatsD
|
|
|
300
301
|
|
|
301
302
|
# Normalizes metric names by replacing special characters
|
|
302
303
|
# @param name [String] The metric name
|
|
303
|
-
# @return [String] The
|
|
304
|
+
# @return [String] The original string if clean, otherwise a new string.
|
|
305
|
+
# Does not copy or freeze clean input; retaining callers own that step.
|
|
304
306
|
def normalize_name(name)
|
|
305
|
-
name
|
|
307
|
+
Sanitization.name(name)
|
|
306
308
|
end
|
|
307
309
|
|
|
308
310
|
private
|
|
@@ -314,16 +316,14 @@ module StatsD
|
|
|
314
316
|
def build_prefix(client_prefix, no_prefix)
|
|
315
317
|
return "" if no_prefix || client_prefix.nil?
|
|
316
318
|
|
|
317
|
-
"#{client_prefix}."
|
|
319
|
+
"#{Sanitization.name(client_prefix.to_s)}."
|
|
318
320
|
end
|
|
319
321
|
|
|
320
322
|
# Normalizes tag names/values by removing StatsD protocol special characters
|
|
321
323
|
# @param str [Symbol, String, Integer, Float] The string to normalize
|
|
322
324
|
# @return [String] The normalized string
|
|
323
325
|
def normalize_statsd_string(str)
|
|
324
|
-
str
|
|
325
|
-
str = str.tr("|,", "") if /[|,]/.match?(str)
|
|
326
|
-
str
|
|
326
|
+
Sanitization.tag(str.to_s)
|
|
327
327
|
end
|
|
328
328
|
|
|
329
329
|
# Compiles all tags (default_tags, static_tags, dynamic_tags) into a single string
|
|
@@ -412,10 +412,10 @@ module StatsD
|
|
|
412
412
|
# Sanitize string and symbol values (other types handled by sprintf %s)
|
|
413
413
|
values = @tag_values.map do |arg|
|
|
414
414
|
if arg.is_a?(String)
|
|
415
|
-
|
|
415
|
+
Sanitization::TAG_PATTERN.match?(arg) ? arg.tr(Sanitization::TAG_CHARACTERS, "") : arg
|
|
416
416
|
elsif arg.is_a?(Symbol)
|
|
417
417
|
str = arg.to_s
|
|
418
|
-
|
|
418
|
+
Sanitization::TAG_PATTERN.match?(str) ? str.tr(Sanitization::TAG_CHARACTERS, "") : str
|
|
419
419
|
else
|
|
420
420
|
arg
|
|
421
421
|
end
|
|
@@ -72,9 +72,9 @@ module StatsD
|
|
|
72
72
|
|
|
73
73
|
PARSER = %r{
|
|
74
74
|
\A
|
|
75
|
-
(?<name>[
|
|
76
|
-
(
|
|
77
|
-
(?:\|\#(?<tags>(?:[
|
|
75
|
+
(?<name>[^:|@]+):(?<value>(?:[^:|@]+:)*[^:|@]+)\|(?<type>c|ms|g|s|h|d)
|
|
76
|
+
(?:\|@(?<sample_rate>\d*(?:\.\d*)?))?
|
|
77
|
+
(?:\|\#(?<tags>(?:[^|,]+(?:,[^|,]+)*)))?
|
|
78
78
|
\n? # In some implementations, the datagram may include a trailing newline.
|
|
79
79
|
\z
|
|
80
80
|
}x.freeze
|
|
@@ -6,6 +6,12 @@ module StatsD
|
|
|
6
6
|
# to become the new default in the next major release of this library.
|
|
7
7
|
class DatagramBuilder
|
|
8
8
|
extend Forwardable
|
|
9
|
+
|
|
10
|
+
# Symbol#name returns the interned string of a symbol, and Symbol#to_s allocates a copy.
|
|
11
|
+
# Symbol#name arrived in Ruby 3.0, and this gem supports Ruby 2.6.
|
|
12
|
+
SYMBOL_HAS_NAME = Symbol.method_defined?(:name)
|
|
13
|
+
private_constant :SYMBOL_HAS_NAME
|
|
14
|
+
|
|
9
15
|
class << self
|
|
10
16
|
def unsupported_datagram_types(*types)
|
|
11
17
|
types.each do |type|
|
|
@@ -20,13 +26,13 @@ module StatsD
|
|
|
20
26
|
end
|
|
21
27
|
|
|
22
28
|
def normalize_string(string)
|
|
23
|
-
string = string.tr("
|
|
29
|
+
string = string.tr("|#\r\n", "_") if /[|#\r\n]/.match?(string)
|
|
24
30
|
string
|
|
25
31
|
end
|
|
26
32
|
end
|
|
27
33
|
|
|
28
34
|
def initialize(prefix: nil, default_tags: nil)
|
|
29
|
-
@prefix = prefix.nil? ? "" : "#{prefix}."
|
|
35
|
+
@prefix = prefix.nil? ? "" : "#{Sanitization.name(prefix.to_s)}."
|
|
30
36
|
@default_tags = default_tags.nil? || default_tags.empty? ? nil : compile_tags(default_tags, "|#".b)
|
|
31
37
|
end
|
|
32
38
|
|
|
@@ -43,6 +49,8 @@ module StatsD
|
|
|
43
49
|
end
|
|
44
50
|
|
|
45
51
|
def s(name, value, sample_rate, tags)
|
|
52
|
+
value = value.to_s
|
|
53
|
+
value = value.tr("\r\n", "_") if /[\r\n]/.match?(value)
|
|
46
54
|
generate_generic_datagram(name, value, "s", sample_rate, tags)
|
|
47
55
|
end
|
|
48
56
|
|
|
@@ -76,16 +84,13 @@ module StatsD
|
|
|
76
84
|
|
|
77
85
|
# Utility function to remove invalid characters from a StatsD metric name
|
|
78
86
|
def normalize_name(name)
|
|
79
|
-
|
|
80
|
-
return name unless /[:|@]/.match?(name)
|
|
81
|
-
|
|
82
|
-
name.tr(":|@", "_")
|
|
87
|
+
Sanitization.name(name)
|
|
83
88
|
end
|
|
84
89
|
|
|
85
90
|
def generate_generic_datagram(name, value, type, sample_rate, tags)
|
|
86
91
|
datagram = "".b <<
|
|
87
92
|
@prefix <<
|
|
88
|
-
(
|
|
93
|
+
(Sanitization::NAME_PATTERN.match?(name) ? name.tr(Sanitization::NAME_CHARACTERS, "_") : name) <<
|
|
89
94
|
":" << value.to_s <<
|
|
90
95
|
"|" << type
|
|
91
96
|
|
|
@@ -105,7 +110,8 @@ module StatsD
|
|
|
105
110
|
|
|
106
111
|
def compile_tags(tags, buffer = "".b)
|
|
107
112
|
if tags.is_a?(String)
|
|
108
|
-
|
|
113
|
+
# String tags are already serialized: commas separate tags here.
|
|
114
|
+
tags = self.class.normalize_string(tags) if Sanitization::TAG_PATTERN.match?(tags)
|
|
109
115
|
buffer << tags
|
|
110
116
|
return buffer
|
|
111
117
|
end
|
|
@@ -117,15 +123,15 @@ module StatsD
|
|
|
117
123
|
else
|
|
118
124
|
buffer << ","
|
|
119
125
|
end
|
|
120
|
-
key = key.to_s
|
|
121
|
-
key = key.tr(
|
|
122
|
-
value = value.to_s
|
|
123
|
-
value = value.tr(
|
|
126
|
+
key = SYMBOL_HAS_NAME && key.is_a?(Symbol) ? key.name : key.to_s
|
|
127
|
+
key = key.tr(Sanitization::TAG_CHARACTERS, "") if Sanitization::TAG_PATTERN.match?(key)
|
|
128
|
+
value = SYMBOL_HAS_NAME && value.is_a?(Symbol) ? value.name : value.to_s
|
|
129
|
+
value = value.tr(Sanitization::TAG_CHARACTERS, "") if Sanitization::TAG_PATTERN.match?(value)
|
|
124
130
|
buffer << key << ":" << value
|
|
125
131
|
end
|
|
126
132
|
else
|
|
127
|
-
if tags.any? { |tag|
|
|
128
|
-
tags = tags.map { |tag| tag
|
|
133
|
+
if tags.any? { |tag| Sanitization::TAG_PATTERN.match?(tag) }
|
|
134
|
+
tags = tags.map { |tag| Sanitization.tag(tag) }
|
|
129
135
|
end
|
|
130
136
|
buffer << tags.join(",")
|
|
131
137
|
end
|
|
@@ -62,11 +62,11 @@ module StatsD
|
|
|
62
62
|
|
|
63
63
|
SERVICE_CHECK_PARSER = %r{
|
|
64
64
|
\A
|
|
65
|
-
(?<type>_sc)\|(?<name>[
|
|
66
|
-
(?:\|h:(?<hostname>[
|
|
65
|
+
(?<type>_sc)\|(?<name>[^|]+)\|(?<value>\d+)
|
|
66
|
+
(?:\|h:(?<hostname>[^|]+))?
|
|
67
67
|
(?:\|d:(?<timestamp>\d+))?
|
|
68
|
-
(?:\|\#(?<tags>(?:[
|
|
69
|
-
(?:\|m:(?<message>[
|
|
68
|
+
(?:\|\#(?<tags>(?:[^|,]+(?:,[^|,]+)*)))?
|
|
69
|
+
(?:\|m:(?<message>[^|]+))?
|
|
70
70
|
\n? # In some implementations, the datagram may include a trailing newline.
|
|
71
71
|
\z
|
|
72
72
|
}x.freeze
|
|
@@ -74,14 +74,14 @@ module StatsD
|
|
|
74
74
|
# |k:my-key|p:low|s:source|t:success|
|
|
75
75
|
EVENT_PARSER = %r{
|
|
76
76
|
\A
|
|
77
|
-
(?<type>_e)\{\d
|
|
78
|
-
(?:\|h:(?<hostname>[
|
|
77
|
+
(?<type>_e)\{\d+,\d+\}:(?<name>[^|]+)\|(?<value>[^|]+)
|
|
78
|
+
(?:\|h:(?<hostname>[^|]+))?
|
|
79
79
|
(?:\|d:(?<timestamp>\d+))?
|
|
80
|
-
(?:\|k:(?<aggregation_key>[
|
|
81
|
-
(?:\|p:(?<priority>[
|
|
82
|
-
(?:\|s:(?<source_type_name>[
|
|
83
|
-
(?:\|t:(?<alert_type>[
|
|
84
|
-
(?:\|\#(?<tags>(?:[
|
|
80
|
+
(?:\|k:(?<aggregation_key>[^|]+))?
|
|
81
|
+
(?:\|p:(?<priority>[^|]+))?
|
|
82
|
+
(?:\|s:(?<source_type_name>[^|]+))?
|
|
83
|
+
(?:\|t:(?<alert_type>[^|]+))?
|
|
84
|
+
(?:\|\#(?<tags>(?:[^|,]+(?:,[^|,]+)*)))?
|
|
85
85
|
\n? # In some implementations, the datagram may include a trailing newline.
|
|
86
86
|
\z
|
|
87
87
|
}x.freeze
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module StatsD
|
|
4
|
+
module Instrument
|
|
5
|
+
# Field-specific wire normalization. Clean strings are returned unchanged;
|
|
6
|
+
# callers retaining a frozen value must take ownership themselves.
|
|
7
|
+
# @api private
|
|
8
|
+
module Sanitization
|
|
9
|
+
# Hot serializers use these same rules inline to avoid per-field Ruby calls.
|
|
10
|
+
NAME_PATTERN = /[:|@\s]/.freeze
|
|
11
|
+
NAME_CHARACTERS = ":|@ \t\r\n\f\v"
|
|
12
|
+
TAG_PATTERN = /[|,\r\n]/.freeze
|
|
13
|
+
TAG_CHARACTERS = "|,\r\n"
|
|
14
|
+
|
|
15
|
+
class << self
|
|
16
|
+
# Replace each ASCII whitespace or protocol delimiter with one underscore.
|
|
17
|
+
# @return [String] The original string if clean, otherwise a new string.
|
|
18
|
+
def name(string)
|
|
19
|
+
NAME_PATTERN.match?(string) ? string.tr(NAME_CHARACTERS, "_") : string
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Tag components allow spaces and colons, but not field/tag separators.
|
|
23
|
+
def tag(string)
|
|
24
|
+
TAG_PATTERN.match?(string) ? string.tr(TAG_CHARACTERS, "") : string
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Keep message whitespace intact; this is not a metric name.
|
|
28
|
+
def service_check_message(string)
|
|
29
|
+
/[:|@\r\n]/.match?(string) ? string.tr(":|@\r\n", "_") : string
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
data/lib/statsd/instrument.rb
CHANGED
|
@@ -391,6 +391,7 @@ require "statsd/instrument/client"
|
|
|
391
391
|
require "statsd/instrument/datagram"
|
|
392
392
|
require "statsd/instrument/aggregator"
|
|
393
393
|
require "statsd/instrument/dogstatsd_datagram"
|
|
394
|
+
require "statsd/instrument/sanitization"
|
|
394
395
|
require "statsd/instrument/datagram_builder"
|
|
395
396
|
require "statsd/instrument/statsd_datagram_builder"
|
|
396
397
|
require "statsd/instrument/dogstatsd_datagram_builder"
|
data/test/aggregator_test.rb
CHANGED
|
@@ -146,6 +146,83 @@ class AggregatorTest < Minitest::Test
|
|
|
146
146
|
assert_equal(2, @sink.datagrams.first.value)
|
|
147
147
|
end
|
|
148
148
|
|
|
149
|
+
def test_unsorted_tags_in_different_orders_aggregate_separately
|
|
150
|
+
aggregator = unsorted_aggregator
|
|
151
|
+
aggregator.increment("foo", 1, ["tag1:val1", "tag2:val2"], false, 1.0)
|
|
152
|
+
aggregator.increment("foo", 1, ["tag1:val1", "tag2:val2"], false, 1.0)
|
|
153
|
+
aggregator.increment("foo", 1, ["tag2:val2", "tag1:val1"], false, 1.0)
|
|
154
|
+
aggregator.flush
|
|
155
|
+
|
|
156
|
+
assert_equal(
|
|
157
|
+
["foo:2|c|#tag1:val1,tag2:val2", "foo:1|c|#tag2:val2,tag1:val1"],
|
|
158
|
+
@sink.datagrams.map(&:source),
|
|
159
|
+
)
|
|
160
|
+
ensure
|
|
161
|
+
aggregator&.instance_variable_get(:@flush_thread)&.kill
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def test_unsorted_tags_keep_hash_order
|
|
165
|
+
aggregator = unsorted_aggregator
|
|
166
|
+
aggregator.aggregate_timing("foo", 1, { tag2: "val2", tag1: "val1" }, false, :d, 1.0)
|
|
167
|
+
aggregator.aggregate_timing("foo", 2, ["tag2:val2", "tag1:val1"], false, :d, 1.0)
|
|
168
|
+
aggregator.gauge("bar", 3, { tag2: "val2", tag1: "val1" }, false)
|
|
169
|
+
aggregator.flush
|
|
170
|
+
|
|
171
|
+
assert_equal(
|
|
172
|
+
["foo:1:2|d|#tag2:val2,tag1:val1", "bar:3|g|#tag2:val2,tag1:val1"],
|
|
173
|
+
@sink.datagrams.map(&:source),
|
|
174
|
+
)
|
|
175
|
+
ensure
|
|
176
|
+
aggregator&.instance_variable_get(:@flush_thread)&.kill
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
def test_unsorted_hash_tags_are_normalized_like_sorted_hash_tags
|
|
180
|
+
tags = { "tool|name": "a,b", pid: 42, kind: :read, "line\nbreak" => true }
|
|
181
|
+
aggregator = unsorted_aggregator
|
|
182
|
+
aggregator.increment("foo", 1, tags, false, 1.0)
|
|
183
|
+
aggregator.flush
|
|
184
|
+
@subject.increment("foo", 1, tags, false, 1.0)
|
|
185
|
+
@subject.flush
|
|
186
|
+
|
|
187
|
+
assert_equal(
|
|
188
|
+
["foo:1|c|#toolname:ab,pid:42,kind:read,linebreak:true", "foo:1|c|#kind:read,linebreak:true,pid:42,toolname:ab"],
|
|
189
|
+
@sink.datagrams.map(&:source),
|
|
190
|
+
)
|
|
191
|
+
ensure
|
|
192
|
+
aggregator&.instance_variable_get(:@flush_thread)&.kill
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
def test_unsorted_tags_accept_a_serialized_string
|
|
196
|
+
aggregator = unsorted_aggregator
|
|
197
|
+
aggregator.increment("foo", 1, ["tag2:val2", "tag1:val1"], false, 1.0)
|
|
198
|
+
aggregator.increment("foo", 1, "tag2:val2,tag1:val1", false, 1.0)
|
|
199
|
+
aggregator.flush
|
|
200
|
+
|
|
201
|
+
assert_equal(["foo:2|c|#tag2:val2,tag1:val1"], @sink.datagrams.map(&:source))
|
|
202
|
+
ensure
|
|
203
|
+
aggregator&.instance_variable_get(:@flush_thread)&.kill
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
def test_unsorted_tags_do_not_mutate_the_caller_tags
|
|
207
|
+
aggregator = unsorted_aggregator
|
|
208
|
+
tags = ["tag2:val2", "tag1:val1"].freeze
|
|
209
|
+
aggregator.increment("foo", 1, tags, false, 1.0)
|
|
210
|
+
aggregator.flush
|
|
211
|
+
|
|
212
|
+
assert_equal(["tag2:val2", "tag1:val1"], tags)
|
|
213
|
+
assert_equal("foo:1|c|#tag2:val2,tag1:val1", @sink.datagrams.first.source)
|
|
214
|
+
ensure
|
|
215
|
+
aggregator&.instance_variable_get(:@flush_thread)&.kill
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
def test_hash_tags_raise_at_the_call_site_without_tag_support_when_sorting
|
|
219
|
+
assert_hash_tags_raise_at_the_call_site(sort_tags: true)
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
def test_hash_tags_raise_at_the_call_site_without_tag_support_when_not_sorting
|
|
223
|
+
assert_hash_tags_raise_at_the_call_site(sort_tags: false)
|
|
224
|
+
end
|
|
225
|
+
|
|
149
226
|
def test_increment_with_different_tag_values
|
|
150
227
|
@subject.increment("foo", 1, ["tag1:val1", "tag2:val2"], false, 1.0)
|
|
151
228
|
@subject.increment("foo", 1, { tag1: "val1", tag2: "val2" }, false, 1.0)
|
|
@@ -473,4 +550,25 @@ class AggregatorTest < Minitest::Test
|
|
|
473
550
|
ensure
|
|
474
551
|
Signal.trap("USR1", old_trap || "DEFAULT")
|
|
475
552
|
end
|
|
553
|
+
|
|
554
|
+
private
|
|
555
|
+
|
|
556
|
+
def unsorted_aggregator
|
|
557
|
+
StatsD::Instrument::Aggregator.new(@sink, StatsD::Instrument::DatagramBuilder, nil, [], sort_tags: false)
|
|
558
|
+
end
|
|
559
|
+
|
|
560
|
+
def assert_hash_tags_raise_at_the_call_site(sort_tags:)
|
|
561
|
+
aggregator = StatsD::Instrument::Aggregator.new(
|
|
562
|
+
@sink, StatsD::Instrument::StatsDDatagramBuilder, nil, [], sort_tags: sort_tags
|
|
563
|
+
)
|
|
564
|
+
tags = { tag2: "val2", tag1: "val1" }
|
|
565
|
+
|
|
566
|
+
assert_raises(NotImplementedError) { aggregator.increment("foo", 1, tags, false, 1.0) }
|
|
567
|
+
assert_raises(NotImplementedError) { aggregator.aggregate_timing("foo", 1, tags, false, :ms, 1.0) }
|
|
568
|
+
assert_raises(NotImplementedError) { aggregator.gauge("bar", 1, tags, false) }
|
|
569
|
+
aggregator.flush
|
|
570
|
+
assert_empty(@sink.datagrams)
|
|
571
|
+
ensure
|
|
572
|
+
aggregator&.instance_variable_get(:@flush_thread)&.kill
|
|
573
|
+
end
|
|
476
574
|
end
|
data/test/client_test.rb
CHANGED
|
@@ -166,6 +166,37 @@ class ClientTest < Minitest::Test
|
|
|
166
166
|
clone&.instance_variable_get(:@aggregator)&.instance_variable_get(:@flush_thread)&.kill
|
|
167
167
|
end
|
|
168
168
|
|
|
169
|
+
def test_client_from_env_without_aggregation_tag_sorting
|
|
170
|
+
env = StatsD::Instrument::Environment.new(
|
|
171
|
+
"STATSD_ENABLE_AGGREGATION" => "true",
|
|
172
|
+
"STATSD_AGGREGATION_SORT_TAGS" => "false",
|
|
173
|
+
)
|
|
174
|
+
client = StatsD::Instrument::Client.from_env(
|
|
175
|
+
env,
|
|
176
|
+
sink: StatsD::Instrument::CaptureSink.new(parent: StatsD::Instrument::NullSink.new),
|
|
177
|
+
)
|
|
178
|
+
clone = client.clone_with_options(prefix: "clone")
|
|
179
|
+
|
|
180
|
+
[client, clone].each do |c|
|
|
181
|
+
c.increment("requests", tags: ["b:2", "a:1"])
|
|
182
|
+
c.increment("requests", tags: ["a:1", "b:2"])
|
|
183
|
+
c.force_flush
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
assert_equal(
|
|
187
|
+
[
|
|
188
|
+
"requests:1|c|#b:2,a:1",
|
|
189
|
+
"requests:1|c|#a:1,b:2",
|
|
190
|
+
"clone.requests:1|c|#b:2,a:1",
|
|
191
|
+
"clone.requests:1|c|#a:1,b:2",
|
|
192
|
+
],
|
|
193
|
+
client.sink.datagrams.map(&:source),
|
|
194
|
+
)
|
|
195
|
+
ensure
|
|
196
|
+
client&.instance_variable_get(:@aggregator)&.instance_variable_get(:@flush_thread)&.kill
|
|
197
|
+
clone&.instance_variable_get(:@aggregator)&.instance_variable_get(:@flush_thread)&.kill
|
|
198
|
+
end
|
|
199
|
+
|
|
169
200
|
def test_capture
|
|
170
201
|
inner_datagrams = nil
|
|
171
202
|
|