Skip to content

Commit

Permalink
Handle FeatureNotSupported errors in queries (#5749)
Browse files Browse the repository at this point in the history
In certain scenarios (like aurora replication readers), calling
`pg_last_xlog_receive_location` can result in an unsupported exception.
This is expected, so it shouldn't make the whole check fails, and we
should ignore the error instead.
  • Loading branch information
therve committed Feb 14, 2020
1 parent 40f0784 commit 1d53b74
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
6 changes: 5 additions & 1 deletion postgres/datadog_checks/postgres/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,11 @@ def _query_scope(self, cursor, scope, instance_tags, is_custom_metrics, relation
cursor.execute(query.replace(r'%', r'%%'))

results = cursor.fetchall()

except psycopg2.errors.FeatureNotSupported as e:
# This happens for example when trying to get replication metrics
# from readers in Aurora. Let's ignote it.
log_func(e)
return
except psycopg2.errors.UndefinedFunction as e:
log_func(e)
log_func(
Expand Down
31 changes: 31 additions & 0 deletions postgres/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from semver import VersionInfo

from datadog_checks.postgres import PostgreSql
from datadog_checks.postgres.util import PartialFormatter, fmt

from .common import DB_NAME, HOST, PORT, POSTGRES_VERSION, check_bgw_metrics, check_common_metrics
from .utils import requires_over_10
Expand Down Expand Up @@ -45,6 +46,36 @@ def test_common_metrics_without_size(aggregator, integration_check, pg_instance)
assert 'postgresql.database_size' not in aggregator.metric_names


@pytest.mark.integration
@pytest.mark.usefixtures('dd_environment')
def test_unsupported_replication(aggregator, integration_check, pg_instance):
check = integration_check(pg_instance)

unpatched_fmt = PartialFormatter()

called = []

def format_with_error(value, **kwargs):
if 'pg_is_in_recovery' in value:
called.append(True)
raise psycopg2.errors.FeatureNotSupported("Not available")
return unpatched_fmt.format(value, **kwargs)

# This simulate an error in the fmt function, as it's a bit hard to mock psycopg
with mock.patch.object(fmt, 'format', passthrough=True) as mock_fmt:
mock_fmt.side_effect = format_with_error
check.check(pg_instance)

# Verify our mocking was called
assert called == [True]

expected_tags = pg_instance['tags'] + ['server:{}'.format(HOST), 'port:{}'.format(PORT)]
check_bgw_metrics(aggregator, expected_tags)

expected_tags += ['db:{}'.format(DB_NAME)]
check_common_metrics(aggregator, expected_tags=expected_tags)


@pytest.mark.integration
@pytest.mark.usefixtures('dd_environment')
def test_can_connect_service_check(aggregator, integration_check, pg_instance):
Expand Down

0 comments on commit 1d53b74

Please sign in to comment.