Skip to content

Commit c9fb73d

Browse files
Add database Health class for sending DBM health events (DataDog#20739)
* Add health events to DBM integrations * WIP * WIP * WIP * WIP * WIP * Cleanup * WIP * Version * Licenses * Fix * Fix * Clean * Namespace * Clean * Changelog * Clean * Lint * Remove dedicated dbm health method * Changelog * Update datadog_checks_base/changelog.d/20739.added Co-authored-by: NouemanKHAL <noueman.khalikine@datadoghq.com> --------- Co-authored-by: NouemanKHAL <noueman.khalikine@datadoghq.com>
1 parent 65c1654 commit c9fb73d

2 files changed

Lines changed: 83 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add database Health class for sending DBM health events
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# (C) Datadog, Inc. 2025-present
2+
# All rights reserved
3+
# Licensed under a 3-clause BSD style license (see LICENSE)
4+
# This is the base implementation of the Agent Health reporting system.
5+
# It provides a structure for health events and codes that can be extended by specific checks.
6+
7+
from __future__ import annotations
8+
9+
import time
10+
from typing import TYPE_CHECKING
11+
12+
from datadog_checks.base.utils.serialization import json
13+
14+
if TYPE_CHECKING:
15+
from datadog_checks.base import AgentCheck
16+
17+
try:
18+
import datadog_agent
19+
except ImportError:
20+
from datadog_checks.base.stubs import datadog_agent
21+
22+
23+
from enum import Enum
24+
25+
26+
class HealthEvent(Enum):
27+
"""
28+
Enum representing the health events.
29+
"""
30+
31+
INITIALIZATION = 'initialization'
32+
33+
34+
class HealthStatus(Enum):
35+
"""
36+
Enum representing the health statuses for a given event.
37+
"""
38+
39+
OK = 'ok'
40+
WARNING = 'warning'
41+
ERROR = 'error'
42+
43+
44+
class Health:
45+
def __init__(self, check: AgentCheck):
46+
"""
47+
Initialize the HealthCheck instance.
48+
49+
:param check: AgentCheck
50+
The check instance that will be used to submit health events.
51+
"""
52+
self.check = check
53+
54+
def submit_health_event(self, name: HealthEvent, status: HealthStatus, tags: list[str] = None, **kwargs):
55+
"""
56+
Submit a health event to the aggregator.
57+
58+
:param name: HealthEvent
59+
The name of the health event.
60+
:param status: HealthStatus
61+
The health status to submit.
62+
:param tags: list of str
63+
Tags to associate with the health event.
64+
:param kwargs: Additional keyword arguments to include in the event under `data`.
65+
"""
66+
self.check.event_platform_event(
67+
json.dumps(
68+
{
69+
'timestamp': time.time() * 1000,
70+
'version': 1,
71+
'check_id': self.check.check_id,
72+
'category': self.check.__NAMESPACE__ or self.check.__class__.__name__.lower(),
73+
'name': name,
74+
'status': status,
75+
'tags': tags or [],
76+
'ddagentversion': datadog_agent.get_version(),
77+
'ddagenthostname': datadog_agent.get_hostname(),
78+
'data': {**kwargs},
79+
}
80+
),
81+
"dbm-health",
82+
)

0 commit comments

Comments
 (0)