Skip to content

Commit 6d24300

Browse files
authored
[DBMON-6256] Fix postgres RDS host resolution (DataDog#22744)
* Fix detecting RDS host resolution * Add changelog * Move auto resolution to config builder * Move to apply_cloud_defaults
1 parent af57177 commit 6d24300

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

postgres/changelog.d/22744.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed a bug where `cloud_metadata.aws.instance_endpoint` was not populated for auto-detected RDS hosts, which could prevent query metrics from being properly associated with the database instance in the UI

postgres/datadog_checks/postgres/config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from datadog_checks.base.utils.aws import rds_parse_tags_from_endpoint
3030
from datadog_checks.base.utils.db.utils import get_agent_host_tags
3131
from datadog_checks.postgres.features import Feature, FeatureKey, FeatureNames
32+
from datadog_checks.postgres.util import AWS_RDS_HOSTNAME_SUFFIX
3233

3334
SSL_MODES = {'disable', 'allow', 'prefer', 'require', 'verify-ca', 'verify-full'}
3435
TABLE_COUNT_LIMIT = 200
@@ -316,6 +317,10 @@ def apply_validated_defaults(args: dict, instance: dict, validation_result: Vali
316317

317318

318319
def apply_cloud_defaults(args: dict, instance: dict, validation_result: ValidationResult):
320+
# Auto-detect RDS endpoints and backfill instance_endpoint when not explicitly configured
321+
if not args['aws'].get('instance_endpoint') and AWS_RDS_HOSTNAME_SUFFIX in args['host']:
322+
args['aws']['instance_endpoint'] = args['host']
323+
319324
# AWS backfill and validation
320325
if (
321326
not instance.get("aws", {}).get("managed_authentication", None)

postgres/tests/test_config.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,40 @@ def test_cloud_validations(mock_check, minimal_instance):
368368
assert config.azure.managed_authentication.enabled
369369

370370

371+
@pytest.mark.parametrize(
372+
'rds_host, expected_rds_tag, expected_instance_endpoint',
373+
[
374+
(
375+
'my-cluster.cluster-cfxdfe8cpixl.us-east-1.rds.amazonaws.com',
376+
'dbclusteridentifier:my-cluster',
377+
'my-cluster.cluster-cfxdfe8cpixl.us-east-1.rds.amazonaws.com',
378+
),
379+
(
380+
'my-instance.cfxdfe8cpixl.us-east-1.rds.amazonaws.com',
381+
'dbinstanceidentifier:my-instance',
382+
'my-instance.cfxdfe8cpixl.us-east-1.rds.amazonaws.com',
383+
),
384+
],
385+
ids=['cluster_endpoint', 'instance_endpoint'],
386+
)
387+
def test_rds_auto_detected_cloud_metadata(
388+
mock_check, minimal_instance, rds_host, expected_rds_tag, expected_instance_endpoint
389+
):
390+
"""
391+
When a user sets host to an RDS endpoint without explicitly setting
392+
aws config, build_config should auto-detect the RDS host and populate
393+
aws.instance_endpoint so the backend can properly associate query
394+
metrics with the RDS resource.
395+
"""
396+
minimal_instance['host'] = rds_host
397+
mock_check.instance = minimal_instance
398+
mock_check.init_config = {}
399+
config, result = build_config(check=mock_check)
400+
assert result.valid
401+
assert config.aws.instance_endpoint == expected_instance_endpoint
402+
assert expected_rds_tag in config.tags
403+
404+
371405
def test_relations_validation_fails_if_no_relname_or_regex():
372406
with pytest.raises(ConfigurationError):
373407
RelationsManager.validate_relations_config([{"relkind": ["i"]}])

0 commit comments

Comments
 (0)