Skip to content

Commit 0d46dc7

Browse files
authored
Allow slowlog collection to fail (#20261)
* Allow slowlog collection to fail * Add changelog * Add test * Run formatter * Fix test and add second one * Remove unnecessary aggregator param
1 parent 67321e4 commit 0d46dc7

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

redisdb/changelog.d/20261.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow slowlog collection to fail without error to support some managed Redis that do not allow SLOWLOG GET.

redisdb/datadog_checks/redisdb/redisdb.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,11 @@ def upstream_parse_slowlog_get(response, **options):
516516
max_slow_entries = int(self.instance.get(MAX_SLOW_ENTRIES_KEY))
517517

518518
# Get all slowlog entries
519-
slowlogs = conn.slowlog_get(max_slow_entries)
519+
try:
520+
slowlogs = conn.slowlog_get(max_slow_entries)
521+
except redis.ResponseError:
522+
self.log.debug("Unable to collect slow log: SLOWLOG GET disabled in some managed Redis.")
523+
return
520524

521525
# Find slowlog entries between last timestamp and now using start_time
522526
slowlogs = [s for s in slowlogs if s['start_time'] > self.last_timestamp_seen]

redisdb/tests/test_unit.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Licensed under a 3-clause BSD style license (see LICENSE)
44
import mock
55
import pytest
6+
from redis.exceptions import ResponseError
67

78
from datadog_checks.dev.utils import get_metadata_metrics
89

@@ -102,3 +103,36 @@ def test__check_total_commands_processed_present(check, aggregator, redis_instan
102103

103104
# Assert that the `redis.net.commands` metric was sent
104105
aggregator.assert_metric('redis.net.commands', value=1000, tags=['test_total_commands_processed'])
106+
107+
108+
def test_slowlog_quiet_failure(check, aggregator, redis_instance):
109+
"""
110+
The check should not fail if the slowlog command fails with redis.ResponseError
111+
"""
112+
redis_check = check(redis_instance)
113+
114+
# Mock the connection object returned by _get_conn
115+
mock_conn = mock.MagicMock()
116+
mock_conn.slowlog_get.side_effect = ResponseError('ERR unknown command `SLOWLOG`')
117+
mock_conn.config_get.return_value = {'slowlog-max-len': '128'}
118+
119+
with mock.patch.object(redis_check, '_get_conn', return_value=mock_conn):
120+
redis_check._check_slowlog()
121+
# Assert that no metrics were sent
122+
aggregator.assert_metric('redis.slowlog.micros', count=0)
123+
124+
125+
def test_slowlog_loud_failure(check, redis_instance):
126+
"""
127+
The check should fail if the slowlog command fails for any other reason
128+
"""
129+
redis_check = check(redis_instance)
130+
131+
# Mock the connection object returned by _get_conn
132+
mock_conn = mock.MagicMock()
133+
mock_conn.slowlog_get.side_effect = RuntimeError('Some other error')
134+
mock_conn.config_get.return_value = {'slowlog-max-len': '128'}
135+
136+
with mock.patch.object(redis_check, '_get_conn', return_value=mock_conn):
137+
with pytest.raises(RuntimeError, match='Some other error'):
138+
redis_check._check_slowlog()

0 commit comments

Comments
 (0)