inst_statsd 2.4.0 → 3.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/inst_statsd/block_stat.rb +14 -14
- data/lib/inst_statsd/block_tracking.rb +12 -13
- data/lib/inst_statsd/counter.rb +2 -4
- data/lib/inst_statsd/default_tracking.rb +30 -27
- data/lib/inst_statsd/event.rb +1 -1
- data/lib/inst_statsd/null_logger.rb +3 -4
- data/lib/inst_statsd/request_logger.rb +4 -6
- data/lib/inst_statsd/request_stat.rb +20 -20
- data/lib/inst_statsd/request_tracking.rb +19 -18
- data/lib/inst_statsd/sql_tracker.rb +9 -11
- data/lib/inst_statsd/statsd.rb +61 -52
- data/lib/inst_statsd/version.rb +2 -2
- data/lib/inst_statsd.rb +36 -29
- data/spec/inst_statsd/block_stat_spec.rb +2 -2
- data/spec/inst_statsd/block_tracking_spec.rb +66 -46
- data/spec/inst_statsd/counter_spec.rb +21 -23
- data/spec/inst_statsd/event_spec.rb +25 -24
- data/spec/inst_statsd/inst_statsd_spec.rb +85 -81
- data/spec/inst_statsd/null_logger_spec.rb +11 -13
- data/spec/inst_statsd/request_logger_spec.rb +43 -50
- data/spec/inst_statsd/request_stat_spec.rb +90 -93
- data/spec/inst_statsd/request_tracking_spec.rb +6 -7
- data/spec/inst_statsd/sql_tracker_spec.rb +29 -31
- data/spec/inst_statsd/statsd_spec.rb +123 -88
- data/spec/spec_helper.rb +2 -2
- metadata +58 -30
@@ -1,20 +1,20 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "spec_helper"
|
4
4
|
|
5
5
|
describe InstStatsd do
|
6
|
-
before
|
6
|
+
before do
|
7
7
|
InstStatsd.settings = nil
|
8
8
|
end
|
9
9
|
|
10
|
-
after
|
11
|
-
[
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
].each {|k| ENV.delete k}
|
10
|
+
after do
|
11
|
+
%w[
|
12
|
+
INST_STATSD_HOST
|
13
|
+
INST_STATSD_NAMESPACE
|
14
|
+
INST_STATSD_PORT
|
15
|
+
INST_STATSD_APPEND_HOST_NAME
|
16
|
+
INST_DOG_TAGS
|
17
|
+
].each { |k| ENV.delete k }
|
18
18
|
end
|
19
19
|
|
20
20
|
describe ".settings" do
|
@@ -23,185 +23,189 @@ describe InstStatsd do
|
|
23
23
|
end
|
24
24
|
|
25
25
|
it "can be assigned a new value" do
|
26
|
-
settings = { host:
|
26
|
+
settings = { host: "bar", port: 1234 }
|
27
27
|
InstStatsd.settings = settings
|
28
28
|
|
29
29
|
expect(InstStatsd.settings).to eq settings
|
30
30
|
end
|
31
31
|
|
32
|
-
it
|
33
|
-
ENV[
|
34
|
-
ENV[
|
32
|
+
it "pulls from ENV if not already set" do
|
33
|
+
ENV["INST_STATSD_HOST"] = "statsd.example.org"
|
34
|
+
ENV["INST_STATSD_NAMESPACE"] = "canvas"
|
35
35
|
|
36
36
|
expected = {
|
37
|
-
host:
|
38
|
-
namespace:
|
37
|
+
host: "statsd.example.org",
|
38
|
+
namespace: "canvas"
|
39
39
|
}
|
40
40
|
expect(InstStatsd.settings).to eq(expected)
|
41
41
|
end
|
42
42
|
|
43
|
-
it
|
44
|
-
ENV[
|
45
|
-
ENV[
|
43
|
+
it "configured settings are merged into and take precedence over any existing ENV settings" do
|
44
|
+
ENV["INST_STATSD_HOST"] = "statsd.example.org"
|
45
|
+
ENV["INST_STATSD_NAMESPACE"] = "canvas"
|
46
46
|
|
47
|
-
settings = { host:
|
47
|
+
settings = { host: "statsd.example-override.org" }
|
48
48
|
InstStatsd.settings = settings
|
49
49
|
|
50
50
|
expect(InstStatsd.settings).to eq(InstStatsd.env_settings.merge(settings))
|
51
51
|
expect(InstStatsd.settings[:host]).to eq(settings[:host])
|
52
52
|
end
|
53
53
|
|
54
|
-
it
|
55
|
-
settings = { foo:
|
54
|
+
it "validates settings" do
|
55
|
+
settings = { foo: "blah" }
|
56
56
|
expect { InstStatsd.settings = settings }.to raise_error(InstStatsd::ConfigurationError)
|
57
57
|
end
|
58
58
|
|
59
|
-
it
|
60
|
-
settings = {
|
59
|
+
it "converts string keys to symbols" do
|
60
|
+
settings = { "host" => "bar", "port" => 1234 }
|
61
61
|
InstStatsd.settings = settings
|
62
|
-
expect(InstStatsd.settings).to eq({ host:
|
62
|
+
expect(InstStatsd.settings).to eq({ host: "bar", port: 1234 })
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
66
66
|
describe ".convert_bool" do
|
67
|
-
it
|
68
|
-
config = {potential_string_bool:
|
67
|
+
it "sets string true values to boolean true" do
|
68
|
+
config = { potential_string_bool: "true" }
|
69
69
|
InstStatsd.convert_bool(config, :potential_string_bool)
|
70
70
|
expect(config[:potential_string_bool]).to be(true)
|
71
71
|
end
|
72
|
-
|
73
|
-
|
72
|
+
|
73
|
+
it "sets string True values to boolean true" do
|
74
|
+
config = { potential_string_bool: "True" }
|
74
75
|
InstStatsd.convert_bool(config, :potential_string_bool)
|
75
76
|
expect(config[:potential_string_bool]).to be(true)
|
76
77
|
end
|
77
|
-
|
78
|
-
|
78
|
+
|
79
|
+
it "sets boolean true values to boolean true" do
|
80
|
+
config = { potential_string_bool: true }
|
79
81
|
InstStatsd.convert_bool(config, :potential_string_bool)
|
80
82
|
expect(config[:potential_string_bool]).to be(true)
|
81
83
|
end
|
82
|
-
|
83
|
-
|
84
|
+
|
85
|
+
it "sets false strings to boolean false" do
|
86
|
+
config = { potential_string_bool: "false" }
|
84
87
|
InstStatsd.convert_bool(config, :potential_string_bool)
|
85
88
|
expect(config[:potential_string_bool]).to be(false)
|
86
89
|
end
|
87
|
-
|
88
|
-
|
90
|
+
|
91
|
+
it "sets False strings to boolean false" do
|
92
|
+
config = { potential_string_bool: "False" }
|
89
93
|
InstStatsd.convert_bool(config, :potential_string_bool)
|
90
94
|
expect(config[:potential_string_bool]).to be(false)
|
91
95
|
end
|
92
|
-
|
93
|
-
|
96
|
+
|
97
|
+
it "sets false booleans to boolean false" do
|
98
|
+
config = { potential_string_bool: false }
|
94
99
|
InstStatsd.convert_bool(config, :potential_string_bool)
|
95
100
|
expect(config[:potential_string_bool]).to be(false)
|
96
101
|
end
|
97
|
-
|
98
|
-
|
102
|
+
|
103
|
+
it "makes no change for nil values" do
|
104
|
+
config = { foo: "bar" }
|
99
105
|
InstStatsd.convert_bool(config, :potential_string_bool)
|
100
|
-
expect(config).to eq({foo:
|
101
|
-
end
|
102
|
-
it 'raises error for non true or false strings or booleans' do
|
103
|
-
config = {potential_string_bool: 'trrruuue'}
|
104
|
-
expect{InstStatsd.convert_bool(config, :potential_string_bool)}.to raise_error(InstStatsd::ConfigurationError)
|
106
|
+
expect(config).to eq({ foo: "bar" })
|
105
107
|
end
|
106
108
|
|
109
|
+
it "raises error for non true or false strings or booleans" do
|
110
|
+
config = { potential_string_bool: "trrruuue" }
|
111
|
+
expect { InstStatsd.convert_bool(config, :potential_string_bool) }.to raise_error(InstStatsd::ConfigurationError)
|
112
|
+
end
|
107
113
|
end
|
108
114
|
|
109
115
|
describe ".env_settings" do
|
110
|
-
it
|
116
|
+
it "returns empty hash when no INST_STATSD_HOST found" do
|
111
117
|
env = {
|
112
|
-
|
118
|
+
"INST_STATSD_NAMESPACE" => "canvas"
|
113
119
|
}
|
114
120
|
expect(InstStatsd.env_settings(env)).to eq({})
|
115
121
|
end
|
116
122
|
|
117
|
-
it
|
123
|
+
it "returns empty hash when missing dog tags" do
|
118
124
|
env = {
|
119
|
-
|
125
|
+
"INST_DOG_API_KEY" => "SEKRET KEY"
|
120
126
|
}
|
121
127
|
expect(InstStatsd.env_settings(env)).to eq({})
|
122
128
|
end
|
123
129
|
|
124
|
-
it
|
130
|
+
it "builds settings hash with dog environment vars" do
|
125
131
|
env = {
|
126
|
-
|
132
|
+
"INST_DOG_TAGS" => '{"app": "canvas", "env": "prod"}'
|
127
133
|
}
|
128
134
|
expected = {
|
129
|
-
dog_tags: {"app" => "canvas", "env" => "prod"}
|
135
|
+
dog_tags: { "app" => "canvas", "env" => "prod" }
|
130
136
|
}
|
131
137
|
expect(InstStatsd.env_settings(env)).to eq(expected)
|
132
138
|
end
|
133
139
|
|
134
|
-
it
|
140
|
+
it "builds settings hash from environment vars" do
|
135
141
|
env = {
|
136
|
-
|
137
|
-
|
142
|
+
"INST_STATSD_HOST" => "statsd.example.org",
|
143
|
+
"INST_STATSD_NAMESPACE" => "canvas"
|
138
144
|
}
|
139
145
|
expected = {
|
140
|
-
host:
|
141
|
-
namespace:
|
146
|
+
host: "statsd.example.org",
|
147
|
+
namespace: "canvas"
|
142
148
|
}
|
143
149
|
expect(InstStatsd.env_settings(env)).to eq(expected)
|
144
150
|
end
|
145
151
|
|
146
|
-
it
|
147
|
-
ENV[
|
148
|
-
ENV[
|
152
|
+
it "uses ENV if env argument hash not passed" do
|
153
|
+
ENV["INST_STATSD_HOST"] = "statsd.example.org"
|
154
|
+
ENV["INST_STATSD_NAMESPACE"] = "canvas"
|
149
155
|
|
150
156
|
expected = {
|
151
|
-
host:
|
152
|
-
namespace:
|
157
|
+
host: "statsd.example.org",
|
158
|
+
namespace: "canvas"
|
153
159
|
}
|
154
160
|
expect(InstStatsd.env_settings).to eq(expected)
|
155
161
|
end
|
156
162
|
|
157
163
|
it 'converts env append_hostname "false" to boolean' do
|
158
164
|
env = {
|
159
|
-
|
160
|
-
|
165
|
+
"INST_STATSD_HOST" => "statsd.example.org",
|
166
|
+
"INST_STATSD_APPEND_HOSTNAME" => "false"
|
161
167
|
}
|
162
168
|
expected = {
|
163
|
-
host:
|
164
|
-
append_hostname: false
|
169
|
+
host: "statsd.example.org",
|
170
|
+
append_hostname: false
|
165
171
|
}
|
166
172
|
expect(InstStatsd.env_settings(env)).to eq(expected)
|
167
173
|
end
|
168
174
|
|
169
175
|
it 'converts env append_hostname "true" to boolean' do
|
170
176
|
env = {
|
171
|
-
|
172
|
-
|
177
|
+
"INST_STATSD_HOST" => "statsd.example.org",
|
178
|
+
"INST_STATSD_APPEND_HOSTNAME" => "true"
|
173
179
|
}
|
174
180
|
expected = {
|
175
|
-
host:
|
176
|
-
append_hostname: true
|
181
|
+
host: "statsd.example.org",
|
182
|
+
append_hostname: true
|
177
183
|
}
|
178
184
|
expect(InstStatsd.env_settings(env)).to eq(expected)
|
179
185
|
end
|
180
186
|
|
181
|
-
it
|
187
|
+
it "keeps boolean false values for append_hostname" do
|
182
188
|
env = {
|
183
|
-
|
184
|
-
|
189
|
+
"INST_STATSD_HOST" => "statsd.example.org",
|
190
|
+
"INST_STATSD_APPEND_HOSTNAME" => false
|
185
191
|
}
|
186
192
|
expected = {
|
187
|
-
host:
|
188
|
-
append_hostname: false
|
193
|
+
host: "statsd.example.org",
|
194
|
+
append_hostname: false
|
189
195
|
}
|
190
196
|
expect(InstStatsd.env_settings(env)).to eq(expected)
|
191
197
|
end
|
192
198
|
|
193
|
-
it
|
199
|
+
it "keeps boolean true values for append_hostname" do
|
194
200
|
env = {
|
195
|
-
|
196
|
-
|
201
|
+
"INST_STATSD_HOST" => "statsd.example.org",
|
202
|
+
"INST_STATSD_APPEND_HOSTNAME" => true
|
197
203
|
}
|
198
204
|
expected = {
|
199
|
-
host:
|
200
|
-
append_hostname: true
|
205
|
+
host: "statsd.example.org",
|
206
|
+
append_hostname: true
|
201
207
|
}
|
202
208
|
expect(InstStatsd.env_settings(env)).to eq(expected)
|
203
209
|
end
|
204
|
-
|
205
210
|
end
|
206
|
-
|
207
211
|
end
|
@@ -1,25 +1,23 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "spec_helper"
|
4
4
|
|
5
5
|
describe InstStatsd::NullLogger do
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
expect{InstStatsd::NullLogger.new}.
|
10
|
-
expect{InstStatsd::NullLogger.new(1, 2, 3)}.to_not raise_error
|
6
|
+
describe "initialize" do
|
7
|
+
it "takes any arguments" do
|
8
|
+
expect { InstStatsd::NullLogger.new }.not_to raise_error
|
9
|
+
expect { InstStatsd::NullLogger.new(1, 2, 3) }.not_to raise_error
|
11
10
|
end
|
12
11
|
end
|
13
12
|
|
14
|
-
describe
|
15
|
-
it
|
16
|
-
log_path =
|
17
|
-
File.
|
13
|
+
describe "debug, info, warn, fatal, and unknown" do
|
14
|
+
it "should all no-op instead of logging" do
|
15
|
+
log_path = "spec/support/test.log"
|
16
|
+
File.write(log_path, "") # empty log file
|
18
17
|
logger = InstStatsd::NullLogger.new(log_path)
|
19
|
-
%w[debug info warn error fatal unknown].each { |m| logger.send(m,
|
18
|
+
%w[debug info warn error fatal unknown].each { |m| logger.send(m, "foo") }
|
20
19
|
log_contents = File.read(log_path)
|
21
|
-
expect(log_contents).to eq
|
20
|
+
expect(log_contents).to eq ""
|
22
21
|
end
|
23
22
|
end
|
24
|
-
|
25
23
|
end
|
@@ -1,60 +1,56 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require "spec_helper"
|
4
|
+
require "logger"
|
5
5
|
|
6
6
|
describe InstStatsd::RequestLogger do
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
@logger = InstStatsd::RequestLogger.new(Logger.new(STDOUT))
|
7
|
+
describe "#build_log_message" do
|
8
|
+
before do
|
9
|
+
@logger = InstStatsd::RequestLogger.new(Logger.new($stdout))
|
11
10
|
end
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
allow(request_stat).to
|
16
|
-
results = @logger.build_log_message(request_stat,
|
11
|
+
|
12
|
+
it "includes the supplied header" do
|
13
|
+
request_stat = instance_double(InstStatsd::RequestStat)
|
14
|
+
allow(request_stat).to receive_messages(exclusive_stats: nil, stats: {})
|
15
|
+
results = @logger.build_log_message(request_stat, "FOO_STATS")
|
17
16
|
expect(results).to eq("[FOO_STATS]")
|
18
17
|
end
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
allow(request_stat).to
|
18
|
+
|
19
|
+
it "falls back to the default header" do
|
20
|
+
request_stat = instance_double(InstStatsd::RequestStat)
|
21
|
+
allow(request_stat).to receive_messages(exclusive_stats: nil, stats: {})
|
23
22
|
results = @logger.build_log_message(request_stat)
|
24
23
|
expect(results).to eq("[STATSD]")
|
25
24
|
end
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
allow(request_stat).to
|
30
|
-
|
31
|
-
"active.record" => 24)
|
25
|
+
|
26
|
+
it "includes stats that are available" do
|
27
|
+
request_stat = instance_double(InstStatsd::RequestStat)
|
28
|
+
allow(request_stat).to receive_messages(exclusive_stats: nil, stats: { "total" => 100.21,
|
29
|
+
"active.record" => 24 })
|
32
30
|
results = @logger.build_log_message(request_stat)
|
33
31
|
expect(results).to eq("[STATSD] (total: 100.21) (active_record: 24.00)")
|
34
32
|
end
|
35
33
|
|
36
|
-
it
|
37
|
-
request_stat =
|
38
|
-
allow(request_stat).to
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
"total" => 54.32,
|
43
|
-
"active.record" => 1)
|
34
|
+
it "includes exclusive_stats if there are any" do
|
35
|
+
request_stat = instance_double(InstStatsd::RequestStat)
|
36
|
+
allow(request_stat).to receive_messages(stats: { "total" => 100.21,
|
37
|
+
"active.record" => 24 },
|
38
|
+
exclusive_stats: { "total" => 54.32,
|
39
|
+
"active.record" => 1 })
|
44
40
|
results = @logger.build_log_message(request_stat)
|
45
|
-
expect(results).to eq("[STATSD] (total: 100.21) (active_record: 24.00) (exclusive_total: 54.32) (exclusive_active_record: 1.00)")
|
41
|
+
expect(results).to eq("[STATSD] (total: 100.21) (active_record: 24.00) (exclusive_total: 54.32) (exclusive_active_record: 1.00)") # rubocop:disable Layout/LineLength
|
46
42
|
end
|
47
43
|
|
48
|
-
describe
|
49
|
-
it
|
50
|
-
request_stat =
|
51
|
-
allow(request_stat).to
|
52
|
-
allow(request_stat).to receive(:exclusive_stats).and_return(nil)
|
44
|
+
describe "decimal precision" do
|
45
|
+
it "forces 2 decimal precision" do
|
46
|
+
request_stat = instance_double(InstStatsd::RequestStat)
|
47
|
+
allow(request_stat).to receive_messages(stats: { total: 72.1 }, exclusive_stats: nil)
|
53
48
|
results = @logger.build_log_message(request_stat)
|
54
49
|
expect(results).to eq("[STATSD] (total: 72.10)")
|
55
50
|
end
|
56
|
-
|
57
|
-
|
51
|
+
|
52
|
+
it "rounds values to 2 decimals" do
|
53
|
+
request_stat = instance_double(InstStatsd::RequestStat)
|
58
54
|
allow(request_stat).to receive(:stats).and_return(total: 72.1382928)
|
59
55
|
allow(request_stat).to receive(:exclusive_stats).and_return(nil)
|
60
56
|
results = @logger.build_log_message(request_stat)
|
@@ -64,28 +60,25 @@ describe InstStatsd::RequestLogger do
|
|
64
60
|
expect(results).to eq("[STATSD] (total: 72.13)")
|
65
61
|
end
|
66
62
|
end
|
67
|
-
|
68
63
|
end
|
69
64
|
|
70
|
-
describe
|
71
|
-
it
|
72
|
-
std_out_logger = Logger.new(
|
65
|
+
describe "#log" do
|
66
|
+
it "sends info method to logger if logger exists" do
|
67
|
+
std_out_logger = Logger.new($stdout)
|
73
68
|
logger = InstStatsd::RequestLogger.new(std_out_logger)
|
74
69
|
expect(std_out_logger).to receive(:info)
|
75
|
-
request_stat =
|
76
|
-
allow(request_stat).to
|
77
|
-
allow(request_stat).to receive(:exclusive_stats).and_return(nil)
|
70
|
+
request_stat = instance_double(InstStatsd::RequestStat)
|
71
|
+
allow(request_stat).to receive_messages(stats: {}, exclusive_stats: nil)
|
78
72
|
logger.log(request_stat)
|
79
73
|
end
|
80
|
-
|
81
|
-
|
74
|
+
|
75
|
+
it "sends info method with build_log_message output if logger exists" do
|
76
|
+
std_out_logger = Logger.new($stdout)
|
82
77
|
logger = InstStatsd::RequestLogger.new(std_out_logger)
|
83
78
|
expect(std_out_logger).to receive(:info).with("[DEFAULT_METRICS] (total: 100.20)")
|
84
|
-
request_stat =
|
85
|
-
allow(request_stat).to
|
86
|
-
allow(request_stat).to receive(:exclusive_stats).and_return(nil)
|
79
|
+
request_stat = instance_double(InstStatsd::RequestStat)
|
80
|
+
allow(request_stat).to receive_messages(stats: { total: 100.2 }, exclusive_stats: nil)
|
87
81
|
logger.log(request_stat, "DEFAULT_METRICS")
|
88
82
|
end
|
89
83
|
end
|
90
|
-
|
91
84
|
end
|