Skip to content

Commit 41e3f56

Browse files
authored
Includes slowlog complexity info if available (#1489)
* Return slowlog complexity info if available based on #622 * Add tests Copied from #622 * address flake E306 * Trigger Build
1 parent 9f82778 commit 41e3f56

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

redis/client.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -397,17 +397,23 @@ def parse_zmscore(response, **options):
397397

398398
def parse_slowlog_get(response, **options):
399399
space = ' ' if options.get('decode_responses', False) else b' '
400-
return [{
401-
'id': item[0],
402-
'start_time': int(item[1]),
403-
'duration': int(item[2]),
404-
'command':
405-
# Redis Enterprise injects another entry at index [3], which has
406-
# the complexity info (i.e. the value N in case the command has
407-
# an O(N) complexity) instead of the command.
408-
space.join(item[3]) if isinstance(item[3], list) else
409-
space.join(item[4])
410-
} for item in response]
400+
401+
def parse_item(item):
402+
result = {
403+
'id': item[0],
404+
'start_time': int(item[1]),
405+
'duration': int(item[2]),
406+
}
407+
# Redis Enterprise injects another entry at index [3], which has
408+
# the complexity info (i.e. the value N in case the command has
409+
# an O(N) complexity) instead of the command.
410+
if isinstance(item[3], list):
411+
result['command'] = space.join(item[3])
412+
else:
413+
result['complexity'] = item[3]
414+
result['command'] = space.join(item[4])
415+
return result
416+
return [parse_item(item) for item in response]
411417

412418

413419
def parse_stralgo(response, **options):

tests/test_commands.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,35 @@ def test_slowlog_get(self, r, slowlog):
499499
assert isinstance(slowlog[0]['start_time'], int)
500500
assert isinstance(slowlog[0]['duration'], int)
501501

502+
# Mock result if we didn't get slowlog complexity info.
503+
if 'complexity' not in slowlog[0]:
504+
# monkey patch parse_response()
505+
COMPLEXITY_STATEMENT = "Complexity info: N:4712,M:3788"
506+
old_parse_response = r.parse_response
507+
508+
def parse_response(connection, command_name, **options):
509+
if command_name != 'SLOWLOG GET':
510+
return old_parse_response(connection,
511+
command_name,
512+
**options)
513+
responses = connection.read_response()
514+
for response in responses:
515+
# Complexity info stored as fourth item in list
516+
response.insert(3, COMPLEXITY_STATEMENT)
517+
return r.response_callbacks[command_name](responses, **options)
518+
r.parse_response = parse_response
519+
520+
# test
521+
slowlog = r.slowlog_get()
522+
assert isinstance(slowlog, list)
523+
commands = [log['command'] for log in slowlog]
524+
assert get_command in commands
525+
idx = commands.index(get_command)
526+
assert slowlog[idx]['complexity'] == COMPLEXITY_STATEMENT
527+
528+
# tear down monkeypatch
529+
r.parse_response = old_parse_response
530+
502531
def test_slowlog_get_limit(self, r, slowlog):
503532
assert r.slowlog_reset()
504533
r.get('foo')

0 commit comments

Comments
 (0)