diff options
| author | Ian Bucad <ian.bucad@datadoghq.com> | 2021-08-29 18:40:29 +1000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-08-29 11:40:29 +0300 |
| commit | 41e3f56f65b690ae39ab798a85b42e44ee72a829 (patch) | |
| tree | b03d494beb2c69bbb0dc1226c1805601456679c7 | |
| parent | 9f82778b78b2e4fd1482255edc91d10c4dda2988 (diff) | |
| download | redis-py-41e3f56f65b690ae39ab798a85b42e44ee72a829.tar.gz | |
Includes slowlog complexity info if available (#1489)
* Return slowlog complexity info if available
based on https://github.com/andymccurdy/redis-py/pull/622
* Add tests
Copied from https://github.com/andymccurdy/redis-py/pull/622
* address flake E306
* Trigger Build
| -rwxr-xr-x | redis/client.py | 28 | ||||
| -rw-r--r-- | tests/test_commands.py | 29 |
2 files changed, 46 insertions, 11 deletions
diff --git a/redis/client.py b/redis/client.py index 3a9a5b6..4a9e33a 100755 --- a/redis/client.py +++ b/redis/client.py @@ -397,17 +397,23 @@ def parse_zmscore(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_stralgo(response, **options): diff --git a/tests/test_commands.py b/tests/test_commands.py index 4b7957c..0e71fcf 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -499,6 +499,35 @@ class TestRedisCommands: 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') |
