Skip to content

Commit a32934f

Browse files
authored
[dbm] Fix logic for only_custom_queries parameter (#21708)
* fix logic for only_custom_queries parameter * fix custom queries test * add changelog * run ddev test format
1 parent 3da9d88 commit a32934f

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

postgres/changelog.d/21708.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix logic for only_custom_queries configuration option.

postgres/datadog_checks/postgres/postgres.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,17 +1099,21 @@ def check(self, _):
10991099

11001100
self.log.debug("Running check against version %s: is_aurora: %s", str(self.version), str(self.is_aurora))
11011101
self._emit_running_metric()
1102-
self._collect_stats(tags)
1102+
1103+
if not self._config.only_custom_queries:
1104+
self._collect_stats(tags)
1105+
if self._config.dbm:
1106+
self.statement_metrics.run_job_loop(tags)
1107+
self.statement_samples.run_job_loop(tags)
1108+
self.metadata_samples.run_job_loop(tags)
1109+
if self._config.collect_wal_metrics:
1110+
# collect wal metrics for pg < 10, disabled by enabled
1111+
self._collect_wal_metrics()
1112+
11031113
if self._query_manager.queries:
11041114
self._query_manager.executor = functools.partial(self.execute_query_raw, db=self.db)
11051115
self._query_manager.execute(extra_tags=tags)
1106-
if self._config.dbm:
1107-
self.statement_metrics.run_job_loop(tags)
1108-
self.statement_samples.run_job_loop(tags)
1109-
self.metadata_samples.run_job_loop(tags)
1110-
if self._config.collect_wal_metrics:
1111-
# collect wal metrics for pg < 10, disabled by enabled
1112-
self._collect_wal_metrics()
1116+
11131117
except Exception as e:
11141118
self.log.exception("Unable to collect postgres metrics.")
11151119
self._clean_state()

postgres/tests/test_custom_metrics.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,35 @@ def test_only_instance_custom_queries(aggregator, pg_instance, dd_run_check, int
141141

142142
aggregator.assert_metric('custom.num', value=value, tags=custom_tags + ['query:custom'])
143143
aggregator.assert_metric('global_custom.num', value=value, tags=custom_tags + ['query:global_custom'], count=0)
144+
145+
146+
@pytest.mark.integration
147+
@pytest.mark.usefixtures('dd_environment')
148+
def test_only_custom_queries(aggregator, pg_instance, dd_run_check, integration_check):
149+
pg_instance.update(
150+
{
151+
'only_custom_queries': True,
152+
'custom_queries': [
153+
{
154+
'metric_prefix': 'custom',
155+
'query': "SELECT letter, num FROM (VALUES (97, 'a'), (98, 'b'), (99, 'c')) AS t (num,letter)",
156+
'columns': [{'name': 'customtag', 'type': 'tag'}, {'name': 'num', 'type': 'gauge'}],
157+
'tags': ['query:custom'],
158+
},
159+
],
160+
}
161+
)
162+
postgres_check = integration_check(pg_instance)
163+
dd_run_check(postgres_check)
164+
tags = _get_expected_tags(postgres_check, pg_instance, with_db=True)
165+
166+
for tag in ('a', 'b', 'c'):
167+
value = ord(tag)
168+
custom_tags = [f'customtag:{tag}']
169+
custom_tags.extend(tags)
170+
171+
aggregator.assert_metric('custom.num', value=value, tags=custom_tags + ['query:custom'])
172+
173+
running_tags = _get_expected_tags(postgres_check, pg_instance)
174+
aggregator.assert_metric('postgresql.running', count=1, value=1, tags=running_tags)
175+
aggregator.assert_all_metrics_covered()

0 commit comments

Comments
 (0)