Skip to content

Commit

Permalink
Send database instance metadata prior to check queries (#17590)
Browse files Browse the repository at this point in the history
* Send database instance metadata prior to check queries
  • Loading branch information
jmeunier28 authored May 17, 2024
1 parent aeb0eaf commit f8eff5f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 deletions postgres/changelog.d/17590.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +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.
16 changes: 10 additions & 6 deletions postgres/datadog_checks/postgres/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,7 @@ def debug_stats_kwargs(self, tags=None):
def check(self, _):
tags = copy.copy(self.tags)
self.tags_without_db = [t for t in copy.copy(self.tags) if not t.startswith("db:")]
# Collect metrics
tags_to_add = []
try:
# Check version
self._connect()
Expand All @@ -1021,17 +1021,18 @@ def check(self, _):

# Add raw version as a tag
tags.append(f'postgresql_version:{self.raw_version}')
self.tags_without_db.append(f'postgresql_version:{self.raw_version}')
tags_to_add.append(f'postgresql_version:{self.raw_version}')

# Add system identifier as a tag
self.load_system_identifier()
tags.append(f'system_identifier:{self.system_identifier}')
self.tags_without_db.append(f'system_identifier:{self.system_identifier}')

tags_to_add.append(f'system_identifier:{self.system_identifier}')
if self._config.tag_replication_role:
replication_role_tag = "replication_role:{}".format(self._get_replication_role())
tags.append(replication_role_tag)
self.tags_without_db.append(replication_role_tag)
tags_to_add.append(replication_role_tag)
self._update_tag_sets(tags_to_add)
self._send_database_instance_metadata()

self.log.debug("Running check against version %s: is_aurora: %s", str(self.version), str(self.is_aurora))
self._emit_running_metric()
Expand All @@ -1044,7 +1045,6 @@ def check(self, _):
if self._config.collect_wal_metrics:
# collect wal metrics for pg < 10, disabled by enabled
self._collect_wal_metrics()
self._send_database_instance_metadata()
except Exception as e:
self.log.exception("Unable to collect postgres metrics.")
self._clean_state()
Expand All @@ -1069,3 +1069,7 @@ def check(self, _):
finally:
# Add the warnings saved during the execution of the check
self._report_warnings()

def _update_tag_sets(self, tags):
self._non_internal_tags = list(set(self._non_internal_tags) | set(tags))
self.tags_without_db = list(set(self.tags_without_db) | set(tags))

0 comments on commit f8eff5f

Please sign in to comment.