From def5220a1cb1c5834ff7a8269a69a0c0dd0aae8c Mon Sep 17 00:00:00 2001 From: Seth Samuel Date: Tue, 16 Jul 2024 08:59:27 -0400 Subject: [PATCH 1/4] SSL warning --- postgres/datadog_checks/postgres/config.py | 6 +++++- postgres/datadog_checks/postgres/postgres.py | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/postgres/datadog_checks/postgres/config.py b/postgres/datadog_checks/postgres/config.py index 7437033f7206a..e7cb97a44cca5 100644 --- a/postgres/datadog_checks/postgres/config.py +++ b/postgres/datadog_checks/postgres/config.py @@ -31,7 +31,7 @@ class PostgresConfig: GAUGE = AgentCheck.gauge MONOTONIC = AgentCheck.monotonic_count - def __init__(self, instance, init_config): + def __init__(self, instance, init_config, check): self.host = instance.get('host', '') if not self.host: raise ConfigurationError('Specify a Postgres host to connect to.') @@ -75,7 +75,11 @@ def __init__(self, instance, init_config): ssl = instance.get('ssl', "allow") if ssl in SSL_MODES: self.ssl_mode = ssl + else: + check.warning(f"Invalid ssl option '{ssl}', should be one of {SSL_MODES}. Defaulting to 'allow'.") + self.ssl_mode = "allow" + self.ssl_cert = instance.get('ssl_cert', None) self.ssl_root_cert = instance.get('ssl_root_cert', None) self.ssl_key = instance.get('ssl_key', None) diff --git a/postgres/datadog_checks/postgres/postgres.py b/postgres/datadog_checks/postgres/postgres.py index 081385f11793e..7a8d83316c30e 100644 --- a/postgres/datadog_checks/postgres/postgres.py +++ b/postgres/datadog_checks/postgres/postgres.py @@ -112,7 +112,7 @@ def __init__(self, name, init_config, instances): "DEPRECATION NOTICE: The managed_identity option is deprecated and will be removed in a future version." " Please use the new azure.managed_authentication option instead." ) - self._config = PostgresConfig(self.instance, self.init_config) + self._config = PostgresConfig(self.instance, self.init_config, self) self.cloud_metadata = self._config.cloud_metadata self.tags = self._config.tags # Keep a copy of the tags without the internal resource tags so they can be used for paths that don't From ec2b6e0df742658dbd6b56d844163f5575508d23 Mon Sep 17 00:00:00 2001 From: Seth Samuel Date: Tue, 16 Jul 2024 09:00:27 -0400 Subject: [PATCH 2/4] Changelog --- postgres/changelog.d/18047.added | 1 + postgres/datadog_checks/postgres/config.py | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 postgres/changelog.d/18047.added diff --git a/postgres/changelog.d/18047.added b/postgres/changelog.d/18047.added new file mode 100644 index 0000000000000..b55f8acdb82c5 --- /dev/null +++ b/postgres/changelog.d/18047.added @@ -0,0 +1 @@ +Added warning when ssl option for Postgres check is invalid diff --git a/postgres/datadog_checks/postgres/config.py b/postgres/datadog_checks/postgres/config.py index e7cb97a44cca5..276f5a7b68555 100644 --- a/postgres/datadog_checks/postgres/config.py +++ b/postgres/datadog_checks/postgres/config.py @@ -79,7 +79,6 @@ def __init__(self, instance, init_config, check): check.warning(f"Invalid ssl option '{ssl}', should be one of {SSL_MODES}. Defaulting to 'allow'.") self.ssl_mode = "allow" - self.ssl_cert = instance.get('ssl_cert', None) self.ssl_root_cert = instance.get('ssl_root_cert', None) self.ssl_key = instance.get('ssl_key', None) From d3476093ccc77be866bcb8ee72723e282880965e Mon Sep 17 00:00:00 2001 From: Seth Samuel Date: Tue, 16 Jul 2024 09:10:00 -0400 Subject: [PATCH 3/4] Default --- postgres/datadog_checks/postgres/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/postgres/datadog_checks/postgres/config.py b/postgres/datadog_checks/postgres/config.py index 276f5a7b68555..05cbbe4f6e5d6 100644 --- a/postgres/datadog_checks/postgres/config.py +++ b/postgres/datadog_checks/postgres/config.py @@ -31,7 +31,7 @@ class PostgresConfig: GAUGE = AgentCheck.gauge MONOTONIC = AgentCheck.monotonic_count - def __init__(self, instance, init_config, check): + def __init__(self, instance, init_config, check={'warning': print}): self.host = instance.get('host', '') if not self.host: raise ConfigurationError('Specify a Postgres host to connect to.') From 4fd4bcaba07c7ce0eb12a8adcc499685da830b6b Mon Sep 17 00:00:00 2001 From: Seth Samuel Date: Tue, 16 Jul 2024 09:11:46 -0400 Subject: [PATCH 4/4] Better stub --- postgres/datadog_checks/postgres/config.py | 2 +- postgres/tests/conftest.py | 4 ++-- postgres/tests/test_metrics_cache.py | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/postgres/datadog_checks/postgres/config.py b/postgres/datadog_checks/postgres/config.py index 05cbbe4f6e5d6..276f5a7b68555 100644 --- a/postgres/datadog_checks/postgres/config.py +++ b/postgres/datadog_checks/postgres/config.py @@ -31,7 +31,7 @@ class PostgresConfig: GAUGE = AgentCheck.gauge MONOTONIC = AgentCheck.monotonic_count - def __init__(self, instance, init_config, check={'warning': print}): + def __init__(self, instance, init_config, check): self.host = instance.get('host', '') if not self.host: raise ConfigurationError('Specify a Postgres host to connect to.') diff --git a/postgres/tests/conftest.py b/postgres/tests/conftest.py index 390d95cdbc54a..91ad37bdde6ab 100644 --- a/postgres/tests/conftest.py +++ b/postgres/tests/conftest.py @@ -106,13 +106,13 @@ def pg_replica_logical(): @pytest.fixture def metrics_cache(pg_instance): - config = PostgresConfig(instance=pg_instance, init_config={}) + config = PostgresConfig(instance=pg_instance, init_config={}, check={'warning': print}) return PostgresMetricsCache(config) @pytest.fixture def metrics_cache_replica(pg_replica_instance): - config = PostgresConfig(instance=pg_replica_instance, init_config={}) + config = PostgresConfig(instance=pg_replica_instance, init_config={}, check={'warning': print}) return PostgresMetricsCache(config) diff --git a/postgres/tests/test_metrics_cache.py b/postgres/tests/test_metrics_cache.py index efb1067263ecf..4507e2c6d06a8 100644 --- a/postgres/tests/test_metrics_cache.py +++ b/postgres/tests/test_metrics_cache.py @@ -28,7 +28,7 @@ ], ) def test_aurora_replication_metrics(pg_instance, version, is_aurora, expected_metrics): - config = PostgresConfig(instance=pg_instance, init_config={}) + config = PostgresConfig(instance=pg_instance, init_config={}, check={'warning': print}) cache = PostgresMetricsCache(config) replication_metrics = cache.get_replication_metrics(version, is_aurora) assert replication_metrics == expected_metrics @@ -50,7 +50,7 @@ def test_dbm_enabled_conn_metric(pg_instance, version, is_dbm_enabled, expected_ pg_instance['dbm'] = is_dbm_enabled pg_instance['collect_resources'] = {'enabled': False} pg_instance['collect_database_size_metrics'] = False - config = PostgresConfig(instance=pg_instance, init_config={}) + config = PostgresConfig(instance=pg_instance, init_config={}, check={'warning': print}) cache = PostgresMetricsCache(config) instance_metrics = cache.get_instance_metrics(version) assert instance_metrics['metrics'] == expected_metrics