Skip to content

Commit 3d6e5e7

Browse files
committed
Add support as template variable to sqlserver template config key
1 parent 824902a commit 3d6e5e7

File tree

4 files changed

+38
-27
lines changed

4 files changed

+38
-27
lines changed

sqlserver/datadog_checks/sqlserver/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
# Keys of the static info cache, used to cache server info which does not change
2525
STATIC_INFO_SERVERNAME = 'servername'
2626
STATIC_INFO_INSTANCENAME = 'instancename'
27+
STATIC_INFO_FULL_SERVERNAME = 'full_servername'
2728
STATIC_INFO_VERSION = 'version'
2829
STATIC_INFO_MAJOR_VERSION = 'major_version'
2930
STATIC_INFO_ENGINE_EDITION = 'engine_edition'

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

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ instances:
8585
#
8686
# exclude_hostname: false
8787

88-
## Controls how the database is identified. The default value is the resolved hostname for the instance,
88+
## Controls how the database is identified. The default value is the resolved hostname for the instance,
8989
## which respects the `reported_hostname` option.
9090
##
9191
## This value will be used as-is for the display name of the instance but will be normalized
@@ -105,9 +105,11 @@ instances:
105105
## - database: The connection database.
106106
## - server_name: The resolved server name of the instance.
107107
## - instance_name: The resolved instance name of the instance.
108+
## - full_server_name: The resolved full server name of the instance, which is the combination of
109+
## the server name and instance name, or more specifically, the exact value returned by `@@SERVERNAME`.
108110
## In addition, you can use any key from the `tags` section of the configuration.
109111
#
110-
# template: $env-$resolved_hostname:$port
112+
# template: $full_server_name
111113

112114
## @param database_autodiscovery - boolean - optional - default: false
113115
## Auto-discover and monitor databases. Supported for the metrics check.
@@ -165,9 +167,9 @@ instances:
165167
##
166168
## Set `ao_metrics.enabled` to true to enable collection of AlwaysOn metrics. Defaults to false.
167169
##
168-
## When `ao_metrics.enabled` is True, use `ao_metrics.availability_group` to specify the
169-
## resource group id of a specific availability group that you would like to monitor.
170-
## If no availability group is specified, then we will collect AlwaysOn metrics for all
170+
## When `ao_metrics.enabled` is True, use `ao_metrics.availability_group` to specify the
171+
## resource group id of a specific availability group that you would like to monitor.
172+
## If no availability group is specified, then we will collect AlwaysOn metrics for all
171173
## availability groups on the current replica.
172174
##
173175
## Primary replicas may emit metrics for remote secondary replicas
@@ -179,11 +181,11 @@ instances:
179181
# ao_metrics: {}
180182

181183
## @param db_fragmentation_metrics - mapping - optional
182-
## Configure collection of database fragmentation metrics.
183-
## Note these queries can be resource intensive on large datasets. Recommend to limit these via
184+
## Configure collection of database fragmentation metrics.
185+
## Note these queries can be resource intensive on large datasets. Recommend to limit these via
184186
## autodiscovery or specific database instances.
185187
##
186-
## Set `db_fragmentation_metrics.enabled` to true to enable collection of
188+
## Set `db_fragmentation_metrics.enabled` to true to enable collection of
187189
## database index fragmentation statistics. Defaults to false.
188190
##
189191
## Use `db_fragmentation_metrics.enabled_tempdb` to enable collection of database index fragmentation statistics
@@ -247,7 +249,7 @@ instances:
247249
# master_files_metrics: {}
248250

249251
## @param primary_log_shipping_metrics - mapping - optional
250-
## Configure collection of metrics for a log shipping setup. Required to run against the
252+
## Configure collection of metrics for a log shipping setup. Required to run against the
251253
## primary instance in a transaction log shipping configuration. Note that
252254
## the Datadog user needs to be present in msdb and must be added to the db_datareader role.
253255
##
@@ -257,7 +259,7 @@ instances:
257259
# primary_log_shipping_metrics: {}
258260

259261
## @param secondary_log_shipping_metrics - mapping - optional
260-
## Configure collection of metrics for a log shipping setup. Required to run against the
262+
## Configure collection of metrics for a log shipping setup. Required to run against the
261263
## secondary instance in a transaction log shipping configuration. Note that
262264
## the Datadog user needs to be present in msdb and must be added to the db_datareader role.
263265
##
@@ -277,13 +279,13 @@ instances:
277279
## @param tempdb_file_space_usage_metrics - mapping - optional
278280
## Configure collection of tempdb file space usage metrics for how space is used in tempdb data files.
279281
##
280-
## Set `tempdb_file_space_usage_metrics.enabled` to true to enable collection of
282+
## Set `tempdb_file_space_usage_metrics.enabled` to true to enable collection of
281283
## tempdb file space usage metrics. Defaults to true.
282284
#
283285
# tempdb_file_space_usage_metrics: {}
284286

285287
## @param xe_metrics - mapping - optional
286-
## Configure collection of extended events (XE) metrics.
288+
## Configure collection of extended events (XE) metrics.
287289
##
288290
## Set `xe_metrics.enabled` to true to enable collection of extended events metrics. Defaults to false.
289291
#
@@ -647,12 +649,12 @@ instances:
647649
# keep_identifier_quotation: false
648650

649651
## Configure the collection of raw query statements in query activity, execution plans, and XE events.
650-
## To collect raw query statements from XE events, set `xe_collection.query_completions.enabled` and
652+
## To collect raw query statements from XE events, set `xe_collection.query_completions.enabled` and
651653
## `xe_collection.query_errors.enabled` to `true`.
652654
## Raw query statements and execution plans may contain sensitive information (e.g., passwords)
653655
## or personally identifiable information in query text.
654-
## Enabling this option will allow the collection and ingestion of raw query statements and
655-
## execution plans into Datadog, which can then become viewable in query samples or explain plans.
656+
## Enabling this option will allow the collection and ingestion of raw query statements and
657+
## execution plans into Datadog, which can then become viewable in query samples or explain plans.
656658
## This option is disabled by default.
657659
## Note: Collection of raw query statements is currently in preview.
658660
## If you are interested in participating, please reach out to your Datadog Customer Success Manager.
@@ -703,7 +705,7 @@ instances:
703705
## of the item in this column:
704706
## i. tag - This is the default tag type
705707
## ii. tag_list - This allows multiple values to be attached
706-
## to the tag name. For example:
708+
## to the tag name. For example:
707709
##
708710
## query = {
709711
## "name": "example",
@@ -730,9 +732,9 @@ instances:
730732
## 3. tags (optional) - A list of tags to apply to each metric.
731733
## 4. collection_interval (optional) - The frequency at which to collect the metrics.
732734
## If collection_interval is not set, the query will be run every check run.
733-
## If the collection interval is less than check collection interval,
735+
## If the collection interval is less than check collection interval,
734736
## the query will be run every check run.
735-
## If the collection interval is greater than check collection interval,
737+
## If the collection interval is greater than check collection interval,
736738
## the query will NOT BE RUN exactly at the collection interval.
737739
## The query will be run at the next check run after the collection interval has passed.
738740
## 5. metric_prefix (optional) - The prefix to apply to each metric.
@@ -813,12 +815,12 @@ instances:
813815
##
814816
## Set `query_completions.enabled` to `true` to enable the collection of query completion events.
815817
##
816-
## Use `query_completions.collection_interval` to set the interval (in seconds) for the collection of
818+
## Use `query_completions.collection_interval` to set the interval (in seconds) for the collection of
817819
## query completion events. Defaults to 10 seconds. If you intend on updating this value,
818820
## it is strongly recommended to use a consistent value throughout all SQL Server agent deployments.
819821
##
820-
## Use `query_completions.max_events` to set the maximum number of query completion events to process
821-
## per collection. Note that SQL Server's ring buffer has a maximum of 1000 events per query,
822+
## Use `query_completions.max_events` to set the maximum number of query completion events to process
823+
## per collection. Note that SQL Server's ring buffer has a maximum of 1000 events per query,
822824
## so values above 1000 will still be capped at 1000 by the database engine. Defaults to 1000.
823825
#
824826
# query_completions: {}
@@ -828,12 +830,12 @@ instances:
828830
##
829831
## Set `query_errors.enabled` to `true` to enable the collection of query error events.
830832
##
831-
## Use `query_errors.collection_interval` to set the interval (in seconds) for the collection of
833+
## Use `query_errors.collection_interval` to set the interval (in seconds) for the collection of
832834
## query error events. Defaults to 10 seconds. If you intend on updating this value,
833835
## it is strongly recommended to use a consistent value throughout all SQL Server agent deployments.
834836
##
835-
## Use `query_errors.max_events` to set the maximum number of query error events to process
836-
## per collection. Note that SQL Server's ring buffer has a maximum of 1000 events per query,
837+
## Use `query_errors.max_events` to set the maximum number of query error events to process
838+
## per collection. Note that SQL Server's ring buffer has a maximum of 1000 events per query,
837839
## so values above 1000 will still be capped at 1000 by the database engine. Defaults to 1000.
838840
#
839841
# query_errors: {}

sqlserver/datadog_checks/sqlserver/sqlserver.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
PERF_RAW_LARGE_FRACTION,
8484
SERVICE_CHECK_NAME,
8585
STATIC_INFO_ENGINE_EDITION,
86+
STATIC_INFO_FULL_SERVERNAME,
8687
STATIC_INFO_INSTANCENAME,
8788
STATIC_INFO_MAJOR_VERSION,
8889
STATIC_INFO_RDS,
@@ -338,8 +339,12 @@ def database_identifier(self):
338339
tag_dict['azure_name'] = self.resolved_hostname[: -len(AZURE_SERVER_SUFFIX)]
339340
if self.static_info_cache.get(STATIC_INFO_SERVERNAME) is not None:
340341
tag_dict['server_name'] = self.static_info_cache.get(STATIC_INFO_SERVERNAME)
341-
if self.static_info_cache.get(STATIC_INFO_INSTANCENAME) is not None:
342-
tag_dict['instance_name'] = self.static_info_cache.get(STATIC_INFO_INSTANCENAME)
342+
# in many cases the instance name is not set, so we use an empty string as a fallback in the
343+
# cases where we already have a server name. This usually means that the instance is not a named instance
344+
# and the server name is the same as the instance name.
345+
tag_dict['instance_name'] = self.static_info_cache.get(STATIC_INFO_INSTANCENAME) or ''
346+
if self.static_info_cache.get(STATIC_INFO_FULL_SERVERNAME) is not None:
347+
tag_dict['full_server_name'] = self.static_info_cache.get(STATIC_INFO_FULL_SERVERNAME)
343348
self._database_identifier = template.safe_substitute(**tag_dict)
344349
return self._database_identifier
345350

@@ -394,6 +399,7 @@ def load_static_information(self):
394399
)
395400
self.static_info_cache[STATIC_INFO_SERVERNAME] = servername
396401
self.static_info_cache[STATIC_INFO_INSTANCENAME] = instancename
402+
self.static_info_cache[STATIC_INFO_FULL_SERVERNAME] = full_servername
397403

398404
self.tags.append("sqlserver_servername:{}".format(servername))
399405
self.non_internal_tags.append("sqlserver_servername:{}".format(servername))

sqlserver/tests/test_unit.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from datadog_checks.dev import EnvVars
1515
from datadog_checks.sqlserver import SQLServer
1616
from datadog_checks.sqlserver.connection import split_sqlserver_host_port
17-
from datadog_checks.sqlserver.const import STATIC_INFO_INSTANCENAME, STATIC_INFO_SERVERNAME
17+
from datadog_checks.sqlserver.const import STATIC_INFO_FULL_SERVERNAME, STATIC_INFO_INSTANCENAME, STATIC_INFO_SERVERNAME
1818
from datadog_checks.sqlserver.metrics import SqlFractionMetric
1919
from datadog_checks.sqlserver.schemas import Schemas, SubmitData
2020
from datadog_checks.sqlserver.sqlserver import SQLConnectionError
@@ -896,6 +896,7 @@ def test_get_unixodbc_sysconfig():
896896
('$env-$resolved_hostname', '$env-stubbed.hostname', []),
897897
('$env-$resolved_hostname', 'prod,staging-stubbed.hostname', ['env:prod', 'env:staging']),
898898
('$env-$server_name/$instance_name', 'prod,staging-server/instance', ['env:prod', 'env:staging']),
899+
('$full_server_name', 'prod-server\\instance', ['env:prod']),
899900
],
900901
)
901902
def test_database_identifier(instance_docker, template, expected, tags):
@@ -908,6 +909,7 @@ def test_database_identifier(instance_docker, template, expected, tags):
908909
check = SQLServer(CHECK_NAME, {}, [instance_docker])
909910
check.static_info_cache[STATIC_INFO_SERVERNAME] = 'server'
910911
check.static_info_cache[STATIC_INFO_INSTANCENAME] = 'instance'
912+
check.static_info_cache[STATIC_INFO_FULL_SERVERNAME] = 'server\\instance'
911913
# Reset for recalculation with static info
912914
check._database_identifier = None
913915

0 commit comments

Comments
 (0)