Skip to content

Commit

Permalink
Revert "[DBMON-3050] Collect dynamic relation metrics for autodiscove…
Browse files Browse the repository at this point in the history
…red databases (#16076)"

This reverts commit b9f2d72.
  • Loading branch information
alexandre-normand committed Dec 13, 2023
1 parent 6ba77bb commit cd09936
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 44 deletions.
1 change: 0 additions & 1 deletion postgres/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

* Add cloudsqladmin to default list of databases to exclude from autodiscovery and databases to ignore to prevent failures on Postgres 15 on Google CloudSQL ([#16027](https://github.com/DataDog/integrations-core/pull/16027))
* Bump the minimum base check version to 34.1.0 ([#16062](https://github.com/DataDog/integrations-core/pull/16062))
* Collect Postgres size metrics for auto-discovered databases ([#16076](https://github.com/DataDog/integrations-core/pull/16076))

## 15.1.1 / 2023-10-17 / Agent 7.49.0

Expand Down
39 changes: 10 additions & 29 deletions postgres/datadog_checks/postgres/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# Licensed under Simplified BSD License (see LICENSE)
import contextlib
import copy
import functools
import os
from time import time

Expand Down Expand Up @@ -115,7 +114,7 @@ def __init__(self, name, init_config, instances):
self.check_initializations.append(self.initialize_is_aurora)
self.tags_without_db = [t for t in copy.copy(self.tags) if not t.startswith("db:")]
self.autodiscovery = self._build_autodiscovery()
self._dynamic_queries = []
self._dynamic_queries = None
# _database_instance_emitted: limit the collection and transmission of the database instance metadata
self._database_instance_emitted = TTLCache(
maxsize=1,
Expand Down Expand Up @@ -173,18 +172,18 @@ def set_resource_tags(self):
)
)

def _new_query_executor(self, queries, db):
def _new_query_executor(self, queries):
return QueryExecutor(
functools.partial(self.execute_query_raw, db=db),
self.execute_query_raw,
self,
queries=queries,
tags=self.tags_without_db,
hostname=self.resolved_hostname,
track_operation_time=True,
)

def execute_query_raw(self, query, db):
with db() as conn:
def execute_query_raw(self, query):
with self.db() as conn:
with conn.cursor() as cursor:
cursor.execute(query)
rows = cursor.fetchall()
Expand Down Expand Up @@ -232,7 +231,6 @@ def dynamic_queries(self):

self.log.debug("Generating dynamic queries")
queries = []
per_database_queries = [] # queries that need to be run per database, used for autodiscovery
if self.version >= V9_2:
q_pg_stat_database = copy.deepcopy(QUERY_PG_STAT_DATABASE)
if len(self._config.ignore_databases) > 0:
Expand Down Expand Up @@ -283,15 +281,10 @@ def dynamic_queries(self):
query = copy.copy(query)
formatted_query = self._relations_manager.filter_relation_query(query['query'], 'nspname')
query['query'] = formatted_query
per_database_queries.append(query)
queries.append(query)

if self.autodiscovery:
self._collect_dynamic_queries_autodiscovery(per_database_queries)
else:
queries.extend(per_database_queries)
self._dynamic_queries.append(self._new_query_executor(queries, db=self.db))
for dynamic_query in self._dynamic_queries:
dynamic_query.compile_queries()
self._dynamic_queries = self._new_query_executor(queries)
self._dynamic_queries.compile_queries()
self.log.debug("initialized %s dynamic querie(s)", len(queries))

return self._dynamic_queries
Expand All @@ -311,7 +304,7 @@ def cancel(self):
def _clean_state(self):
self.log.debug("Cleaning state")
self.metrics_cache.clean_state()
self._dynamic_queries = []
self._dynamic_queries = None

def _get_debug_tags(self):
return ['agent_hostname:{}'.format(self.agent_hostname)]
Expand Down Expand Up @@ -593,17 +586,6 @@ def _collect_relations_autodiscovery(self, instance_tags, relations_scopes):
),
)

def _collect_dynamic_queries_autodiscovery(self, queries):
if not self.autodiscovery:
return

databases = self.autodiscovery.get_items()
for dbname in databases:
db = functools.partial(
self.db_pool.get_connection, dbname=dbname, ttl_ms=self._config.idle_connection_timeout
)
self._dynamic_queries.append(self._new_query_executor(queries, db=db))

def _collect_stats(self, instance_tags):
"""Query pg_stat_* for various metrics
If relations is not an empty list, gather per-relation metrics
Expand Down Expand Up @@ -677,8 +659,7 @@ def _collect_stats(self, instance_tags):
self._query_scope(cursor, scope, instance_tags, True)

if self.dynamic_queries:
for dynamic_query in self.dynamic_queries:
dynamic_query.execute()
self.dynamic_queries.execute()

def _new_connection(self, dbname):
if self._config.host == 'localhost' and self._config.password == '':
Expand Down
13 changes: 0 additions & 13 deletions postgres/tests/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,6 @@
'postgresql.autoanalyzed',
}

DYNAMIC_RELATION_METRICS = {
'postgresql.relation.pages',
'postgresql.relation.tuples',
'postgresql.relation.all_visible',
'postgresql.table_size',
'postgresql.relation_size',
'postgresql.index_size',
'postgresql.toast_size',
'postgresql.total_size',
}


@pytest.mark.integration
@pytest.mark.usefixtures('dd_environment')
Expand Down Expand Up @@ -183,8 +172,6 @@ def test_autodiscovery_collect_all_relations(aggregator, integration_check, pg_i
expected_tags = _get_expected_tags(check, pg_instance, db=db, table='breed', schema='public')
for metric in RELATION_METRICS:
aggregator.assert_metric(metric, tags=expected_tags)
for metric in DYNAMIC_RELATION_METRICS:
aggregator.assert_metric(metric, tags=expected_tags)

aggregator.assert_metric(
'dd.postgres._collect_relations_autodiscovery.time',
Expand Down
2 changes: 1 addition & 1 deletion postgres/tests/test_statements.py
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ def obfuscate_sql(query, options=None):
if POSTGRES_VERSION.split('.')[0] == "9" and pg_stat_statements_view == "pg_stat_statements":
# cannot catch any queries from other users
# only can see own queries
return
return False

fqt_samples = [
s for s in samples if s.get('dbm_type') == 'fqt' and s['db']['query_signature'] == normalized_query_signature
Expand Down

0 comments on commit cd09936

Please sign in to comment.