Skip to content

Commit b98b1f0

Browse files
authored
Add optional display_name tag (#17657)
* adding collect_display_name_as_tag * linter reformat * Update description in spec.yaml * adding changelog * Adding test_display_name_tag * set default display name for unseen services
1 parent 051148d commit b98b1f0

File tree

7 files changed

+76
-1
lines changed

7 files changed

+76
-1
lines changed

windows_service/assets/configuration/spec.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,16 @@ files:
6464
value:
6565
type: boolean
6666
example: false
67+
- name: collect_display_name_as_tag
68+
description: |
69+
Whether or not to submit the Windows Service Display Name as the `display_name` tag.
70+
`display_name` will be formatted according to Datadog's tagging requirements:
71+
https://docs.datadoghq.com/getting_started/tagging/#define-tags
72+
73+
Example of the default `windows_service` tag and `display_name` tag:
74+
windows_service:wuauserv
75+
display_name:windows_update
76+
value:
77+
type: boolean
78+
example: false
6779
- template: instances/default
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add optional display_name tag

windows_service/datadog_checks/windows_service/config_models/defaults.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
# ddev -x validate models -s <INTEGRATION_NAME>
99

1010

11+
def instance_collect_display_name_as_tag():
12+
return False
13+
14+
1115
def instance_disable_generic_tags():
1216
return False
1317

windows_service/datadog_checks/windows_service/config_models/instance.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class InstanceConfig(BaseModel):
3535
arbitrary_types_allowed=True,
3636
frozen=True,
3737
)
38+
collect_display_name_as_tag: Optional[bool] = None
3839
disable_generic_tags: Optional[bool] = None
3940
disable_legacy_service_tag: Optional[bool] = None
4041
empty_default_hostname: Optional[bool] = None

windows_service/datadog_checks/windows_service/data/conf.yaml.example

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ instances:
6161
#
6262
# windows_service_startup_type_tag: false
6363

64+
## @param collect_display_name_as_tag - boolean - optional - default: false
65+
## Whether or not to submit the Windows Service Display Name as the `display_name` tag.
66+
## `display_name` will be formatted according to Datadog's tagging requirements:
67+
## https://docs.datadoghq.com/getting_started/tagging/#define-tags
68+
##
69+
## Example of the default `windows_service` tag and `display_name` tag:
70+
## windows_service:wuauserv
71+
## display_name:windows_update
72+
#
73+
# collect_display_name_as_tag: false
74+
6475
## @param tags - list of strings - optional
6576
## A list of tags to attach to every metric and service check emitted by this instance.
6677
##

windows_service/datadog_checks/windows_service/windows_service.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ def check(self, instance):
259259
# See test_name_regex_order()
260260
service_filters = sorted(service_filters, reverse=True, key=lambda x: len(x.name or ""))
261261

262-
for short_name, _, service_status in service_statuses:
262+
for short_name, display_name, service_status in service_statuses:
263263
service_view = ServiceView(scm_handle, short_name)
264264

265265
if 'ALL' not in services:
@@ -285,6 +285,9 @@ def check(self, instance):
285285
tags = ['windows_service:{}'.format(short_name)]
286286
tags.extend(custom_tags)
287287

288+
if instance.get('collect_display_name_as_tag', False):
289+
tags.append('display_name:{}'.format(display_name))
290+
288291
if instance.get('windows_service_startup_type_tag', False):
289292
try:
290293
tags.append('windows_service_startup_type:{}'.format(service_view.startup_type_string()))
@@ -306,6 +309,7 @@ def check(self, instance):
306309
# if a name doesn't match anything (wrong name or no permission to access the service), report UNKNOWN
307310
status = self.UNKNOWN
308311
startup_type_string = ServiceView.STARTUP_TYPE_UNKNOWN
312+
display_name = "Not_Found"
309313

310314
tags = ['windows_service:{}'.format(service)]
311315

@@ -314,6 +318,9 @@ def check(self, instance):
314318
if instance.get('windows_service_startup_type_tag', False):
315319
tags.append('windows_service_startup_type:{}'.format(startup_type_string))
316320

321+
if instance.get('collect_display_name_as_tag', False):
322+
tags.append('display_name:{}'.format(display_name))
323+
317324
if not instance.get('disable_legacy_service_tag', False):
318325
self._log_deprecation('service_tag', 'windows_service')
319326
tags.append('service:{}'.format(service))

windows_service/tests/test_windows_service.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,45 @@ def test_startup_type_tag(aggregator, check, instance_basic):
221221
)
222222

223223

224+
def test_display_name_tag(aggregator, check, instance_basic):
225+
instance_basic['collect_display_name_as_tag'] = True
226+
c = check(instance_basic)
227+
c.check(instance_basic)
228+
aggregator.assert_service_check(
229+
c.SERVICE_CHECK_NAME,
230+
status=c.OK,
231+
tags=[
232+
'service:EventLog',
233+
'windows_service:EventLog',
234+
'display_name:Windows Event Log',
235+
'optional:tag1',
236+
],
237+
count=1,
238+
)
239+
aggregator.assert_service_check(
240+
c.SERVICE_CHECK_NAME,
241+
status=c.OK,
242+
tags=[
243+
'service:Dnscache',
244+
'windows_service:Dnscache',
245+
'display_name:DNS Client',
246+
'optional:tag1',
247+
],
248+
count=1,
249+
)
250+
aggregator.assert_service_check(
251+
c.SERVICE_CHECK_NAME,
252+
status=c.UNKNOWN,
253+
tags=[
254+
'service:NonExistentService',
255+
'windows_service:NonExistentService',
256+
'display_name:Not_Found',
257+
'optional:tag1',
258+
],
259+
count=1,
260+
)
261+
262+
224263
def test_openservice_failure(aggregator, check, instance_basic_dict, caplog):
225264
# dict type
226265
instance_basic_dict['services'].append({'startup_type': 'automatic'})

0 commit comments

Comments
 (0)