Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions postgres/changelog.d/24923.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Respect dbstrict in schema collection
28 changes: 16 additions & 12 deletions postgres/datadog_checks/postgres/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,18 +208,22 @@ def _get_databases(self):
query = DATABASE_INFORMATION_QUERY
params: list[str] = []

query += regex_exclude_clauses("datname", self._config.exclude_databases)
params.extend(self._config.exclude_databases)

query += regex_include_clause("datname", self._config.include_databases)
params.extend(self._config.include_databases)

# Autodiscovery trumps exclude and include
autodiscovery_databases = self._check.autodiscovery.get_items() if self._check.autodiscovery else []
if autodiscovery_databases:
in_clause = ", ".join(["%s"] * len(autodiscovery_databases))
query += f" AND datname IN ({in_clause})"
params.extend(autodiscovery_databases)
if self._check._config.dbstrict and not self._check.autodiscovery:
query += " AND datname = %s"
params.append(self._check._config.dbname)
else:
Comment on lines +211 to +214

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve schema database filters under dbstrict

When dbstrict is enabled without autodiscovery, this branch bypasses both collect_schemas.include_databases and exclude_databases. Consequently, a configured database is now collected even when it matches an explicit exclusion or fails to match the inclusion list; these filters applied before this commit and are documented as controlling which databases are included. Apply the dbname restriction in addition to the existing filters rather than replacing them.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems fine to me, dbstrict should supersede include/exclude databases imo

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With dbstrict we're only collecting from one database, so if it was exlcluded collection is effectively disabled

query += regex_exclude_clauses("datname", self._config.exclude_databases)
params.extend(self._config.exclude_databases)

query += regex_include_clause("datname", self._config.include_databases)
params.extend(self._config.include_databases)

# Autodiscovery trumps exclude and include
autodiscovery_databases = self._check.autodiscovery.get_items() if self._check.autodiscovery else []
if autodiscovery_databases:
in_clause = ", ".join(["%s"] * len(autodiscovery_databases))
query += f" AND datname IN ({in_clause})"
params.extend(autodiscovery_databases)

with self._check._get_main_db() as conn:
with conn.cursor(row_factory=dict_row) as cursor:
Expand Down
24 changes: 24 additions & 0 deletions postgres/tests/test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)

from unittest import mock

import pytest

from datadog_checks.postgres.schemas import PostgresSchemaCollector
Expand Down Expand Up @@ -82,6 +84,28 @@ def test_databases_include_and_exclude_filter(dbm_instance, integration_check):
assert 'datadog_test' not in database_names


def test_databases_dbstrict(dbm_instance, integration_check):
dbm_instance['dbstrict'] = True
check = integration_check(dbm_instance)
collector = PostgresSchemaCollector(check)

databases = collector._get_databases()
database_names = [database['name'] for database in databases]
assert database_names == [dbm_instance['dbname']]


def test_databases_dbstrict_with_autodiscovery(dbm_instance, integration_check):
dbm_instance['dbstrict'] = True
dbm_instance['database_autodiscovery'] = {'enabled': True, 'include': ['^dogs_[0-9]$']}
check = integration_check(dbm_instance)
collector = PostgresSchemaCollector(check)

with mock.patch.object(check.autodiscovery, 'get_items', return_value=['dogs_3']):
databases = collector._get_databases()
database_names = [database['name'] for database in databases]
assert database_names == ['dogs_3']


def test_get_cursor(dbm_instance, integration_check):
check = integration_check(dbm_instance)
check.version = POSTGRES_VERSION
Expand Down
Loading