|
28 | 28 | PG_STAT_STATEMENTS_TIMING_COLUMNS,
|
29 | 29 | PG_STAT_STATEMENTS_TIMING_COLUMNS_LT_17,
|
30 | 30 | PostgresStatementMetrics,
|
| 31 | + StatementMetrics, |
| 32 | + _row_key, |
31 | 33 | )
|
32 | 34 | from datadog_checks.postgres.util import payload_pg_version
|
33 | 35 | from datadog_checks.postgres.version_utils import V12
|
@@ -2060,6 +2062,41 @@ def test_pg_stat_statements_dealloc(aggregator, integration_check, dbm_instance_
|
2060 | 2062 | aggregator.assert_metric("postgresql.pg_stat_statements.count", tags=expected_tags)
|
2061 | 2063 |
|
2062 | 2064 |
|
| 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 | + |
2063 | 2100 | @requires_over_13
|
2064 | 2101 | def test_plan_time_metrics(aggregator, integration_check, dbm_instance):
|
2065 | 2102 | dbm_instance['pg_stat_statements_view'] = "pg_stat_statements"
|
|
0 commit comments