Skip to content

Commit ddbae62

Browse files
committed
chore: refactor for clarity
1 parent 55cb30a commit ddbae62

File tree

2 files changed

+43
-45
lines changed

2 files changed

+43
-45
lines changed

lib/slowlog_check.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ def replication_group
2222
end
2323
end
2424

25+
def status_or_error(resp)
26+
return resp[1].fetch("status") if resp[1].key?("status")
27+
return resp[1].fetch("errors") if resp[1].key?("errors")
28+
return resp
29+
end
30+
2531
# TODO: Rather than hard code a day lookback,
2632
# look back at an increasing increment until hitting some max value
2733
def last_datadog_metrics_submitted_by_me_in_the_last_day
@@ -222,6 +228,9 @@ def ship_slowlogs
222228
end
223229

224230

231+
##
232+
# Metadata
233+
225234
def metric_metadatas
226235
[
227236
'avg',
@@ -279,15 +288,10 @@ def update_metadatas
279288
name = metadata.delete("name")
280289
resp = @ddog.update_metadata(
281290
name,
282-
metadata.transform_keys { |key| key.to_sym rescue key }
291+
metadata.transform_keys { |key| key.to_sym }
283292
)
284293
LOGGER.info "Updating metadata for #{name} #{status_or_error(resp)}"
285294
end
286295
end
287296

288-
def status_or_error(resp)
289-
return resp[1].fetch("status") if resp[1].key?("status")
290-
return resp[1].fetch("errors") if resp[1].key?("errors")
291-
return resp
292-
end
293297
end

spec/slowlog_check_spec.rb

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
}
1717

1818
before(:example) do
19+
##
20+
# redis mock
1921
allow(redis).to receive(:connection) { {host: 'master.replicationgroup.abcde.use2.cache.amazonaws.com' } }
2022
allow(redis).to receive(:slowlog).with('get') {
2123
[
2224
[
2325
1,
24-
Time.new(2020,04,20,04,21,15).to_i,
26+
Time.new(2020,04,20,04,19,45).to_i,
2527
100000,
2628
[
2729
"eval",
@@ -33,7 +35,7 @@
3335
],
3436
[
3537
0,
36-
Time.new(2020,04,20,04,21,13).to_i,
38+
Time.new(2020,04,20,04,19,15).to_i,
3739
200000,
3840
[
3941
"eval",
@@ -46,6 +48,8 @@
4648
]
4749
}
4850

51+
##
52+
# ddog mock
4953
allow(ddog).to receive(:get_points).with(
5054
'rspec.redis.slowlog.micros.95percentile{replication_group:replicationgroup}',
5155
Time.now - 86400,
@@ -68,8 +72,8 @@
6872
"length"=>3,
6973
"query_index"=>0,
7074
"aggr"=>nil,
71-
"scope"=>"replication_group:infraeng-dev-redis",
72-
"pointlist"=>[[1587602100000.0, 99848.0], [1587603600000.0, 99994.0], [1587684300000.0, 99378.0]],
75+
"scope"=>"replication_group:replicationgroup",
76+
"pointlist"=>[[1587381405000.0, 99994.0], [1587381404000.0, 99378.0]],
7377
"expression"=>"rspec.redis.slowlog.micros.95percentile{replication_group:infraeng-dev-redis}",
7478
"unit"=>nil,
7579
"display_name"=>"rspec.redis.slowlog.micros.95percentile"
@@ -83,12 +87,12 @@
8387
]
8488
}
8589

86-
90+
# Freeze time
8791
Timecop.freeze(2020, 04, 20, 04, 20, 45)
8892

93+
# Shhh...
8994
allow_any_instance_of(Logger).to receive(:info) {}
9095

91-
allow(slowlog_check).to receive(:last_time_submitted).and_return(slowlog_check.minute_precision(Time.now) - 240)
9296
end
9397

9498

@@ -107,6 +111,24 @@
107111
end
108112
end
109113

114+
describe '#status_or_error' do
115+
context 'ok' do
116+
subject { slowlog_check.status_or_error(["200", {"status" => "ok"}]) }
117+
it { is_expected.to eq('ok') }
118+
end
119+
120+
context 'error' do
121+
subject { slowlog_check.status_or_error(["404", {"errors" => ["error"]}]) }
122+
it { is_expected.to eq(['error']) }
123+
end
124+
125+
context 'otherwise' do
126+
subject { slowlog_check.status_or_error(["404", {"somenewthing" => ["error"]}]) }
127+
it { is_expected.to eq(["404", {"somenewthing" => ['error']}]) }
128+
end
129+
130+
end
131+
110132
describe '#last_datadog_metric' do
111133
subject { slowlog_check.last_datadog_metric}
112134

@@ -118,7 +140,7 @@
118140
end
119141

120142
context 'nth time' do
121-
it { is_expected.to eq(Time.at(1587684300)) }
143+
it { is_expected.to eq(Time.new(2020,4,20,4,16)) }
122144
end
123145
end
124146

@@ -147,6 +169,7 @@
147169
end
148170

149171
describe '#add_metric_to_bucket' do
172+
subject { slowlog_check.add_metric_to_bucket(prior, new) }
150173
let(:prior) {
151174
{
152175
values: [10],
@@ -172,13 +195,11 @@
172195
sum: 30
173196
}
174197
}
175-
subject { slowlog_check.add_metric_to_bucket(prior, new) }
176198
it { is_expected.to eq(result) }
177199
end
178200

179201
describe '#slowlogs_by_flush_interval' do
180202
subject { slowlog_check.slowlogs_by_flush_interval }
181-
182203
let(:bucket) {
183204
{
184205
"eval" =>
@@ -198,9 +219,8 @@
198219
{
199220
Time.new(2020,04,20,04,17) => nil,
200221
Time.new(2020,04,20,04,18) => nil,
201-
Time.new(2020,04,20,04,19) => nil,
202-
Time.new(2020,04,20,04,20) => nil,
203-
Time.new(2020,04,20,04,21) => bucket
222+
Time.new(2020,04,20,04,19) => bucket,
223+
Time.new(2020,04,20,04,20) => nil
204224
}
205225
)
206226
}
@@ -223,14 +243,13 @@
223243
describe '#ship_slowlogs' do
224244
subject { slowlog_check.ship_slowlogs }
225245
let(:tags) { slowlog_check.default_tags.merge(command: 'eval') }
226-
227246
it 'sends the right data to datadog' do
228247
allow(ddog).to receive(:emit_points) {["200", { "status" => "ok" }]}
229248
subject
230249

231250
expect(ddog).to have_received(:emit_points).with(
232251
"rspec.redis.slowlog.micros.avg",
233-
[[Time.new(2020,04,20,04,21), 150000]],
252+
[[Time.new(2020,04,20,04,19), 150000]],
234253
{
235254
:host=>"replicationgroup",
236255
:interval=>60,
@@ -269,7 +288,6 @@
269288

270289
describe '#diff_metadatas' do
271290
subject { slowlog_check.diff_metadatas }
272-
273291
let(:diff) {
274292
{
275293
"name"=>"rspec.redis.slowlog.micros.count",
@@ -282,7 +300,6 @@
282300
"unit"=>"entries"
283301
}
284302
}
285-
286303
it { is_expected.to contain_exactly(diff) }
287304
end
288305

@@ -307,30 +324,7 @@
307324
'rspec.redis.slowlog.micros.count',
308325
diff
309326
)
310-
311327
end
312328
end
313329
end
314-
315-
describe '#status_or_error' do
316-
context 'ok' do
317-
subject { slowlog_check.status_or_error(["200", {"status" => "ok"}]) }
318-
319-
it { is_expected.to eq('ok') }
320-
321-
end
322-
323-
context 'error' do
324-
subject { slowlog_check.status_or_error(["404", {"errors" => ["error"]}]) }
325-
326-
it { is_expected.to eq(['error']) }
327-
end
328-
329-
context 'otherwise' do
330-
subject { slowlog_check.status_or_error(["404", {"somenewthing" => ["error"]}]) }
331-
332-
it { is_expected.to eq(["404", {"somenewthing" => ['error']}]) }
333-
end
334-
335-
end
336330
end

0 commit comments

Comments
 (0)