From f76a27a4d26152529460a7f1c8af68cc947bf818 Mon Sep 17 00:00:00 2001 From: jmeunier28 Date: Fri, 1 Apr 2022 16:16:10 -0700 Subject: [PATCH] Bug fix: fix postgres activity inflated query durations --- postgres/datadog_checks/postgres/statement_samples.py | 10 ++++++++-- postgres/tests/test_statements.py | 6 +++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/postgres/datadog_checks/postgres/statement_samples.py b/postgres/datadog_checks/postgres/statement_samples.py index 432ea6cbceb6a..0068ff6f8a5be 100644 --- a/postgres/datadog_checks/postgres/statement_samples.py +++ b/postgres/datadog_checks/postgres/statement_samples.py @@ -75,12 +75,13 @@ ] PG_BLOCKING_PIDS_FUNC = ",pg_blocking_pids(pid) as blocking_pids" +CURRENT_TIME_FUNC = "clock_timestamp() as now," PG_STAT_ACTIVITY_QUERY = re.sub( r'\s+', ' ', """ - SELECT {pg_stat_activity_cols} {pg_blocking_func} FROM {pg_stat_activity_view} + SELECT {current_time_func} {pg_stat_activity_cols} {pg_blocking_func} FROM {pg_stat_activity_view} WHERE coalesce(TRIM(query), '') != '' AND query_start IS NOT NULL {extra_filters} @@ -236,12 +237,17 @@ def _get_active_connections(self): def _get_new_pg_stat_activity(self, available_activity_columns): start_time = time.time() extra_filters, params = self._get_extra_filters_and_params(filter_stale_idle_conn=True) + report_activity = self._report_activity_event() + cur_time_func = "" blocking_func = "" # minimum version for pg_blocking_pids function is v9.6 # only call pg_blocking_pids as often as we collect activity snapshots - if self._check.version >= V9_6 and self._report_activity_event(): + if self._check.version >= V9_6 and report_activity: blocking_func = PG_BLOCKING_PIDS_FUNC + if report_activity: + cur_time_func = CURRENT_TIME_FUNC query = PG_STAT_ACTIVITY_QUERY.format( + current_time_func=cur_time_func, pg_stat_activity_cols=', '.join(available_activity_columns), pg_blocking_func=blocking_func, pg_stat_activity_view=self._config.pg_stat_activity_view, diff --git a/postgres/tests/test_statements.py b/postgres/tests/test_statements.py index 1278ee2c17b03..67041b604a5e2 100644 --- a/postgres/tests/test_statements.py +++ b/postgres/tests/test_statements.py @@ -11,6 +11,7 @@ import mock import psycopg2 import pytest +from dateutil import parser from semver import VersionInfo from six import string_types @@ -644,7 +645,7 @@ def obfuscate_sql(query, options=None): 'query_signature': '9382c42e92099c04', 'statement': "BEGIN TRANSACTION; SELECT city FROM persons WHERE city = 'hello';", }, - ["xact_start", "query_start", "pid", "client_port", "client_addr", "backend_type", "blocking_pids"], + ["now", "xact_start", "query_start", "pid", "client_port", "client_addr", "backend_type", "blocking_pids"], { 'usename': 'bob', 'state': 'active', @@ -735,6 +736,9 @@ def wait(conn): for val in expected_keys: assert val in bobs_query + # assert that the current timestamp is being collected as an ISO timestamp with TZ info + assert parser.isoparse(bobs_query['now']).tzinfo, "current timestamp not formatted correctly" + if 'blocking_pids' in expected_keys: # if we are collecting pg blocking information, then # blocking_bob's pid should show up in bob's activity