Skip to content

Commit f8eff5f

Browse files
authored
Send database instance metadata prior to check queries (#17590)
* Send database instance metadata prior to check queries
1 parent aeb0eaf commit f8eff5f

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

postgres/changelog.d/17590.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Send database instance metadata prior to check queries. This prevents a scenario where exceptions thrown during check execution can cause the database_instance resource to not be emitted. When the resource is not emitted, this can cause flapping in host tags. For example, a customer might see dbms:N/A for a period of time until a new database_instance resource is created. Moving this means it will always be sent, even if an unexepected exception is thrown during check execution.

postgres/datadog_checks/postgres/postgres.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ def debug_stats_kwargs(self, tags=None):
10121012
def check(self, _):
10131013
tags = copy.copy(self.tags)
10141014
self.tags_without_db = [t for t in copy.copy(self.tags) if not t.startswith("db:")]
1015-
# Collect metrics
1015+
tags_to_add = []
10161016
try:
10171017
# Check version
10181018
self._connect()
@@ -1021,17 +1021,18 @@ def check(self, _):
10211021

10221022
# Add raw version as a tag
10231023
tags.append(f'postgresql_version:{self.raw_version}')
1024-
self.tags_without_db.append(f'postgresql_version:{self.raw_version}')
1024+
tags_to_add.append(f'postgresql_version:{self.raw_version}')
10251025

10261026
# Add system identifier as a tag
10271027
self.load_system_identifier()
10281028
tags.append(f'system_identifier:{self.system_identifier}')
1029-
self.tags_without_db.append(f'system_identifier:{self.system_identifier}')
1030-
1029+
tags_to_add.append(f'system_identifier:{self.system_identifier}')
10311030
if self._config.tag_replication_role:
10321031
replication_role_tag = "replication_role:{}".format(self._get_replication_role())
10331032
tags.append(replication_role_tag)
1034-
self.tags_without_db.append(replication_role_tag)
1033+
tags_to_add.append(replication_role_tag)
1034+
self._update_tag_sets(tags_to_add)
1035+
self._send_database_instance_metadata()
10351036

10361037
self.log.debug("Running check against version %s: is_aurora: %s", str(self.version), str(self.is_aurora))
10371038
self._emit_running_metric()
@@ -1044,7 +1045,6 @@ def check(self, _):
10441045
if self._config.collect_wal_metrics:
10451046
# collect wal metrics for pg < 10, disabled by enabled
10461047
self._collect_wal_metrics()
1047-
self._send_database_instance_metadata()
10481048
except Exception as e:
10491049
self.log.exception("Unable to collect postgres metrics.")
10501050
self._clean_state()
@@ -1069,3 +1069,7 @@ def check(self, _):
10691069
finally:
10701070
# Add the warnings saved during the execution of the check
10711071
self._report_warnings()
1072+
1073+
def _update_tag_sets(self, tags):
1074+
self._non_internal_tags = list(set(self._non_internal_tags) | set(tags))
1075+
self.tags_without_db = list(set(self.tags_without_db) | set(tags))

0 commit comments

Comments
 (0)