Skip to content

Commit 57fdb3b

Browse files
authored
[postgres] Add calls to query metrics derivative compute execution_indicators (#20096)
* add calls to metrics derivative compute execution_indicators * fix tests * add changelog * update base package min version
1 parent 90a8217 commit 57fdb3b

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

postgres/changelog.d/20096.added

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added query call count `calls` to StatementMetrics execution indicators to filter out false positives from normalized queries being evicted and re-inserted with same call count and slight duration change.

postgres/datadog_checks/postgres/statements.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ def _collect_metrics_rows(self):
613613
available_columns = set(rows[0].keys())
614614
metric_columns = available_columns & PG_STAT_STATEMENTS_METRICS_COLUMNS
615615

616-
rows = self._state.compute_derivative_rows(rows, metric_columns, key=_row_key)
616+
rows = self._state.compute_derivative_rows(rows, metric_columns, key=_row_key, execution_indicators=['calls'])
617617
self._check.gauge(
618618
'dd.postgres.queries.query_rows_raw',
619619
len(rows),

postgres/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ classifiers = [
2828
"Private :: Do Not Upload",
2929
]
3030
dependencies = [
31-
"datadog-checks-base>=37.4.0",
31+
"datadog-checks-base>=37.10.0",
3232
]
3333
dynamic = [
3434
"version",

postgres/tests/test_statements.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
PG_STAT_STATEMENTS_TIMING_COLUMNS,
2929
PG_STAT_STATEMENTS_TIMING_COLUMNS_LT_17,
3030
PostgresStatementMetrics,
31+
StatementMetrics,
32+
_row_key,
3133
)
3234
from datadog_checks.postgres.util import payload_pg_version
3335
from datadog_checks.postgres.version_utils import V12
@@ -2060,6 +2062,41 @@ def test_pg_stat_statements_dealloc(aggregator, integration_check, dbm_instance_
20602062
aggregator.assert_metric("postgresql.pg_stat_statements.count", tags=expected_tags)
20612063

20622064

2065+
@pytest.mark.parametrize(
2066+
"prev_row,curr_row,expected_included",
2067+
[
2068+
# Base case - normal increase in calls and duration
2069+
({"calls": 5, "total_time": 100}, {"calls": 6, "total_time": 120}, True),
2070+
# Test case - no change in calls but duration increased
2071+
({"calls": 5, "total_time": 100}, {"calls": 5, "total_time": 120}, False),
2072+
# Test case - calls=0 with duration>0
2073+
({"calls": 0, "total_time": 0}, {"calls": 0, "total_time": 50}, False),
2074+
],
2075+
)
2076+
def test_statement_metrics_execution_indicators(prev_row, curr_row, expected_included):
2077+
"""Test that queries are only included in results if execution indicators (calls) have increased."""
2078+
# Setup
2079+
state = StatementMetrics()
2080+
metrics = {"calls", "total_time"}
2081+
execution_indicators = ["calls"]
2082+
2083+
# Add query signature and other required fields
2084+
prev_row.update({"query_signature": "test_query", "datname": "test_db", "rolname": "test_role"})
2085+
curr_row.update({"query_signature": "test_query", "datname": "test_db", "rolname": "test_role"})
2086+
2087+
# First run to establish baseline
2088+
state.compute_derivative_rows([prev_row], metrics, key=_row_key, execution_indicators=execution_indicators)
2089+
2090+
# Second run to test the behavior
2091+
result = state.compute_derivative_rows([curr_row], metrics, key=_row_key, execution_indicators=execution_indicators)
2092+
2093+
# Verify
2094+
assert bool(result) == expected_included
2095+
if result:
2096+
assert result[0]["calls"] == curr_row["calls"] - prev_row["calls"]
2097+
assert result[0]["total_time"] == curr_row["total_time"] - prev_row["total_time"]
2098+
2099+
20632100
@requires_over_13
20642101
def test_plan_time_metrics(aggregator, integration_check, dbm_instance):
20652102
dbm_instance['pg_stat_statements_view'] = "pg_stat_statements"

0 commit comments

Comments
 (0)