Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions postgres/changelog.d/23645.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Run `diagnose` when the main Postgres check fails then emit any warnings and failures as Agent Health events.
9 changes: 8 additions & 1 deletion postgres/datadog_checks/postgres/diagnose.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@
health event instead.
"""

from __future__ import annotations

from typing import TYPE_CHECKING

import psycopg

if TYPE_CHECKING:
from .postgres import PostgreSql

from .util import (
DIAGNOSTIC_METADATA,
DatabaseConfigurationError,
Expand All @@ -35,7 +42,7 @@
class PostgresDiagnose:
"""Explicit pre-flight diagnostics for `datadog-agent diagnose`."""

def __init__(self, check):
def __init__(self, check: PostgreSql):
self._check = check
# Codes that have FAIL'd in the current explicit run. Used for cascade skipping so we
# don't emit downstream-effect FAILs with nonsensical remediations (e.g. "CREATE EXTENSION"
Expand Down
19 changes: 19 additions & 0 deletions postgres/datadog_checks/postgres/health.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

from typing import TYPE_CHECKING

from datadog_checks.base.utils.diagnose import Diagnosis

if TYPE_CHECKING:
from datadog_checks.postgres import PostgreSql

Expand Down Expand Up @@ -62,3 +64,20 @@ def submit_health_event(
},
**kwargs,
)

def diagnose(self):
"""
Run the diagnostics for the Postgres check.
"""
self.check.diagnose.run()
for diagnosis in self.check.diagnosis.diagnoses:
if diagnosis.result == Diagnosis.DIAGNOSIS_FAIL:
self.submit_health_event(
name=diagnosis.name,
status=HealthStatus.WARNING
if diagnosis.result == Diagnosis.DIAGNOSIS_WARNING
else HealthStatus.ERROR,
data={
"diagnosis": diagnosis,
},
)
7 changes: 6 additions & 1 deletion postgres/datadog_checks/postgres/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ def __init__(self, name, init_config, instances):
) # type: TTLCache

# Register explicit pre-flight diagnostics for `datadog-agent diagnose`.
PostgresDiagnose(self).register()
self.diagnose = PostgresDiagnose(self)
self.diagnose.register()

def _submit_initialization_health_event(self):
try:
Expand Down Expand Up @@ -492,6 +493,7 @@ def cancel(self):
if self.data_observability._job_loop_future:
self.data_observability._job_loop_future.result()
self._close_db_pool()
self.diagnose = None

def _clean_state(self):
self.log.debug("Cleaning state")
Expand Down Expand Up @@ -1206,6 +1208,9 @@ def check(self, _):
hostname=self.reported_hostname,
raw=True,
)

self.health.diagnose()

raise e
else:
self.service_check(
Expand Down
Loading