statsd-instrument 3.0.0.pre1 → 3.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/lint.yml +22 -0
- data/.github/workflows/tests.yml +31 -0
- data/.rubocop.yml +3 -13
- data/CHANGELOG.md +50 -0
- data/Gemfile +8 -2
- data/README.md +6 -3
- data/Rakefile +7 -7
- data/benchmark/send-metrics-to-dev-null-log +12 -11
- data/benchmark/send-metrics-to-local-udp-receiver +16 -15
- data/bin/rake +29 -0
- data/bin/rubocop +29 -0
- data/lib/statsd-instrument.rb +1 -1
- data/lib/statsd/instrument.rb +112 -145
- data/lib/statsd/instrument/assertions.rb +200 -208
- data/lib/statsd/instrument/batched_udp_sink.rb +154 -0
- data/lib/statsd/instrument/capture_sink.rb +23 -19
- data/lib/statsd/instrument/client.rb +410 -306
- 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 +88 -77
- data/lib/statsd/instrument/expectation.rb +96 -96
- data/lib/statsd/instrument/helpers.rb +11 -7
- data/lib/statsd/instrument/log_sink.rb +20 -16
- data/lib/statsd/instrument/matchers.rb +93 -74
- data/lib/statsd/instrument/null_sink.rb +12 -8
- data/lib/statsd/instrument/railtie.rb +11 -7
- data/lib/statsd/instrument/rubocop.rb +8 -8
- 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 +2 -2
- 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/statsd_datagram_builder.rb +12 -8
- data/lib/statsd/instrument/strict.rb +1 -6
- data/lib/statsd/instrument/udp_sink.rb +49 -47
- data/lib/statsd/instrument/version.rb +1 -1
- data/statsd-instrument.gemspec +4 -8
- data/test/assertions_test.rb +205 -161
- data/test/benchmark/clock_gettime.rb +1 -1
- data/test/benchmark/default_tags.rb +9 -9
- data/test/benchmark/metrics.rb +8 -8
- data/test/benchmark/tags.rb +4 -4
- data/test/capture_sink_test.rb +14 -14
- data/test/client_test.rb +96 -96
- data/test/datagram_builder_test.rb +55 -55
- data/test/datagram_test.rb +5 -5
- data/test/dogstatsd_datagram_builder_test.rb +37 -37
- data/test/environment_test.rb +30 -21
- data/test/helpers/rubocop_helper.rb +12 -9
- data/test/helpers_test.rb +15 -15
- data/test/integration_test.rb +7 -7
- data/test/log_sink_test.rb +4 -4
- data/test/matchers_test.rb +54 -54
- data/test/null_sink_test.rb +4 -4
- 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 +6 -6
- data/test/rubocop/metric_value_keyword_argument_test.rb +3 -3
- data/test/rubocop/positional_arguments_test.rb +12 -12
- 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 +122 -122
- data/test/statsd_test.rb +69 -67
- data/test/test_helper.rb +19 -10
- data/test/udp_sink_test.rb +122 -50
- metadata +12 -92
- data/.github/workflows/ci.yml +0 -56
- data/.rubocop-https---shopify-github-io-ruby-style-guide-rubocop-yml +0 -1027
@@ -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
|
18
|
+
assert_equal(["ign#ored"], @datagram_builder.send(:normalize_tags, ["ign#o|re,d"]))
|
19
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(
|
32
|
-
assert_equal
|
31
|
+
datagram = @datagram_builder.c("foo", 1, nil, nil)
|
32
|
+
assert_equal("foo:1|c", datagram)
|
33
33
|
|
34
|
-
datagram = @datagram_builder.c(
|
35
|
-
assert_equal
|
34
|
+
datagram = @datagram_builder.c("fo:o", 10, 0.1, nil)
|
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(
|
40
|
-
assert_equal
|
39
|
+
datagram = @datagram_builder.ms("foo", 1, nil, nil)
|
40
|
+
assert_equal("foo:1|ms", datagram)
|
41
41
|
|
42
|
-
datagram = @datagram_builder.ms(
|
43
|
-
assert_equal
|
42
|
+
datagram = @datagram_builder.ms("fo:o", 10, 0.1, nil)
|
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(
|
48
|
-
assert_equal
|
47
|
+
datagram = @datagram_builder.g("foo", 1, nil, nil)
|
48
|
+
assert_equal("foo:1|g", datagram)
|
49
49
|
|
50
|
-
datagram = @datagram_builder.g(
|
51
|
-
assert_equal
|
50
|
+
datagram = @datagram_builder.g("fo|o", 10, 0.01, nil)
|
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(
|
56
|
-
assert_equal
|
55
|
+
datagram = @datagram_builder.s("foo", 1, nil, nil)
|
56
|
+
assert_equal("foo:1|s", datagram)
|
57
57
|
|
58
|
-
datagram = @datagram_builder.s(
|
59
|
-
assert_equal
|
58
|
+
datagram = @datagram_builder.s("fo@o", 10, 0.01, nil)
|
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(
|
64
|
-
assert_equal
|
63
|
+
datagram = @datagram_builder.h("foo", 1, nil, nil)
|
64
|
+
assert_equal("foo:1|h", datagram)
|
65
65
|
|
66
|
-
datagram = @datagram_builder.h(
|
67
|
-
assert_equal
|
66
|
+
datagram = @datagram_builder.h("fo@o", 10, 0.01, nil)
|
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(
|
72
|
-
assert_equal
|
71
|
+
datagram = @datagram_builder.d("foo", 1, nil, nil)
|
72
|
+
assert_equal("foo:1|d", datagram)
|
73
73
|
|
74
|
-
datagram = @datagram_builder.d(
|
75
|
-
assert_equal
|
74
|
+
datagram = @datagram_builder.d("fo@o", 10, 0.01, nil)
|
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(
|
80
|
-
assert_equal
|
79
|
+
datagram = @datagram_builder.d("foo", 10, nil, ["foo", "bar"])
|
80
|
+
assert_equal("foo:10|d|#foo,bar", datagram)
|
81
81
|
|
82
|
-
datagram = @datagram_builder.d(
|
83
|
-
assert_equal
|
82
|
+
datagram = @datagram_builder.d("foo", 10, 0.1, ["foo:bar"])
|
83
|
+
assert_equal("foo:10|d|@0.1|#foo:bar", datagram)
|
84
84
|
|
85
|
-
datagram = @datagram_builder.d(
|
86
|
-
assert_equal
|
85
|
+
datagram = @datagram_builder.d("foo", 10, 1, foo: "bar", baz: "quc")
|
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
|
data/test/datagram_test.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "test_helper"
|
4
4
|
|
5
5
|
class DatagramTest < Minitest::Test
|
6
6
|
def test_parsing_datagrams
|
7
|
-
datagram =
|
8
|
-
|
9
|
-
|
7
|
+
datagram = "Kernel.Orders.order_creation_path:1|c|" \
|
8
|
+
"#order_source:web,code_source:NilController#NilAction,order_builder:false," \
|
9
|
+
"multi_currency:false,fulfillment_orders_beta_enabled:false"
|
10
10
|
|
11
11
|
parsed = StatsD::Instrument::Datagram.new(datagram)
|
12
|
-
assert_includes
|
12
|
+
assert_includes(parsed.tags, "code_source:NilController#NilAction")
|
13
13
|
end
|
14
14
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "test_helper"
|
4
4
|
|
5
5
|
class DogStatsDDatagramBuilderTest < Minitest::Test
|
6
6
|
def setup
|
@@ -8,60 +8,60 @@ class DogStatsDDatagramBuilderTest < Minitest::Test
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def test_raises_on_unsupported_metrics
|
11
|
-
assert_raises(NotImplementedError) { @datagram_builder.kv(
|
11
|
+
assert_raises(NotImplementedError) { @datagram_builder.kv("foo", 10, nil, nil) }
|
12
12
|
end
|
13
13
|
|
14
14
|
def test_simple_service_check
|
15
|
-
datagram = @datagram_builder._sc(
|
16
|
-
assert_equal
|
15
|
+
datagram = @datagram_builder._sc("service", :ok)
|
16
|
+
assert_equal("_sc|service|0", datagram)
|
17
17
|
parsed_datagram = StatsD::Instrument::DogStatsDDatagramBuilder.datagram_class.new(datagram)
|
18
|
-
assert_equal
|
19
|
-
assert_equal
|
20
|
-
assert_equal
|
18
|
+
assert_equal(:_sc, parsed_datagram.type)
|
19
|
+
assert_equal("service", parsed_datagram.name)
|
20
|
+
assert_equal(0, parsed_datagram.value)
|
21
21
|
end
|
22
22
|
|
23
23
|
def test_complex_service_check
|
24
|
-
datagram = @datagram_builder._sc(
|
25
|
-
hostname:
|
26
|
-
assert_equal
|
24
|
+
datagram = @datagram_builder._sc("service", :warning, timestamp: Time.parse("2019-09-30T04:22:12Z"),
|
25
|
+
hostname: "localhost", tags: { foo: "bar|baz" }, message: "blah")
|
26
|
+
assert_equal("_sc|service|1|h:localhost|d:1569817332|#foo:barbaz|m:blah", datagram)
|
27
27
|
|
28
28
|
parsed_datagram = StatsD::Instrument::DogStatsDDatagramBuilder.datagram_class.new(datagram)
|
29
|
-
assert_equal
|
30
|
-
assert_equal
|
31
|
-
assert_equal
|
32
|
-
assert_equal
|
33
|
-
assert_equal
|
34
|
-
assert_equal
|
35
|
-
assert_equal
|
29
|
+
assert_equal(:_sc, parsed_datagram.type)
|
30
|
+
assert_equal("service", parsed_datagram.name)
|
31
|
+
assert_equal(1, parsed_datagram.value)
|
32
|
+
assert_equal("localhost", parsed_datagram.hostname)
|
33
|
+
assert_equal(Time.parse("2019-09-30T04:22:12Z"), parsed_datagram.timestamp)
|
34
|
+
assert_equal(["foo:barbaz"], parsed_datagram.tags)
|
35
|
+
assert_equal("blah", parsed_datagram.message)
|
36
36
|
end
|
37
37
|
|
38
38
|
def test_simple_event
|
39
|
-
datagram = @datagram_builder._e(
|
40
|
-
assert_equal
|
39
|
+
datagram = @datagram_builder._e("hello", "world")
|
40
|
+
assert_equal("_e{5,5}:hello|world", datagram)
|
41
41
|
|
42
42
|
parsed_datagram = StatsD::Instrument::DogStatsDDatagramBuilder.datagram_class.new(datagram)
|
43
|
-
assert_equal
|
44
|
-
assert_equal
|
45
|
-
assert_equal
|
43
|
+
assert_equal(:_e, parsed_datagram.type)
|
44
|
+
assert_equal("hello", parsed_datagram.name)
|
45
|
+
assert_equal("world", parsed_datagram.value)
|
46
46
|
end
|
47
47
|
|
48
48
|
def test_complex_event
|
49
|
-
datagram = @datagram_builder._e("testing", "with\nnewline", timestamp: Time.parse(
|
50
|
-
hostname:
|
51
|
-
alert_type:
|
52
|
-
assert_equal
|
53
|
-
|
49
|
+
datagram = @datagram_builder._e("testing", "with\nnewline", timestamp: Time.parse("2019-09-30T04:22:12Z"),
|
50
|
+
hostname: "localhost", aggregation_key: "my-key", priority: "low", source_type_name: "source",
|
51
|
+
alert_type: "success", tags: { foo: "bar|baz" })
|
52
|
+
assert_equal('_e{7,13}:testing|with\\nnewline|h:localhost|d:1569817332|k:my-key|' \
|
53
|
+
"p:low|s:source|t:success|#foo:barbaz", datagram)
|
54
54
|
|
55
55
|
parsed_datagram = StatsD::Instrument::DogStatsDDatagramBuilder.datagram_class.new(datagram)
|
56
|
-
assert_equal
|
57
|
-
assert_equal
|
58
|
-
assert_equal
|
59
|
-
assert_equal
|
60
|
-
assert_equal
|
61
|
-
assert_equal
|
62
|
-
assert_equal
|
63
|
-
assert_equal
|
64
|
-
assert_equal
|
65
|
-
assert_equal
|
56
|
+
assert_equal(:_e, parsed_datagram.type)
|
57
|
+
assert_equal("testing", parsed_datagram.name)
|
58
|
+
assert_equal("with\nnewline", parsed_datagram.value)
|
59
|
+
assert_equal("localhost", parsed_datagram.hostname)
|
60
|
+
assert_equal(Time.parse("2019-09-30T04:22:12Z"), parsed_datagram.timestamp)
|
61
|
+
assert_equal(["foo:barbaz"], parsed_datagram.tags)
|
62
|
+
assert_equal("my-key", parsed_datagram.aggregation_key)
|
63
|
+
assert_equal("low", parsed_datagram.priority)
|
64
|
+
assert_equal("source", parsed_datagram.source_type_name)
|
65
|
+
assert_equal("success", parsed_datagram.alert_type)
|
66
66
|
end
|
67
67
|
end
|
data/test/environment_test.rb
CHANGED
@@ -1,58 +1,67 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "test_helper"
|
4
4
|
|
5
5
|
module Rails; end
|
6
6
|
|
7
7
|
class EnvironmentTest < Minitest::Test
|
8
8
|
def test_environment_prefers_statsd_env_if_available
|
9
9
|
env = StatsD::Instrument::Environment.new(
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
"STATSD_ENV" => "set_from_STATSD_ENV",
|
11
|
+
"RACK_ENV" => "set_from_RACK_ENV",
|
12
|
+
"ENV" => "set_from_ENV",
|
13
13
|
)
|
14
|
-
assert_equal
|
14
|
+
assert_equal("set_from_STATSD_ENV", env.environment)
|
15
15
|
end
|
16
16
|
|
17
17
|
def test_environment_uses_env_when_rails_does_not_respond_to_env_and_statsd_env_is_not_set
|
18
18
|
env = StatsD::Instrument::Environment.new(
|
19
|
-
|
19
|
+
"ENV" => "set_from_ENV",
|
20
20
|
)
|
21
|
-
assert_equal
|
21
|
+
assert_equal("set_from_ENV", env.environment)
|
22
22
|
end
|
23
23
|
|
24
24
|
def test_environment_uses_rails_env_when_rails_is_available
|
25
|
-
Rails.stubs(:env).returns(
|
26
|
-
assert_equal
|
25
|
+
Rails.stubs(:env).returns("production")
|
26
|
+
assert_equal("production", StatsD::Instrument::Environment.environment)
|
27
27
|
end
|
28
28
|
|
29
29
|
def test_environment_defaults_to_development
|
30
30
|
env = StatsD::Instrument::Environment.new({})
|
31
|
-
assert_equal
|
31
|
+
assert_equal("development", env.environment)
|
32
32
|
end
|
33
33
|
|
34
34
|
def test_client_returns_client_instance
|
35
35
|
env = StatsD::Instrument::Environment.new({})
|
36
|
-
assert_kind_of
|
36
|
+
assert_kind_of(StatsD::Instrument::Client, env.client)
|
37
37
|
end
|
38
38
|
|
39
39
|
def test_client_from_env_uses_log_sink_in_development_environment
|
40
|
-
env = StatsD::Instrument::Environment.new(
|
41
|
-
assert_kind_of
|
40
|
+
env = StatsD::Instrument::Environment.new("STATSD_USE_NEW_CLIENT" => "1", "STATSD_ENV" => "development")
|
41
|
+
assert_kind_of(StatsD::Instrument::LogSink, env.client.sink)
|
42
42
|
end
|
43
43
|
|
44
44
|
def test_client_from_env_uses_null_sink_in_test_environment
|
45
|
-
env = StatsD::Instrument::Environment.new(
|
46
|
-
assert_kind_of
|
45
|
+
env = StatsD::Instrument::Environment.new("STATSD_USE_NEW_CLIENT" => "1", "STATSD_ENV" => "test")
|
46
|
+
assert_kind_of(StatsD::Instrument::NullSink, env.client.sink)
|
47
47
|
end
|
48
48
|
|
49
|
-
def
|
50
|
-
env = StatsD::Instrument::Environment.new(
|
51
|
-
assert_kind_of
|
49
|
+
def test_client_from_env_uses_batched_udp_sink_in_staging_environment
|
50
|
+
env = StatsD::Instrument::Environment.new("STATSD_USE_NEW_CLIENT" => "1", "STATSD_ENV" => "staging")
|
51
|
+
assert_kind_of(StatsD::Instrument::BatchedUDPSink, env.client.sink)
|
52
52
|
end
|
53
53
|
|
54
|
-
def
|
55
|
-
env = StatsD::Instrument::Environment.new(
|
56
|
-
assert_kind_of
|
54
|
+
def test_client_from_env_uses_batched_udp_sink_in_production_environment
|
55
|
+
env = StatsD::Instrument::Environment.new("STATSD_USE_NEW_CLIENT" => "1", "STATSD_ENV" => "production")
|
56
|
+
assert_kind_of(StatsD::Instrument::BatchedUDPSink, env.client.sink)
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_client_from_env_uses_regular_udp_sink_when_flush_interval_is_0
|
60
|
+
env = StatsD::Instrument::Environment.new(
|
61
|
+
"STATSD_USE_NEW_CLIENT" => "1",
|
62
|
+
"STATSD_ENV" => "staging",
|
63
|
+
"STATSD_FLUSH_INTERVAL" => "0.0",
|
64
|
+
)
|
65
|
+
assert_kind_of(StatsD::Instrument::UDPSink, env.client.sink)
|
57
66
|
end
|
58
67
|
end
|
@@ -1,25 +1,29 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "rubocop"
|
4
|
+
require "rubocop/cop/legacy/corrector"
|
4
5
|
|
5
6
|
module RubocopHelper
|
6
7
|
attr_accessor :cop
|
7
8
|
|
8
9
|
private
|
9
10
|
|
11
|
+
RUBY_VERSION = 2.5
|
12
|
+
private_constant :RUBY_VERSION
|
13
|
+
|
10
14
|
def assert_no_offenses(source)
|
11
|
-
investigate(RuboCop::ProcessedSource.new(source,
|
12
|
-
assert_predicate
|
15
|
+
investigate(RuboCop::ProcessedSource.new(source, RUBY_VERSION, nil))
|
16
|
+
assert_predicate(cop.offenses, :empty?, "Did not expect Rubocop to find offenses")
|
13
17
|
end
|
14
18
|
|
15
19
|
def assert_offense(source)
|
16
|
-
investigate(RuboCop::ProcessedSource.new(source,
|
17
|
-
refute_predicate
|
20
|
+
investigate(RuboCop::ProcessedSource.new(source, RUBY_VERSION, nil))
|
21
|
+
refute_predicate(cop.offenses, :empty?, "Expected Rubocop to find offenses")
|
18
22
|
end
|
19
23
|
|
20
24
|
def assert_no_autocorrect(source)
|
21
25
|
corrected = autocorrect_source(source)
|
22
|
-
assert_equal
|
26
|
+
assert_equal(source, corrected)
|
23
27
|
end
|
24
28
|
|
25
29
|
def autocorrect_source(source)
|
@@ -27,16 +31,15 @@ module RubocopHelper
|
|
27
31
|
RuboCop::Formatter::DisabledConfigFormatter.detected_styles = {}
|
28
32
|
cop.instance_variable_get(:@options)[:auto_correct] = true
|
29
33
|
|
30
|
-
processed_source = RuboCop::ProcessedSource.new(source,
|
34
|
+
processed_source = RuboCop::ProcessedSource.new(source, RUBY_VERSION, nil)
|
31
35
|
investigate(processed_source)
|
32
36
|
|
33
|
-
corrector = RuboCop::Cop::Corrector.new(processed_source.buffer, cop.corrections)
|
37
|
+
corrector = RuboCop::Cop::Legacy::Corrector.new(processed_source.buffer, cop.corrections)
|
34
38
|
corrector.rewrite
|
35
39
|
end
|
36
40
|
|
37
41
|
def investigate(processed_source)
|
38
42
|
forces = RuboCop::Cop::Force.all.each_with_object([]) do |klass, instances|
|
39
|
-
next unless cop.join_force?(klass)
|
40
43
|
instances << klass.new([cop])
|
41
44
|
end
|
42
45
|
|
data/test/helpers_test.rb
CHANGED
@@ -1,40 +1,40 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "test_helper"
|
4
4
|
|
5
5
|
class HelpersTest < Minitest::Test
|
6
6
|
def setup
|
7
7
|
test_class = Class.new(Minitest::Test)
|
8
8
|
test_class.send(:include, StatsD::Instrument::Helpers)
|
9
|
-
@test_case = test_class.new(
|
9
|
+
@test_case = test_class.new("fake")
|
10
10
|
end
|
11
11
|
|
12
12
|
def test_capture_metrics_inside_block_only
|
13
|
-
StatsD.increment(
|
13
|
+
StatsD.increment("counter")
|
14
14
|
metrics = @test_case.capture_statsd_calls do
|
15
|
-
StatsD.increment(
|
16
|
-
StatsD.gauge(
|
15
|
+
StatsD.increment("counter")
|
16
|
+
StatsD.gauge("gauge", 12)
|
17
17
|
end
|
18
|
-
StatsD.gauge(
|
18
|
+
StatsD.gauge("gauge", 15)
|
19
19
|
|
20
|
-
assert_equal
|
21
|
-
assert_equal
|
22
|
-
assert_equal
|
23
|
-
assert_equal
|
20
|
+
assert_equal(2, metrics.length)
|
21
|
+
assert_equal("counter", metrics[0].name)
|
22
|
+
assert_equal("gauge", metrics[1].name)
|
23
|
+
assert_equal(12, metrics[1].value)
|
24
24
|
end
|
25
25
|
|
26
26
|
def test_capture_metrics_with_new_client
|
27
27
|
@old_client = StatsD.singleton_client
|
28
28
|
StatsD.singleton_client = StatsD::Instrument::Client.new
|
29
29
|
|
30
|
-
StatsD.increment(
|
30
|
+
StatsD.increment("counter")
|
31
31
|
metrics = @test_case.capture_statsd_datagrams do
|
32
|
-
StatsD.increment(
|
33
|
-
StatsD.gauge(
|
32
|
+
StatsD.increment("counter")
|
33
|
+
StatsD.gauge("gauge", 12)
|
34
34
|
end
|
35
|
-
StatsD.gauge(
|
35
|
+
StatsD.gauge("gauge", 15)
|
36
36
|
|
37
|
-
assert_equal
|
37
|
+
assert_equal(2, metrics.length)
|
38
38
|
|
39
39
|
ensure
|
40
40
|
StatsD.singleton_client = @old_client
|