Skip to content

Includes slowlog complexity info if available #1489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,17 +400,23 @@ def parse_zscan(response, **options):

def parse_slowlog_get(response, **options):
space = ' ' if options.get('decode_responses', False) else b' '
return [{
'id': item[0],
'start_time': int(item[1]),
'duration': int(item[2]),
'command':
# Redis Enterprise injects another entry at index [3], which has
# the complexity info (i.e. the value N in case the command has
# an O(N) complexity) instead of the command.
space.join(item[3]) if isinstance(item[3], list) else
space.join(item[4])
} for item in response]

def parse_item(item):
result = {
'id': item[0],
'start_time': int(item[1]),
'duration': int(item[2]),
}
# Redis Enterprise injects another entry at index [3], which has
# the complexity info (i.e. the value N in case the command has
# an O(N) complexity) instead of the command.
if isinstance(item[3], list):
result['command'] = space.join(item[3])
else:
result['complexity'] = item[3]
result['command'] = space.join(item[4])
return result
return [parse_item(item) for item in response]


def parse_cluster_info(response, **options):
Expand Down
29 changes: 29 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,35 @@ def test_slowlog_get(self, r, slowlog):
assert isinstance(slowlog[0]['start_time'], int)
assert isinstance(slowlog[0]['duration'], int)

# Mock result if we didn't get slowlog complexity info.
if 'complexity' not in slowlog[0]:
# monkey patch parse_response()
COMPLEXITY_STATEMENT = "Complexity info: N:4712,M:3788"
old_parse_response = r.parse_response

def parse_response(connection, command_name, **options):
if command_name != 'SLOWLOG GET':
return old_parse_response(connection,
command_name,
**options)
responses = connection.read_response()
for response in responses:
# Complexity info stored as fourth item in list
response.insert(3, COMPLEXITY_STATEMENT)
return r.response_callbacks[command_name](responses, **options)
r.parse_response = parse_response

# test
slowlog = r.slowlog_get()
assert isinstance(slowlog, list)
commands = [log['command'] for log in slowlog]
assert get_command in commands
idx = commands.index(get_command)
assert slowlog[idx]['complexity'] == COMPLEXITY_STATEMENT

# tear down monkeypatch
r.parse_response = old_parse_response

def test_slowlog_get_limit(self, r, slowlog):
assert r.slowlog_reset()
r.get('foo')
Expand Down