Skip to content

Commit d9011d1

Browse files
authored
[DBMON-6781] Implement agent_hostname in DatabaseCheck (#24243)
* Implement agent_hostname in DatabaseCheck * Add changelog
1 parent 428fa40 commit d9011d1

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Implement the ``agent_hostname`` property on the ``DatabaseCheck`` base class.

datadog_checks_base/datadog_checks/base/checks/db.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from abc import abstractmethod
66

7+
from datadog_checks.base.agent import datadog_agent
78
from datadog_checks.base.utils.db.utils import TagManager
89

910
from . import AgentCheck
@@ -12,6 +13,7 @@
1213
class DatabaseCheck(AgentCheck):
1314
def __init__(self, *args, **kwargs):
1415
super().__init__(*args, **kwargs)
16+
self._agent_hostname = None
1517
self.tag_manager = TagManager()
1618

1719
def database_monitoring_query_sample(self, raw_event: str):
@@ -39,6 +41,12 @@ def reported_hostname(self) -> str | None:
3941
def database_identifier(self) -> str:
4042
pass
4143

44+
@property
45+
def agent_hostname(self) -> str:
46+
if self._agent_hostname is None:
47+
self._agent_hostname = datadog_agent.get_hostname()
48+
return self._agent_hostname
49+
4250
@property
4351
def dbms(self) -> str:
4452
return self.__class__.__name__.lower()
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# (C) Datadog, Inc. 2026-present
2+
# All rights reserved
3+
# Licensed under a 3-clause BSD style license (see LICENSE)
4+
from unittest import mock
5+
6+
from datadog_checks.base.checks.db import DatabaseCheck
7+
from datadog_checks.base.stubs.datadog_agent import datadog_agent
8+
9+
10+
class FakeDatabaseCheck(DatabaseCheck):
11+
@property
12+
def reported_hostname(self):
13+
return None
14+
15+
@property
16+
def database_identifier(self):
17+
return "test-db"
18+
19+
@property
20+
def dbms_version(self):
21+
return "1.0.0"
22+
23+
@property
24+
def tags(self):
25+
return []
26+
27+
@property
28+
def cloud_metadata(self):
29+
return {}
30+
31+
32+
def test_agent_hostname_resolves_once_and_caches():
33+
check = FakeDatabaseCheck("test", {}, [{}])
34+
# The hostname comes from an FFI call, so it should only be resolved once and cached.
35+
with mock.patch.object(datadog_agent, "get_hostname", return_value="my-agent-host") as get_hostname:
36+
assert check.agent_hostname == "my-agent-host"
37+
assert check.agent_hostname == "my-agent-host"
38+
assert get_hostname.call_count == 1

0 commit comments

Comments
 (0)