Skip to content

Commit 40e4f36

Browse files
authored
Add database to database identifier template variables (#20301)
* Add database to database identifier template variables * Changelog * None check * Add azure_name to SQL server variables * Docs * Validate
1 parent 1359ad7 commit 40e4f36

File tree

5 files changed

+15
-1
lines changed

5 files changed

+15
-1
lines changed

sqlserver/assets/configuration/spec.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ files:
9898
- resolved_hostname: The resolved hostname of the instance, which respects the `reported_hostname` option.
9999
- host: The provided host of the instance.
100100
- port: The port number of the instance.
101+
- azure_name: The resolved hostname of the instance, which respects the `reported_hostname` option,
102+
but removes `.database.windows.net`
103+
- database: The connection database.
101104
- server_name: The resolved server name of the instance.
102105
- instance_name: The resolved instance name of the instance.
103106
In addition, you can use any key from the `tags` section of the configuration.

sqlserver/changelog.d/20301.added

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add database and Azure name to SQL Server database identifier template variables

sqlserver/datadog_checks/sqlserver/const.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
"managed_instance": "azure_sql_server_managed_instance",
3939
"virtual_machine": "azure_virtual_machine_instance",
4040
}
41+
AZURE_SERVER_SUFFIX = ".database.windows.net"
42+
4143

4244
# Metric discovery queries
4345
COUNTER_TYPE_QUERY = """select distinct cntr_type

sqlserver/datadog_checks/sqlserver/data/conf.yaml.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ instances:
100100
## - resolved_hostname: The resolved hostname of the instance, which respects the `reported_hostname` option.
101101
## - host: The provided host of the instance.
102102
## - port: The port number of the instance.
103+
## - azure_name: The resolved hostname of the instance, which respects the `reported_hostname` option,
104+
## but removes `.database.windows.net`
105+
## - database: The connection database.
103106
## - server_name: The resolved server name of the instance.
104107
## - instance_name: The resolved instance name of the instance.
105108
## In addition, you can use any key from the `tags` section of the configuration.

sqlserver/datadog_checks/sqlserver/sqlserver.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
AUTODISCOVERY_QUERY,
6868
AWS_RDS_HOSTNAME_SUFFIX,
6969
AZURE_DEPLOYMENT_TYPE_TO_RESOURCE_TYPES,
70+
AZURE_SERVER_SUFFIX,
7071
BASE_NAME_QUERY,
7172
COUNTER_TYPE_QUERY,
7273
DATABASE_SERVICE_CHECK_NAME,
@@ -330,7 +331,11 @@ def database_identifier(self):
330331
tag_dict['resolved_hostname'] = self.resolved_hostname
331332
tag_dict['host'] = str(self.host)
332333
tag_dict['port'] = str(self.port)
333-
print(self.static_info_cache)
334+
tag_dict['database'] = str(
335+
self.instance.get('database', self.connection.DEFAULT_DATABASE if self.connection else None)
336+
)
337+
if self.resolved_hostname.endswith(AZURE_SERVER_SUFFIX):
338+
tag_dict['azure_name'] = self.resolved_hostname[: -len(AZURE_SERVER_SUFFIX)]
334339
if self.static_info_cache.get(STATIC_INFO_SERVERNAME) is not None:
335340
tag_dict['server_name'] = self.static_info_cache.get(STATIC_INFO_SERVERNAME)
336341
if self.static_info_cache.get(STATIC_INFO_INSTANCENAME) is not None:

0 commit comments

Comments
 (0)