statsd-instrument 3.0.1 → 3.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/lint.yml +22 -0
- data/.github/workflows/{ci.yml → tests.yml} +3 -21
- data/.rubocop.yml +2 -1
- data/CHANGELOG.md +18 -0
- data/Gemfile +8 -10
- data/README.md +7 -4
- data/Rakefile +6 -6
- data/benchmark/send-metrics-to-dev-null-log +14 -14
- data/benchmark/send-metrics-to-local-udp-receiver +18 -18
- data/lib/statsd/instrument/assertions.rb +7 -7
- data/lib/statsd/instrument/batched_udp_sink.rb +159 -0
- data/lib/statsd/instrument/client.rb +3 -3
- data/lib/statsd/instrument/datagram.rb +1 -1
- data/lib/statsd/instrument/datagram_builder.rb +10 -22
- data/lib/statsd/instrument/dogstatsd_datagram_builder.rb +2 -2
- data/lib/statsd/instrument/environment.rb +19 -11
- data/lib/statsd/instrument/expectation.rb +6 -18
- data/lib/statsd/instrument/matchers.rb +8 -4
- data/lib/statsd/instrument/railtie.rb +1 -1
- data/lib/statsd/instrument/rubocop/measure_as_dist_argument.rb +1 -1
- data/lib/statsd/instrument/rubocop/metaprogramming_positional_arguments.rb +2 -2
- data/lib/statsd/instrument/rubocop/metric_prefix_argument.rb +1 -1
- data/lib/statsd/instrument/rubocop/metric_return_value.rb +3 -3
- data/lib/statsd/instrument/rubocop/metric_value_keyword_argument.rb +1 -1
- data/lib/statsd/instrument/rubocop/positional_arguments.rb +4 -4
- data/lib/statsd/instrument/rubocop/singleton_configuration.rb +1 -1
- data/lib/statsd/instrument/rubocop/splat_arguments.rb +2 -2
- data/lib/statsd/instrument/rubocop.rb +13 -34
- data/lib/statsd/instrument/strict.rb +1 -1
- data/lib/statsd/instrument/udp_sink.rb +11 -13
- data/lib/statsd/instrument/version.rb +1 -1
- data/lib/statsd/instrument.rb +56 -59
- data/lib/statsd-instrument.rb +1 -1
- data/statsd-instrument.gemspec +2 -0
- data/test/assertions_test.rb +200 -155
- data/test/benchmark/clock_gettime.rb +1 -1
- data/test/benchmark/metrics.rb +8 -8
- data/test/benchmark/tags.rb +4 -4
- data/test/capture_sink_test.rb +11 -11
- data/test/client_test.rb +64 -64
- data/test/datagram_builder_test.rb +41 -41
- data/test/datagram_test.rb +5 -5
- data/test/dogstatsd_datagram_builder_test.rb +22 -22
- data/test/environment_test.rb +26 -17
- data/test/helpers/rubocop_helper.rb +3 -3
- data/test/helpers_test.rb +12 -12
- data/test/integration_test.rb +6 -6
- data/test/log_sink_test.rb +2 -2
- data/test/matchers_test.rb +46 -46
- data/test/null_sink_test.rb +2 -2
- data/test/rubocop/measure_as_dist_argument_test.rb +2 -2
- data/test/rubocop/metaprogramming_positional_arguments_test.rb +2 -2
- data/test/rubocop/metric_prefix_argument_test.rb +2 -2
- data/test/rubocop/metric_return_value_test.rb +3 -3
- data/test/rubocop/metric_value_keyword_argument_test.rb +3 -3
- data/test/rubocop/positional_arguments_test.rb +2 -2
- data/test/rubocop/singleton_configuration_test.rb +8 -8
- data/test/rubocop/splat_arguments_test.rb +2 -2
- data/test/statsd_datagram_builder_test.rb +6 -6
- data/test/statsd_instrumentation_test.rb +104 -104
- data/test/statsd_test.rb +35 -35
- data/test/test_helper.rb +13 -6
- data/test/udp_sink_test.rb +142 -44
- metadata +21 -7
- data/test/benchmark/default_tags.rb +0 -47
data/test/benchmark/metrics.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require "statsd-instrument"
|
4
|
+
require "benchmark/ips"
|
5
5
|
|
6
6
|
def helper_function
|
7
7
|
a = 10
|
@@ -12,28 +12,28 @@ end
|
|
12
12
|
|
13
13
|
Benchmark.ips do |bench|
|
14
14
|
bench.report("increment metric benchmark") do
|
15
|
-
StatsD.increment(
|
15
|
+
StatsD.increment("GoogleBase.insert", 10)
|
16
16
|
end
|
17
17
|
|
18
18
|
bench.report("measure metric benchmark") do
|
19
|
-
StatsD.measure(
|
19
|
+
StatsD.measure("helper_function") do
|
20
20
|
helper_function
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
24
|
bench.report("gauge metric benchmark") do
|
25
|
-
StatsD.gauge(
|
25
|
+
StatsD.gauge("GoogleBase.insert", 12)
|
26
26
|
end
|
27
27
|
|
28
28
|
bench.report("set metric benchmark") do
|
29
|
-
StatsD.set(
|
29
|
+
StatsD.set("GoogleBase.customers", "12345", sample_rate: 1.0)
|
30
30
|
end
|
31
31
|
|
32
32
|
bench.report("event metric benchmark") do
|
33
|
-
StatsD.event(
|
33
|
+
StatsD.event("Event Title", "12345")
|
34
34
|
end
|
35
35
|
|
36
36
|
bench.report("service check metric benchmark") do
|
37
|
-
StatsD.service_check(
|
37
|
+
StatsD.service_check("shipit.redis_connection", "ok")
|
38
38
|
end
|
39
39
|
end
|
data/test/benchmark/tags.rb
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require "statsd-instrument"
|
4
|
+
require "benchmark/ips"
|
5
5
|
|
6
6
|
Benchmark.ips do |bench|
|
7
7
|
bench.report("normalized tags with simple hash") do
|
8
|
-
StatsD::Instrument::Metric.normalize_tags(tag:
|
8
|
+
StatsD::Instrument::Metric.normalize_tags(tag: "value")
|
9
9
|
end
|
10
10
|
|
11
11
|
bench.report("normalized tags with simple array") do
|
12
|
-
StatsD::Instrument::Metric.normalize_tags([
|
12
|
+
StatsD::Instrument::Metric.normalize_tags(["test:test"])
|
13
13
|
end
|
14
14
|
|
15
15
|
bench.report("normalized tags with large hash") do
|
data/test/capture_sink_test.rb
CHANGED
@@ -1,23 +1,23 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "test_helper"
|
4
4
|
|
5
5
|
class CaptureSinkTest < Minitest::Test
|
6
6
|
def test_capture_sink_captures_datagram_instances
|
7
7
|
capture_sink = StatsD::Instrument::CaptureSink.new(parent: [])
|
8
|
-
capture_sink <<
|
8
|
+
capture_sink << "foo:1|c"
|
9
9
|
|
10
10
|
assert_equal(1, capture_sink.datagrams.length)
|
11
11
|
assert_kind_of(StatsD::Instrument::Datagram, capture_sink.datagrams.first)
|
12
|
-
assert_equal(
|
12
|
+
assert_equal("foo:1|c", capture_sink.datagrams.first.source)
|
13
13
|
end
|
14
14
|
|
15
15
|
def test_capture_sink_sends_datagrams_to_parent
|
16
16
|
parent = []
|
17
17
|
capture_sink = StatsD::Instrument::CaptureSink.new(parent: parent)
|
18
|
-
capture_sink <<
|
18
|
+
capture_sink << "foo:1|c" << "bar:1|c"
|
19
19
|
|
20
|
-
assert_equal([
|
20
|
+
assert_equal(["foo:1|c", "bar:1|c"], parent)
|
21
21
|
end
|
22
22
|
|
23
23
|
def test_nesting_capture_sink_instances
|
@@ -25,18 +25,18 @@ class CaptureSinkTest < Minitest::Test
|
|
25
25
|
outer_capture_sink = StatsD::Instrument::CaptureSink.new(parent: null_sink)
|
26
26
|
inner_capture_sink = StatsD::Instrument::CaptureSink.new(parent: outer_capture_sink)
|
27
27
|
|
28
|
-
outer_capture_sink <<
|
29
|
-
inner_capture_sink <<
|
28
|
+
outer_capture_sink << "foo:1|c"
|
29
|
+
inner_capture_sink << "bar:1|c"
|
30
30
|
|
31
|
-
assert_equal([
|
32
|
-
assert_equal([
|
31
|
+
assert_equal(["foo:1|c", "bar:1|c"], outer_capture_sink.datagrams.map(&:source))
|
32
|
+
assert_equal(["bar:1|c"], inner_capture_sink.datagrams.map(&:source))
|
33
33
|
end
|
34
34
|
|
35
35
|
def test_using_a_different_datagram_class
|
36
36
|
sink = StatsD::Instrument::CaptureSink.new(parent: [], datagram_class: String)
|
37
|
-
sink <<
|
37
|
+
sink << "foo:1|c"
|
38
38
|
|
39
39
|
assert(sink.datagrams.all? { |datagram| datagram.is_a?(String) })
|
40
|
-
assert_equal([
|
40
|
+
assert_equal(["foo:1|c"], sink.datagrams)
|
41
41
|
end
|
42
42
|
end
|
data/test/client_test.rb
CHANGED
@@ -1,31 +1,31 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "test_helper"
|
4
4
|
|
5
5
|
class ClientTest < Minitest::Test
|
6
6
|
def setup
|
7
7
|
@client = StatsD::Instrument::Client.new(datagram_builder_class: StatsD::Instrument::StatsDDatagramBuilder)
|
8
|
-
@dogstatsd_client = StatsD::Instrument::Client.new(implementation:
|
8
|
+
@dogstatsd_client = StatsD::Instrument::Client.new(implementation: "datadog")
|
9
9
|
end
|
10
10
|
|
11
11
|
def test_client_from_env
|
12
12
|
env = StatsD::Instrument::Environment.new(
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
13
|
+
"STATSD_ENV" => "production",
|
14
|
+
"STATSD_SAMPLE_RATE" => "0.1",
|
15
|
+
"STATSD_PREFIX" => "foo",
|
16
|
+
"STATSD_DEFAULT_TAGS" => "shard:1,env:production",
|
17
|
+
"STATSD_IMPLEMENTATION" => "statsd",
|
18
|
+
"STATSD_ADDR" => "1.2.3.4:8125",
|
19
19
|
)
|
20
20
|
client = StatsD::Instrument::Client.from_env(env)
|
21
21
|
|
22
22
|
assert_equal(0.1, client.default_sample_rate)
|
23
|
-
assert_equal(
|
24
|
-
assert_equal([
|
23
|
+
assert_equal("foo", client.prefix)
|
24
|
+
assert_equal(["shard:1", "env:production"], client.default_tags)
|
25
25
|
assert_equal(StatsD::Instrument::StatsDDatagramBuilder, client.datagram_builder_class)
|
26
26
|
|
27
|
-
assert_kind_of(StatsD::Instrument::
|
28
|
-
assert_equal(
|
27
|
+
assert_kind_of(StatsD::Instrument::BatchedUDPSink, client.sink)
|
28
|
+
assert_equal("1.2.3.4", client.sink.host)
|
29
29
|
assert_equal(8125, client.sink.port)
|
30
30
|
end
|
31
31
|
|
@@ -42,18 +42,18 @@ class ClientTest < Minitest::Test
|
|
42
42
|
|
43
43
|
def test_client_from_env_with_overrides
|
44
44
|
env = StatsD::Instrument::Environment.new(
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
45
|
+
"STATSD_SAMPLE_RATE" => "0.1",
|
46
|
+
"STATSD_PREFIX" => "foo",
|
47
|
+
"STATSD_DEFAULT_TAGS" => "shard:1,env:production",
|
48
|
+
"STATSD_IMPLEMENTATION" => "statsd",
|
49
|
+
"STATSD_ADDR" => "1.2.3.4:8125",
|
50
50
|
)
|
51
51
|
client = StatsD::Instrument::Client.from_env(env,
|
52
|
-
prefix:
|
52
|
+
prefix: "bar", implementation: "dogstatsd", sink: StatsD::Instrument::NullSink.new)
|
53
53
|
|
54
54
|
assert_equal(0.1, client.default_sample_rate)
|
55
|
-
assert_equal(
|
56
|
-
assert_equal([
|
55
|
+
assert_equal("bar", client.prefix)
|
56
|
+
assert_equal(["shard:1", "env:production"], client.default_tags)
|
57
57
|
assert_equal(StatsD::Instrument::DogStatsDDatagramBuilder, client.datagram_builder_class)
|
58
58
|
|
59
59
|
assert_kind_of(StatsD::Instrument::NullSink, client.sink)
|
@@ -62,75 +62,75 @@ class ClientTest < Minitest::Test
|
|
62
62
|
def test_capture
|
63
63
|
inner_datagrams = nil
|
64
64
|
|
65
|
-
@client.increment(
|
65
|
+
@client.increment("foo")
|
66
66
|
outer_datagrams = @client.capture do
|
67
|
-
@client.increment(
|
67
|
+
@client.increment("bar")
|
68
68
|
inner_datagrams = @client.capture do
|
69
|
-
@client.increment(
|
69
|
+
@client.increment("baz")
|
70
70
|
end
|
71
71
|
end
|
72
|
-
@client.increment(
|
72
|
+
@client.increment("quc")
|
73
73
|
|
74
|
-
assert_equal([
|
75
|
-
assert_equal([
|
74
|
+
assert_equal(["bar", "baz"], outer_datagrams.map(&:name))
|
75
|
+
assert_equal(["baz"], inner_datagrams.map(&:name))
|
76
76
|
end
|
77
77
|
|
78
78
|
def test_metric_methods_return_truish_void
|
79
|
-
assert(@client.increment(
|
80
|
-
assert(@client.measure(
|
81
|
-
assert(@client.set(
|
82
|
-
assert(@client.gauge(
|
79
|
+
assert(@client.increment("foo"))
|
80
|
+
assert(@client.measure("bar", 122.54))
|
81
|
+
assert(@client.set("baz", 123))
|
82
|
+
assert(@client.gauge("baz", 12.3))
|
83
83
|
end
|
84
84
|
|
85
85
|
def test_increment_with_default_value
|
86
|
-
datagrams = @client.capture { @client.increment(
|
86
|
+
datagrams = @client.capture { @client.increment("foo") }
|
87
87
|
assert_equal(1, datagrams.size)
|
88
|
-
assert_equal(
|
88
|
+
assert_equal("foo:1|c", datagrams.first.source)
|
89
89
|
end
|
90
90
|
|
91
91
|
def test_measure_with_value
|
92
|
-
datagrams = @client.capture { @client.measure(
|
92
|
+
datagrams = @client.capture { @client.measure("foo", 122.54) }
|
93
93
|
assert_equal(1, datagrams.size)
|
94
|
-
assert_equal(
|
94
|
+
assert_equal("foo:122.54|ms", datagrams.first.source)
|
95
95
|
end
|
96
96
|
|
97
97
|
def test_measure_with_block
|
98
98
|
Process.stubs(:clock_gettime).with(Process::CLOCK_MONOTONIC).returns(0.1, 0.2)
|
99
99
|
datagrams = @client.capture do
|
100
|
-
@client.measure(
|
100
|
+
@client.measure("foo") {}
|
101
101
|
end
|
102
102
|
assert_equal(1, datagrams.size)
|
103
|
-
assert_equal(
|
103
|
+
assert_equal("foo:100.0|ms", datagrams.first.source)
|
104
104
|
end
|
105
105
|
|
106
106
|
def test_gauge
|
107
|
-
datagrams = @client.capture { @client.gauge(
|
107
|
+
datagrams = @client.capture { @client.gauge("foo", 123) }
|
108
108
|
assert_equal(1, datagrams.size)
|
109
|
-
assert_equal(
|
109
|
+
assert_equal("foo:123|g", datagrams.first.source)
|
110
110
|
end
|
111
111
|
|
112
112
|
def test_set
|
113
|
-
datagrams = @client.capture { @client.set(
|
113
|
+
datagrams = @client.capture { @client.set("foo", 12345) }
|
114
114
|
assert_equal(1, datagrams.size)
|
115
|
-
assert_equal(
|
115
|
+
assert_equal("foo:12345|s", datagrams.first.source)
|
116
116
|
end
|
117
117
|
|
118
118
|
def test_histogram
|
119
|
-
datagrams = @dogstatsd_client.capture { @dogstatsd_client.histogram(
|
119
|
+
datagrams = @dogstatsd_client.capture { @dogstatsd_client.histogram("foo", 12.44) }
|
120
120
|
assert_equal(1, datagrams.size)
|
121
|
-
assert_equal(
|
121
|
+
assert_equal("foo:12.44|h", datagrams.first.source)
|
122
122
|
end
|
123
123
|
|
124
124
|
def test_distribution_with_value
|
125
|
-
datagrams = @dogstatsd_client.capture { @dogstatsd_client.distribution(
|
125
|
+
datagrams = @dogstatsd_client.capture { @dogstatsd_client.distribution("foo", 12.44) }
|
126
126
|
assert_equal(1, datagrams.size)
|
127
|
-
assert_equal(
|
127
|
+
assert_equal("foo:12.44|d", datagrams.first.source)
|
128
128
|
end
|
129
129
|
|
130
130
|
def test_distribution_with_block
|
131
131
|
Process.stubs(:clock_gettime).with(Process::CLOCK_MONOTONIC).returns(0.1, 0.2)
|
132
132
|
datagrams = @dogstatsd_client.capture do
|
133
|
-
@dogstatsd_client.distribution(
|
133
|
+
@dogstatsd_client.distribution("foo") {}
|
134
134
|
end
|
135
135
|
assert_equal(1, datagrams.size)
|
136
136
|
assert_equal("foo:100.0|d", datagrams.first.source)
|
@@ -139,7 +139,7 @@ class ClientTest < Minitest::Test
|
|
139
139
|
def test_latency_emits_ms_metric
|
140
140
|
Process.stubs(:clock_gettime).with(Process::CLOCK_MONOTONIC).returns(0.1, 0.2)
|
141
141
|
datagrams = @client.capture do
|
142
|
-
@client.latency(
|
142
|
+
@client.latency("foo") {}
|
143
143
|
end
|
144
144
|
assert_equal(1, datagrams.size)
|
145
145
|
assert_equal("foo:100.0|ms", datagrams.first.source)
|
@@ -148,7 +148,7 @@ class ClientTest < Minitest::Test
|
|
148
148
|
def test_latency_on_dogstatsd_prefers_distribution_metric_type
|
149
149
|
Process.stubs(:clock_gettime).with(Process::CLOCK_MONOTONIC).returns(0.1, 0.2)
|
150
150
|
datagrams = @dogstatsd_client.capture do
|
151
|
-
@dogstatsd_client.latency(
|
151
|
+
@dogstatsd_client.latency("foo") {}
|
152
152
|
end
|
153
153
|
assert_equal(1, datagrams.size)
|
154
154
|
assert_equal("foo:100.0|d", datagrams.first.source)
|
@@ -157,28 +157,28 @@ class ClientTest < Minitest::Test
|
|
157
157
|
def test_latency_calls_block_even_when_not_sending_a_sample
|
158
158
|
called = false
|
159
159
|
@client.capture do
|
160
|
-
@client.latency(
|
160
|
+
@client.latency("foo", sample_rate: 0) { called = true }
|
161
161
|
end
|
162
162
|
assert(called, "The block should have been called")
|
163
163
|
end
|
164
164
|
|
165
165
|
def test_service_check
|
166
|
-
datagrams = @dogstatsd_client.capture { @dogstatsd_client.service_check(
|
166
|
+
datagrams = @dogstatsd_client.capture { @dogstatsd_client.service_check("service", :ok) }
|
167
167
|
assert_equal(1, datagrams.size)
|
168
168
|
assert_equal("_sc|service|0", datagrams.first.source)
|
169
169
|
end
|
170
170
|
|
171
171
|
def test_event
|
172
|
-
datagrams = @dogstatsd_client.capture { @dogstatsd_client.event(
|
172
|
+
datagrams = @dogstatsd_client.capture { @dogstatsd_client.event("service", "event\ndescription") }
|
173
173
|
assert_equal(1, datagrams.size)
|
174
174
|
assert_equal("_e{7,18}:service|event\\ndescription", datagrams.first.source)
|
175
175
|
end
|
176
176
|
|
177
177
|
def test_no_prefix
|
178
|
-
client = StatsD::Instrument::Client.new(prefix:
|
178
|
+
client = StatsD::Instrument::Client.new(prefix: "foo")
|
179
179
|
datagrams = client.capture do
|
180
|
-
client.increment(
|
181
|
-
client.increment(
|
180
|
+
client.increment("bar")
|
181
|
+
client.increment("bar", no_prefix: true)
|
182
182
|
end
|
183
183
|
|
184
184
|
assert_equal(2, datagrams.size)
|
@@ -187,36 +187,36 @@ class ClientTest < Minitest::Test
|
|
187
187
|
end
|
188
188
|
|
189
189
|
def test_default_tags_normalization
|
190
|
-
client = StatsD::Instrument::Client.new(default_tags: { first_tag:
|
190
|
+
client = StatsD::Instrument::Client.new(default_tags: { first_tag: "f|irst_value", second_tag: "sec,ond_value" })
|
191
191
|
datagrams = client.capture do
|
192
|
-
client.increment(
|
192
|
+
client.increment("bar", tags: ["th|ird_#,tag"])
|
193
193
|
end
|
194
194
|
|
195
|
-
assert_includes(datagrams.first.tags,
|
196
|
-
assert_includes(datagrams.first.tags,
|
197
|
-
assert_includes(datagrams.first.tags,
|
195
|
+
assert_includes(datagrams.first.tags, "first_tag:first_value")
|
196
|
+
assert_includes(datagrams.first.tags, "second_tag:second_value")
|
197
|
+
assert_includes(datagrams.first.tags, "third_#tag")
|
198
198
|
end
|
199
199
|
|
200
200
|
def test_sampling
|
201
|
-
mock_sink = mock(
|
201
|
+
mock_sink = mock("sink")
|
202
202
|
mock_sink.stubs(:sample?).returns(false, true, false, false, true)
|
203
203
|
mock_sink.expects(:<<).twice
|
204
204
|
|
205
205
|
client = StatsD::Instrument::Client.new(sink: mock_sink)
|
206
|
-
5.times { client.increment(
|
206
|
+
5.times { client.increment("metric") }
|
207
207
|
end
|
208
208
|
|
209
209
|
def test_clone_with_prefix_option
|
210
210
|
# Both clients will use the same sink.
|
211
|
-
mock_sink = mock(
|
211
|
+
mock_sink = mock("sink")
|
212
212
|
mock_sink.stubs(:sample?).returns(true)
|
213
213
|
mock_sink.expects(:<<).with("metric:1|c").returns(mock_sink)
|
214
214
|
mock_sink.expects(:<<).with("foo.metric:1|c").returns(mock_sink)
|
215
215
|
|
216
216
|
original_client = StatsD::Instrument::Client.new(sink: mock_sink)
|
217
|
-
client_with_other_options = original_client.clone_with_options(prefix:
|
217
|
+
client_with_other_options = original_client.clone_with_options(prefix: "foo")
|
218
218
|
|
219
|
-
original_client.increment(
|
220
|
-
client_with_other_options.increment(
|
219
|
+
original_client.increment("metric")
|
220
|
+
client_with_other_options.increment("metric")
|
221
221
|
end
|
222
222
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "test_helper"
|
4
4
|
|
5
5
|
class DatagramBuilderTest < Minitest::Test
|
6
6
|
def setup
|
@@ -8,111 +8,111 @@ class DatagramBuilderTest < Minitest::Test
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def test_normalize_name
|
11
|
-
assert_equal(
|
12
|
-
assert_equal(
|
13
|
-
assert_equal(
|
14
|
-
assert_equal(
|
11
|
+
assert_equal("foo", @datagram_builder.send(:normalize_name, "foo"))
|
12
|
+
assert_equal("fo_o", @datagram_builder.send(:normalize_name, "fo|o"))
|
13
|
+
assert_equal("fo_o", @datagram_builder.send(:normalize_name, "fo@o"))
|
14
|
+
assert_equal("fo_o", @datagram_builder.send(:normalize_name, "fo:o"))
|
15
15
|
end
|
16
16
|
|
17
17
|
def test_normalize_unsupported_tag_names
|
18
|
-
assert_equal([
|
19
|
-
#
|
18
|
+
assert_equal(["ign#ored"], @datagram_builder.send(:normalize_tags, ["ign#o|re,d"]))
|
19
|
+
# NOTE: how this is interpreted by the backend is undefined.
|
20
20
|
# We rely on the user to not do stuff like this if they don't want to be surprised.
|
21
21
|
# We do not want to take the performance hit of normalizing this.
|
22
|
-
assert_equal([
|
22
|
+
assert_equal(["lol::class:omg::lol"], @datagram_builder.send(:normalize_tags, "lol::class" => "omg::lol"))
|
23
23
|
end
|
24
24
|
|
25
25
|
def test_normalize_tags_converts_hash_to_array
|
26
|
-
assert_equal([
|
27
|
-
assert_equal([
|
26
|
+
assert_equal(["tag:value"], @datagram_builder.send(:normalize_tags, tag: "value"))
|
27
|
+
assert_equal(["tag1:v1", "tag2:v2"], @datagram_builder.send(:normalize_tags, tag1: "v1", tag2: "v2"))
|
28
28
|
end
|
29
29
|
|
30
30
|
def test_c
|
31
|
-
datagram = @datagram_builder.c(
|
31
|
+
datagram = @datagram_builder.c("foo", 1, nil, nil)
|
32
32
|
assert_equal("foo:1|c", datagram)
|
33
33
|
|
34
|
-
datagram = @datagram_builder.c(
|
34
|
+
datagram = @datagram_builder.c("fo:o", 10, 0.1, nil)
|
35
35
|
assert_equal("fo_o:10|c|@0.1", datagram)
|
36
36
|
end
|
37
37
|
|
38
38
|
def test_ms
|
39
|
-
datagram = @datagram_builder.ms(
|
39
|
+
datagram = @datagram_builder.ms("foo", 1, nil, nil)
|
40
40
|
assert_equal("foo:1|ms", datagram)
|
41
41
|
|
42
|
-
datagram = @datagram_builder.ms(
|
42
|
+
datagram = @datagram_builder.ms("fo:o", 10, 0.1, nil)
|
43
43
|
assert_equal("fo_o:10|ms|@0.1", datagram)
|
44
44
|
end
|
45
45
|
|
46
46
|
def test_g
|
47
|
-
datagram = @datagram_builder.g(
|
47
|
+
datagram = @datagram_builder.g("foo", 1, nil, nil)
|
48
48
|
assert_equal("foo:1|g", datagram)
|
49
49
|
|
50
|
-
datagram = @datagram_builder.g(
|
50
|
+
datagram = @datagram_builder.g("fo|o", 10, 0.01, nil)
|
51
51
|
assert_equal("fo_o:10|g|@0.01", datagram)
|
52
52
|
end
|
53
53
|
|
54
54
|
def test_s
|
55
|
-
datagram = @datagram_builder.s(
|
55
|
+
datagram = @datagram_builder.s("foo", 1, nil, nil)
|
56
56
|
assert_equal("foo:1|s", datagram)
|
57
57
|
|
58
|
-
datagram = @datagram_builder.s(
|
58
|
+
datagram = @datagram_builder.s("fo@o", 10, 0.01, nil)
|
59
59
|
assert_equal("fo_o:10|s|@0.01", datagram)
|
60
60
|
end
|
61
61
|
|
62
62
|
def test_h
|
63
|
-
datagram = @datagram_builder.h(
|
63
|
+
datagram = @datagram_builder.h("foo", 1, nil, nil)
|
64
64
|
assert_equal("foo:1|h", datagram)
|
65
65
|
|
66
|
-
datagram = @datagram_builder.h(
|
66
|
+
datagram = @datagram_builder.h("fo@o", 10, 0.01, nil)
|
67
67
|
assert_equal("fo_o:10|h|@0.01", datagram)
|
68
68
|
end
|
69
69
|
|
70
70
|
def test_d
|
71
|
-
datagram = @datagram_builder.d(
|
71
|
+
datagram = @datagram_builder.d("foo", 1, nil, nil)
|
72
72
|
assert_equal("foo:1|d", datagram)
|
73
73
|
|
74
|
-
datagram = @datagram_builder.d(
|
74
|
+
datagram = @datagram_builder.d("fo@o", 10, 0.01, nil)
|
75
75
|
assert_equal("fo_o:10|d|@0.01", datagram)
|
76
76
|
end
|
77
77
|
|
78
78
|
def test_tags
|
79
|
-
datagram = @datagram_builder.d(
|
79
|
+
datagram = @datagram_builder.d("foo", 10, nil, ["foo", "bar"])
|
80
80
|
assert_equal("foo:10|d|#foo,bar", datagram)
|
81
81
|
|
82
|
-
datagram = @datagram_builder.d(
|
82
|
+
datagram = @datagram_builder.d("foo", 10, 0.1, ["foo:bar"])
|
83
83
|
assert_equal("foo:10|d|@0.1|#foo:bar", datagram)
|
84
84
|
|
85
|
-
datagram = @datagram_builder.d(
|
85
|
+
datagram = @datagram_builder.d("foo", 10, 1, foo: "bar", baz: "quc")
|
86
86
|
assert_equal("foo:10|d|#foo:bar,baz:quc", datagram)
|
87
87
|
end
|
88
88
|
|
89
89
|
def test_prefix
|
90
|
-
datagram_builder = StatsD::Instrument::DatagramBuilder.new(prefix:
|
91
|
-
datagram = datagram_builder.c(
|
92
|
-
assert_equal(
|
90
|
+
datagram_builder = StatsD::Instrument::DatagramBuilder.new(prefix: "foo")
|
91
|
+
datagram = datagram_builder.c("bar", 1, nil, nil)
|
92
|
+
assert_equal("foo.bar:1|c", datagram)
|
93
93
|
|
94
94
|
# The prefix should also be normalized
|
95
|
-
datagram_builder = StatsD::Instrument::DatagramBuilder.new(prefix:
|
96
|
-
datagram = datagram_builder.c(
|
97
|
-
assert_equal(
|
95
|
+
datagram_builder = StatsD::Instrument::DatagramBuilder.new(prefix: "foo|bar")
|
96
|
+
datagram = datagram_builder.c("baz", 1, nil, nil)
|
97
|
+
assert_equal("foo_bar.baz:1|c", datagram)
|
98
98
|
end
|
99
99
|
|
100
100
|
def test_default_tags
|
101
|
-
datagram_builder = StatsD::Instrument::DatagramBuilder.new(default_tags: [
|
102
|
-
datagram = datagram_builder.c(
|
103
|
-
assert_equal(
|
101
|
+
datagram_builder = StatsD::Instrument::DatagramBuilder.new(default_tags: ["foo"])
|
102
|
+
datagram = datagram_builder.c("bar", 1, nil, nil)
|
103
|
+
assert_equal("bar:1|c|#foo", datagram)
|
104
104
|
|
105
|
-
datagram = datagram_builder.c(
|
106
|
-
assert_equal(
|
105
|
+
datagram = datagram_builder.c("bar", 1, nil, a: "b")
|
106
|
+
assert_equal("bar:1|c|#a:b,foo", datagram)
|
107
107
|
|
108
108
|
# We do not filter out duplicates, because detecting dupes is too time consuming.
|
109
109
|
# We let the server deal with the situation
|
110
|
-
datagram = datagram_builder.c(
|
111
|
-
assert_equal(
|
110
|
+
datagram = datagram_builder.c("bar", 1, nil, ["foo"])
|
111
|
+
assert_equal("bar:1|c|#foo,foo", datagram)
|
112
112
|
|
113
113
|
# Default tags are also normalized
|
114
|
-
datagram_builder = StatsD::Instrument::DatagramBuilder.new(default_tags: [
|
115
|
-
datagram = datagram_builder.c(
|
116
|
-
assert_equal(
|
114
|
+
datagram_builder = StatsD::Instrument::DatagramBuilder.new(default_tags: ["f,o|o"])
|
115
|
+
datagram = datagram_builder.c("bar", 1, nil, nil)
|
116
|
+
assert_equal("bar:1|c|#foo", datagram)
|
117
117
|
end
|
118
118
|
end
|