Skip to content

Commit 8530079

Browse files
authored
[SDBM-2637] Restore agent hostname fallback resolution for SQL Server named instance configs (DataDog#23862)
* Reintroduce old agent hostname fallback * Update test * Add changelog
1 parent 5b64084 commit 8530079

3 files changed

Lines changed: 70 additions & 0 deletions

File tree

sqlserver/changelog.d/23862.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Restore agent hostname instrumentation for SQL Server named instance host configurations.

sqlserver/datadog_checks/sqlserver/sqlserver.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,9 @@ def port(self):
328328
return self.host_and_port[1]
329329

330330
def resolve_db_host(self):
331+
if "\\" in self.host:
332+
# SQL Server instance names are not resolvable, this preserves original fallback behavior prior to v7.79.0
333+
return datadog_agent.get_hostname()
331334
return agent_host_resolver(self.host)
332335

333336
@property

sqlserver/tests/test_unit.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import mock
1212
import pytest
1313

14+
from datadog_checks.base.stubs.datadog_agent import datadog_agent
1415
from datadog_checks.dev import EnvVars
1516
from datadog_checks.sqlserver import SQLServer
1617
from datadog_checks.sqlserver.connection import split_sqlserver_host_port
@@ -908,6 +909,71 @@ def test_split_sqlserver_host(instance_host, split_host, split_port):
908909
assert (s_host, s_port) == (split_host, split_port)
909910

910911

912+
AGENT_HOSTNAME = 'sql-agent-host.example.com'
913+
914+
915+
@pytest.fixture
916+
def agent_hostname_for_resolve_db_host():
917+
datadog_agent.set_hostname(AGENT_HOSTNAME)
918+
yield
919+
datadog_agent.reset_hostname()
920+
921+
922+
@pytest.mark.parametrize(
923+
'instance_host,host_part',
924+
[
925+
(r'SQL-HOST01\INSTANCE01,1601', r'SQL-HOST01\INSTANCE01'),
926+
(r'MY-SERVER\SQLEXPRESS,1433', r'MY-SERVER\SQLEXPRESS'),
927+
(r'MY-SERVER\SQLEXPRESS', r'MY-SERVER\SQLEXPRESS'),
928+
],
929+
)
930+
def test_resolve_db_host_named_instance_returns_agent_hostname(
931+
agent_hostname_for_resolve_db_host, instance_host, host_part
932+
):
933+
instance = {
934+
'host': instance_host,
935+
'username': 'datadog',
936+
'password': 'secret',
937+
}
938+
check = SQLServer(CHECK_NAME, {}, [instance])
939+
assert check.host == host_part
940+
941+
# Agent 7.79+ base resolver returns the literal host string for unresolvable names.
942+
with mock.patch(
943+
'datadog_checks.sqlserver.sqlserver.agent_host_resolver',
944+
return_value=host_part,
945+
):
946+
assert check.resolve_db_host() == AGENT_HOSTNAME
947+
assert check.resolved_hostname == AGENT_HOSTNAME
948+
assert check.database_hostname == AGENT_HOSTNAME
949+
950+
951+
@pytest.mark.parametrize(
952+
'instance_host,host_part,base_resolver_return',
953+
[
954+
('db.example.com,1433', 'db.example.com', 'resolved-db.example.com'),
955+
('192.0.2.10,1433', '192.0.2.10', '192.0.2.10'),
956+
],
957+
)
958+
def test_resolve_db_host_plain_host_delegates_to_base_resolver(
959+
agent_hostname_for_resolve_db_host, instance_host, host_part, base_resolver_return
960+
):
961+
instance = {
962+
'host': instance_host,
963+
'username': 'datadog',
964+
'password': 'secret',
965+
}
966+
check = SQLServer(CHECK_NAME, {}, [instance])
967+
assert check.host == host_part
968+
969+
with mock.patch(
970+
'datadog_checks.sqlserver.sqlserver.agent_host_resolver',
971+
return_value=base_resolver_return,
972+
) as mock_resolver:
973+
assert check.resolve_db_host() == base_resolver_return
974+
mock_resolver.assert_called_once_with(host_part)
975+
976+
911977
@pytest.mark.parametrize(
912978
"query,expected_comments,is_proc,expected_name",
913979
[

0 commit comments

Comments
 (0)