Skip to content

Commit

Permalink
Improve log error for invalid ssl option in postgres config (#18047)
Browse files Browse the repository at this point in the history
  • Loading branch information
sethsamuel authored Jul 16, 2024
1 parent 60b7ed1 commit c7a9e29
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 6 deletions.
1 change: 1 addition & 0 deletions postgres/changelog.d/18047.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added warning when ssl option for Postgres check is invalid
5 changes: 4 additions & 1 deletion postgres/datadog_checks/postgres/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
Expand Down Expand Up @@ -75,6 +75,9 @@ 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)
Expand Down
2 changes: 1 addition & 1 deletion postgres/datadog_checks/postgres/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions postgres/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
4 changes: 2 additions & 2 deletions postgres/tests/test_metrics_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit c7a9e29

Please sign in to comment.