Skip to content

Commit ed7b094

Browse files
committed
feat: When slowlogs no longer appear following a slowlog event, post a zero for that command.
fixes #4
1 parent 0eb807f commit ed7b094

File tree

2 files changed

+116
-1
lines changed

2 files changed

+116
-1
lines changed

lib/slowlog_check.rb

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def slowlog_microseconds(slowlog)
7777
slowlog[2]
7878
end
7979

80+
8081
def reporting_interval
8182
now_i = minute_precision(Time.now).to_i - 60
8283
start_time_i = last_time_submitted.to_i + 60
@@ -125,6 +126,47 @@ def redis_slowlog(length=128)
125126
return redis_slowlog(length * 2)
126127
end
127128

129+
def empty_values
130+
{
131+
values: [],
132+
avg: 0,
133+
count: 0,
134+
median: 0,
135+
_95percentile: 0,
136+
min: 0,
137+
max: 0,
138+
sum: 0
139+
}
140+
end
141+
142+
def new_commands(timestamp, timebucket)
143+
result = {}
144+
timebucket.keys.each do |command|
145+
result[command] = timestamp
146+
end
147+
result
148+
end
149+
150+
def pad_results_with_zero(report)
151+
@seen_commands ||= {}
152+
report.keys.sort.each do |timebucket|
153+
# Collect new_commands
154+
new_commands = new_commands(timebucket, report[timebucket])
155+
156+
# donut zero existing commands
157+
commands_to_zero = @seen_commands.keys - new_commands.keys
158+
commands_to_zero.each do |command|
159+
# inject_zeroing_commands
160+
report[timebucket].merge!({command => empty_values})
161+
# And remove it from the seen_commands queue
162+
@seen_commands.delete(command)
163+
end
164+
# add new_commands to seen_commands queue
165+
@seen_commands.merge!(new_commands)
166+
end
167+
report
168+
end
169+
128170
def slowlogs_by_flush_interval
129171
result = reporting_interval
130172
redis_slowlog.each do |slowlog|
@@ -164,7 +206,7 @@ def slowlogs_by_flush_interval
164206
end
165207
end
166208

167-
result
209+
pad_results_with_zero(result)
168210
end
169211

170212
def default_tags

spec/slowlog_check_spec.rb

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,34 @@ def redis_slowlog(index, time, microseconds, command='eval')
261261
it { is_expected.to eq(result) }
262262
end
263263

264+
describe '#pad_results_with_zero' do
265+
let(:report) {
266+
{
267+
Time.utc(2020,4,20,4,16) => {"a" => {}},
268+
Time.utc(2020,4,20,4,17) => {"b" => {}},
269+
Time.utc(2020,4,20,4,18) => {},
270+
Time.utc(2020,4,20,4,19) => {"a" => {}},
271+
}
272+
273+
}
274+
subject {slowlog_check.pad_results_with_zero(report)}
275+
it { is_expected.to eq(
276+
{
277+
Time.utc(2020,4,20,4,16) => {"a" => {}},
278+
Time.utc(2020,4,20,4,17) => {"b" => {}, "a" => slowlog_check.empty_values},
279+
Time.utc(2020,4,20,4,18) => {"b" => slowlog_check.empty_values},
280+
Time.utc(2020,4,20,4,19) => {"a" => {}},
281+
282+
}
283+
)
284+
}
285+
286+
describe '#new_commands' do
287+
subject { slowlog_check.new_commands(Time.utc(2020,4,20,4,16), {"a" => {}}) }
288+
it { is_expected.to eq({"a" => Time.utc(2020,4,20,4,16).localtime}) }
289+
end
290+
end
291+
264292
describe '#slowlogs_by_flush_interval' do
265293
subject { slowlog_check.slowlogs_by_flush_interval }
266294
let(:bucket18) {
@@ -302,6 +330,51 @@ def redis_slowlog(index, time, microseconds, command='eval')
302330
}
303331
)
304332
}
333+
334+
describe 'remembers commands it has seen' do
335+
def example_bucket(index)
336+
value = index + 1000
337+
collector = {
338+
index.to_s =>
339+
{
340+
_95percentile: value,
341+
avg: value,
342+
count: value == 0 ? value : 1,
343+
max: value,
344+
median: value,
345+
min: value,
346+
sum: value,
347+
values: value == 0 ? [] : [value]
348+
}
349+
}
350+
if index > 0
351+
collector.merge!({(index -1).to_s => slowlog_check.empty_values})
352+
end
353+
collector
354+
end
355+
356+
before(:example) {
357+
allow(redis).to receive(:slowlog).with('get', 128) {
358+
Array.new(5) { |x|
359+
redis_slowlog(x, Time.utc(2020,04,20,04,15,10) + (x * 60), x + 1000, x.to_s)
360+
}.reverse
361+
}
362+
363+
allow(slowlog_check).to receive(:last_time_submitted) { Time.utc(2020,4,20,4,14) }
364+
}
365+
366+
it { is_expected.to eq(
367+
{
368+
Time.utc(2020,4,20,4,15).localtime => example_bucket(0),
369+
Time.utc(2020,4,20,4,16).localtime => example_bucket(1),
370+
Time.utc(2020,4,20,4,17).localtime => example_bucket(2),
371+
Time.utc(2020,4,20,4,18).localtime => example_bucket(3),
372+
Time.utc(2020,4,20,4,19).localtime => example_bucket(4),
373+
}
374+
)
375+
}
376+
377+
end
305378
end
306379

307380
describe '#default_tags' do

0 commit comments

Comments
 (0)