Skip to content

Commit 3f6a86a

Browse files
[DBMON-6789] Implement database_identifier hooks in sqlserver (#24279)
* [DBMON-6789] Implement database_identifier hooks in sqlserver Co-authored-by: Cursor <cursoragent@cursor.com> * Add changelog Co-authored-by: Cursor <cursoragent@cursor.com> --------- Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent eaed9b9 commit 3f6a86a

2 files changed

Lines changed: 23 additions & 34 deletions

File tree

sqlserver/changelog.d/24279.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove duplicated database_identifier logic now provided by the DatabaseCheck base class.

sqlserver/datadog_checks/sqlserver/sqlserver.py

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import functools
88
import time
99
from collections import defaultdict
10-
from string import Template
1110

1211
from cachetools import TTLCache
1312

@@ -147,7 +146,6 @@ def __init__(self, name, init_config, instances):
147146

148147
self._resolved_hostname = None
149148
self._database_hostname = None
150-
self._database_identifier = None
151149
self._connection = None
152150
self.failed_connections = {}
153151
self.instance_metrics = []
@@ -358,38 +356,28 @@ def resolved_hostname(self):
358356
return self._resolved_hostname
359357

360358
@property
361-
def database_identifier(self):
362-
# type: () -> str
363-
if self._database_identifier is None:
364-
template = Template(self._config.database_identifier.get('template') or '$resolved_hostname')
365-
# Copy self.tag_manager._tags and map values to single values instead of lists
366-
tag_dict = {}
367-
tags = self.tag_manager.get_tags()
368-
# sort tags to ensure consistent ordering
369-
tags.sort()
370-
for t in tags:
371-
if ':' in t:
372-
key, value = t.split(':', 1)
373-
if key in tag_dict:
374-
tag_dict[key] += f",{value}"
375-
else:
376-
tag_dict[key] = value
377-
tag_dict['resolved_hostname'] = self.resolved_hostname
378-
tag_dict['host'] = str(self.host)
379-
tag_dict['port'] = str(self.port) if self.port is not None else None
380-
database = self.instance.get('database', self.connection.DEFAULT_DATABASE if self.connection else None)
381-
if database is not None:
382-
tag_dict['database'] = database
383-
if self.resolved_hostname.endswith(AZURE_SERVER_SUFFIX):
384-
tag_dict['azure_name'] = self.resolved_hostname[: -len(AZURE_SERVER_SUFFIX)]
385-
if self.static_info_cache.get(STATIC_INFO_SERVERNAME) is not None:
386-
tag_dict['server_name'] = self.static_info_cache.get(STATIC_INFO_SERVERNAME)
387-
if self.static_info_cache.get(STATIC_INFO_INSTANCENAME) is not None:
388-
tag_dict['instance_name'] = self.static_info_cache.get(STATIC_INFO_INSTANCENAME)
389-
if self.static_info_cache.get(STATIC_INFO_FULL_SERVERNAME) is not None:
390-
tag_dict['full_server_name'] = self.static_info_cache.get(STATIC_INFO_FULL_SERVERNAME)
391-
self._database_identifier = template.safe_substitute(**tag_dict)
392-
return self._database_identifier
359+
def database_identifier_template(self) -> str:
360+
return self._config.database_identifier.get('template') or '$resolved_hostname'
361+
362+
@property
363+
def database_identifier_params(self) -> dict:
364+
params = {
365+
'resolved_hostname': self.resolved_hostname,
366+
'host': str(self.host),
367+
'port': str(self.port) if self.port is not None else None,
368+
}
369+
database = self.instance.get('database', self.connection.DEFAULT_DATABASE if self.connection else None)
370+
if database is not None:
371+
params['database'] = database
372+
if self.resolved_hostname.endswith(AZURE_SERVER_SUFFIX):
373+
params['azure_name'] = self.resolved_hostname[: -len(AZURE_SERVER_SUFFIX)]
374+
if self.static_info_cache.get(STATIC_INFO_SERVERNAME) is not None:
375+
params['server_name'] = self.static_info_cache.get(STATIC_INFO_SERVERNAME)
376+
if self.static_info_cache.get(STATIC_INFO_INSTANCENAME) is not None:
377+
params['instance_name'] = self.static_info_cache.get(STATIC_INFO_INSTANCENAME)
378+
if self.static_info_cache.get(STATIC_INFO_FULL_SERVERNAME) is not None:
379+
params['full_server_name'] = self.static_info_cache.get(STATIC_INFO_FULL_SERVERNAME)
380+
return params
393381

394382
@property
395383
def database_hostname(self):

0 commit comments

Comments
 (0)