From 4687a30094c8319454d698490cdfc8b10ac70065 Mon Sep 17 00:00:00 2001 From: Eric Weaver Date: Thu, 15 May 2025 16:16:56 -0400 Subject: [PATCH 1/4] Check for aws extensions before querying aurora_version --- .../datadog_checks/postgres/version_utils.py | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/postgres/datadog_checks/postgres/version_utils.py b/postgres/datadog_checks/postgres/version_utils.py index 89b27e3e3d735..8ad384c3c88b0 100644 --- a/postgres/datadog_checks/postgres/version_utils.py +++ b/postgres/datadog_checks/postgres/version_utils.py @@ -27,7 +27,7 @@ class VersionUtils(object): def __init__(self): self.log = get_check_logger() - self._seen_aurora_exception = False + self._is_aurora = None @staticmethod def get_raw_version(db): @@ -38,21 +38,25 @@ def get_raw_version(db): return raw_version def is_aurora(self, db): - if self._seen_aurora_exception: - return False + if self._is_aurora is not None: + return self._is_aurora with db as conn: with conn.cursor(cursor_factory=CommenterCursor) as cursor: - # This query will pollute PG logs in non aurora versions, - # but is the only reliable way to detect aurora - try: - cursor.execute('select AURORA_VERSION();') - return True - except Exception as e: - self.log.debug( - "Captured exception %s while determining if the DB is aurora. Assuming is not", str(e) - ) - self._seen_aurora_exception = True - return False + cursor.execute("SELECT * FROM pg_available_extension_versions WHERE name ILIKE '%aurora%' OR comment ILIKE '%aurora%';") + if len(cursor.fetchall()) > 0: + # This query will pollute PG logs in non aurora versions, + # but is the only reliable way to detect aurora. + # Since we found aurora extensions, this should exist. + try: + cursor.execute('select AURORA_VERSION();') + self._is_aurora = True + return self._is_aurora + except Exception as e: + self.log.debug( + "Captured exception %s while determining if the DB is aurora. Assuming is not", str(e) + ) + self._is_aurora = False + return self._is_aurora @staticmethod def parse_version(raw_version): From 27060014590affdf5b79d267b3715de1885c8abb Mon Sep 17 00:00:00 2001 From: Eric Weaver Date: Thu, 15 May 2025 16:17:55 -0400 Subject: [PATCH 2/4] Create 20310.fixed --- postgres/changelog.d/20310.fixed | 1 + 1 file changed, 1 insertion(+) create mode 100644 postgres/changelog.d/20310.fixed diff --git a/postgres/changelog.d/20310.fixed b/postgres/changelog.d/20310.fixed new file mode 100644 index 0000000000000..cb3688a96b8f9 --- /dev/null +++ b/postgres/changelog.d/20310.fixed @@ -0,0 +1 @@ +Check for aws extensions before querying aurora_version() From f10d082af3d4a5fd048c1e6ae2a799e21d15830c Mon Sep 17 00:00:00 2001 From: Eric Weaver Date: Fri, 16 May 2025 11:16:44 -0400 Subject: [PATCH 3/4] Minor query tuning --- postgres/datadog_checks/postgres/version_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/postgres/datadog_checks/postgres/version_utils.py b/postgres/datadog_checks/postgres/version_utils.py index 8ad384c3c88b0..ecdcc2e0434f4 100644 --- a/postgres/datadog_checks/postgres/version_utils.py +++ b/postgres/datadog_checks/postgres/version_utils.py @@ -42,8 +42,8 @@ def is_aurora(self, db): return self._is_aurora with db as conn: with conn.cursor(cursor_factory=CommenterCursor) as cursor: - cursor.execute("SELECT * FROM pg_available_extension_versions WHERE name ILIKE '%aurora%' OR comment ILIKE '%aurora%';") - if len(cursor.fetchall()) > 0: + cursor.execute("SELECT 1 FROM pg_available_extension_versions WHERE name ILIKE '%aurora%' OR comment ILIKE '%aurora%' LIMIT 1;") + if cursor.fetchone(): # This query will pollute PG logs in non aurora versions, # but is the only reliable way to detect aurora. # Since we found aurora extensions, this should exist. From e37ec05581c871a69e85648875a8649d509713e1 Mon Sep 17 00:00:00 2001 From: Eric Weaver Date: Fri, 16 May 2025 11:24:24 -0400 Subject: [PATCH 4/4] fix linting error --- postgres/datadog_checks/postgres/version_utils.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/postgres/datadog_checks/postgres/version_utils.py b/postgres/datadog_checks/postgres/version_utils.py index ecdcc2e0434f4..349f866054b4f 100644 --- a/postgres/datadog_checks/postgres/version_utils.py +++ b/postgres/datadog_checks/postgres/version_utils.py @@ -42,7 +42,11 @@ def is_aurora(self, db): return self._is_aurora with db as conn: with conn.cursor(cursor_factory=CommenterCursor) as cursor: - cursor.execute("SELECT 1 FROM pg_available_extension_versions WHERE name ILIKE '%aurora%' OR comment ILIKE '%aurora%' LIMIT 1;") + cursor.execute( + "SELECT 1 FROM pg_available_extension_versions " + "WHERE name ILIKE '%aurora%' OR comment ILIKE '%aurora%' " + "LIMIT 1;" + ) if cursor.fetchone(): # This query will pollute PG logs in non aurora versions, # but is the only reliable way to detect aurora.