Skip to content

Commit 4ba1bef

Browse files
authored
Add health events to Postgres (DataDog#21193)
* Add Postgres Health class for tracking Postgres health events * Changelog * Fix changelog number
1 parent 17e34f8 commit 4ba1bef

3 files changed

Lines changed: 63 additions & 0 deletions

File tree

postgres/changelog.d/21193.added

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add Postgres Health class for submitting Postgres health events
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# (C) Datadog, Inc. 2025-present
2+
# All rights reserved
3+
# Licensed under a 3-clause BSD style license (see LICENSE)
4+
from __future__ import annotations
5+
6+
from typing import TYPE_CHECKING
7+
8+
if TYPE_CHECKING:
9+
from datadog_checks.postgres import PostgreSql
10+
11+
from enum import Enum
12+
13+
from datadog_checks.base.utils.db.health import Health, HealthEvent, HealthStatus
14+
15+
16+
class PostgresHealthEvent(Enum):
17+
"""
18+
Enum representing the health events for PostgreSQL monitoring.
19+
"""
20+
21+
EXPLAIN_PLAN_ERROR = 'explain_plan_error'
22+
23+
24+
class PostgresHealth(Health):
25+
def __init__(self, check: PostgreSql):
26+
# type: (PostgreSql) -> None
27+
"""
28+
Initialize the PostgresHealth instance.
29+
30+
:param check: PostgreSql
31+
The check instance that will be used to submit health events.
32+
"""
33+
super().__init__(check)
34+
self.check = check
35+
36+
def submit_health_event(
37+
self,
38+
name: HealthEvent | PostgresHealthEvent,
39+
status: HealthStatus,
40+
**kwargs,
41+
):
42+
"""
43+
Submit a health event to the aggregator.
44+
45+
:param name: PostgresHealthEvent
46+
The name of the health event.
47+
:param status: HealthStatus
48+
The health status to submit.
49+
:param kwargs: Additional keyword arguments to include in the event.
50+
"""
51+
super().submit_health_event(
52+
name,
53+
status,
54+
# If we have an error parsing the config we may not have tags yet
55+
self.check.tags if hasattr(self.check, 'tags') else [],
56+
database_identifier=self.check.database_identifier,
57+
**kwargs,
58+
)

postgres/datadog_checks/postgres/postgres.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from datadog_checks.postgres import aws, azure
2424
from datadog_checks.postgres.connection_pool import LRUConnectionPoolManager, PostgresConnectionArgs
2525
from datadog_checks.postgres.discovery import PostgresAutodiscovery
26+
from datadog_checks.postgres.health import PostgresHealth
2627
from datadog_checks.postgres.metadata import PostgresMetadata
2728
from datadog_checks.postgres.metrics_cache import PostgresMetricsCache
2829
from datadog_checks.postgres.relationsmanager import (
@@ -102,6 +103,7 @@ class PostgreSql(AgentCheck):
102103

103104
def __init__(self, name, init_config, instances):
104105
super(PostgreSql, self).__init__(name, init_config, instances)
106+
self.health = PostgresHealth(self)
105107
self._resolved_hostname = None
106108
self._database_identifier = None
107109
self._agent_hostname = None
@@ -125,10 +127,12 @@ def __init__(self, name, init_config, instances):
125127
"DEPRECATION NOTICE: The managed_identity option is deprecated and will be removed in a future version."
126128
" Please use the new azure.managed_authentication option instead."
127129
)
130+
128131
self._config = PostgresConfig(self.instance, self.init_config, self)
129132
self.cloud_metadata = self._config.cloud_metadata
130133
self.tags = self._config.tags
131134
self.add_core_tags()
135+
132136
# Keep a copy of the tags without the internal resource tags so they can be used for paths that don't
133137
# go through the agent internal metrics submission processing those tags
134138
self._non_internal_tags = copy.deepcopy(self.tags)

0 commit comments

Comments
 (0)