Skip to content

Commit 3680191

Browse files
committed
updating tag_by alias
1 parent 70c0285 commit 3680191

File tree

9 files changed

+54
-37
lines changed

9 files changed

+54
-37
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# (C) Datadog, Inc. 2018-present
22
# All rights reserved
33
# Licensed under a 3-clause BSD style license (see LICENSE)
4-
__version__ = "37.22.1"
4+
__version__ = "37.22.0"

datadog_checks_base/datadog_checks/base/checks/win/wmi/base.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ def _get_tag_query_tag(self, sampler, wmi_obj, tag_query):
147147
self.log.debug("Extracted `tag_queries` tag: '%s'", tag)
148148
return tag
149149

150-
def _extract_metrics(self, wmi_sampler, tag_by, tag_queries, constant_tags, tag_by_prefix):
151-
# type: (WMISampler, str, List[List[str]], List[str], str) -> List[WMIMetric]
150+
def _extract_metrics(self, wmi_sampler, tag_by, tag_queries, constant_tags):
151+
# type: (WMISampler, str, List[List[str]], List[str]) -> List[WMIMetric]
152152
"""
153153
Extract and tag metrics from the WMISampler.
154154
@@ -174,8 +174,6 @@ def _extract_metrics(self, wmi_sampler, tag_by, tag_queries, constant_tags, tag_
174174

175175
extracted_metrics = []
176176
tag_by = tag_by.lower()
177-
if tag_by_prefix:
178-
tag_by_prefix = tag_by_prefix.lower()
179177

180178
for wmi_obj in wmi_sampler:
181179
tags = list(constant_tags) if constant_tags else []
@@ -205,13 +203,21 @@ def _extract_metrics(self, wmi_sampler, tag_by, tag_queries, constant_tags, tag_
205203
# Tag with `tag_by` parameter
206204
for t in tag_by.split(','):
207205
t = t.strip()
206+
if ' as ' in t:
207+
t_split = t.split(' as ')
208+
t = t_split[0].strip()
209+
alias = t_split[1].strip()
210+
else:
211+
alias = None
208212
if normalized_wmi_property == t:
209213
tag_value = str(wmi_value).lower()
210214
if tag_queries and tag_value.find("#") > 0:
211215
tag_value = tag_value[: tag_value.find("#")]
212-
if tag_by_prefix:
213-
t = "{prefix}_{name}".format(prefix=tag_by_prefix, name=t)
214-
tags.append("{name}:{value}".format(name=t, value=tag_value))
216+
217+
if alias:
218+
tags.append("{name}:{value}".format(name=alias, value=tag_value))
219+
else:
220+
tags.append("{name}:{value}".format(name=t, value=tag_value))
215221
continue
216222

217223
# No metric extraction on 'Name' and properties in tag_by
@@ -272,6 +278,7 @@ def _get_instance_key(self, host, namespace, wmi_class, other=None):
272278
def get_running_wmi_sampler(self, properties, filters, **kwargs):
273279
# type: (List[str], List[Dict[str, WMIFilter]], **Any) -> WMISampler
274280
tag_by = kwargs.pop('tag_by', "")
281+
275282
return self._get_running_wmi_sampler(
276283
instance_key=None,
277284
wmi_class=self.wmi_class,

wmi_check/assets/configuration/spec.yaml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,18 +143,12 @@ files:
143143
The `tag_by` parameter lets you tag each metric with a property from the WMI class you're using.
144144
This is only useful when you will have multiple values for your WMI query.
145145
Comma-separated list of property names
146+
Add aliases to the property tags with by appending AS <ALIAS_NAME> to the property name.
147+
For example: Name AS wmi_name will be tagged as wmi_name:value instead of Name:value.
146148
value:
147149
type: string
148150
display_default: null
149-
example: Name,Label
150-
- name: tag_by_prefix
151-
description: |
152-
The `tag_by_prefix` parameter lets you specify a prefix to use for the tags generated by the `tag_by` parameter.
153-
If not provided, the tags will not be prefixed. Prefixes will added like this: <PREFIX>_<PROPERTY_NAME>.
154-
value:
155-
type: string
156-
display_default: null
157-
example: wmi
151+
example: Name AS wmi_name,Label
158152
- name: tag_queries
159153
description: |
160154
The `tag_queries` parameter lets you specify a list of queries, to tag metrics with a target class property.

wmi_check/datadog_checks/wmi_check/__about__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
# All rights reserved
33
# Licensed under a 3-clause BSD style license (see LICENSE)
44

5-
__version__ = '3.4.0'
5+
__version__ = '3.5.0'

wmi_check/datadog_checks/wmi_check/config_models/instance.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ class InstanceConfig(BaseModel):
4848
provider: Optional[int] = None
4949
service: Optional[str] = None
5050
tag_by: Optional[str] = None
51-
tag_by_prefix: Optional[str] = None
5251
tag_queries: Optional[tuple[tuple[str, ...], ...]] = None
5352
tags: Optional[tuple[str, ...]] = None
5453
username: Optional[str] = None

wmi_check/datadog_checks/wmi_check/data/conf.yaml.example

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,10 @@ instances:
132132
## The `tag_by` parameter lets you tag each metric with a property from the WMI class you're using.
133133
## This is only useful when you will have multiple values for your WMI query.
134134
## Comma-separated list of property names
135+
## Add aliases to the property tags with by appending AS <ALIAS_NAME> to the property name.
136+
## For example: Name AS wmi_name will be tagged as wmi_name:value instead of Name:value.
135137
#
136-
# tag_by: Name,Label
137-
138-
## @param tag_by_prefix - string - optional
139-
## The `tag_by_prefix` parameter lets you specify a prefix to use for the tags generated by the `tag_by` parameter.
140-
## If not provided, the tags will not be prefixed. Prefixes will added like this: <PREFIX>_<PROPERTY_NAME>.
141-
#
142-
# tag_by_prefix: wmi
138+
# tag_by: Name AS wmi_name,Label
143139

144140
## @param tag_queries - list of lists - optional
145141
## The `tag_queries` parameter lets you specify a list of queries, to tag metrics with a target class property.

wmi_check/datadog_checks/wmi_check/wmi_check.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ def __init__(self, name, init_config, instances):
2121
self.filters = self.instance.get('filters', []) # type: List[Dict[str, WMIFilter]]
2222
self.metrics_to_capture = self.instance.get('metrics', []) # type: List[List[str]]
2323
self.tag_by = self.instance.get('tag_by', "") # type: str
24-
self.tag_by_prefix = self.instance.get('tag_by_prefix', "") # type: str
2524
self.tag_queries = self.instance.get('tag_queries', []) # type: List[TagQuery]
2625

2726
custom_tags = self.instance.get('tags', []) # type: List[str]
@@ -38,7 +37,18 @@ def check(self, _):
3837
# Create or retrieve an existing WMISampler
3938
metric_name_and_type_by_property, properties = self.get_wmi_properties()
4039

41-
wmi_sampler = self.get_running_wmi_sampler(properties, self.filters, tag_by=self.tag_by)
40+
tag_by_properties = ""
41+
for t in self.tag_by.split(','):
42+
t = t.strip()
43+
if ' AS ' in t:
44+
t_split = t.split(' AS ')
45+
t = t_split[0].strip()
46+
tag_by_properties += t + ","
47+
else:
48+
tag_by_properties += t + ","
49+
tag_by_properties = tag_by_properties.rstrip(',')
50+
51+
wmi_sampler = self.get_running_wmi_sampler(properties, self.filters, tag_by=tag_by_properties)
4252

4353
# Sample, extract & submit metrics
4454
try:
@@ -57,7 +67,7 @@ def check(self, _):
5767

5868
def extract_metrics(self, wmi_sampler):
5969
# type: (WMISampler) -> List[WMIMetric]
60-
return self._extract_metrics(wmi_sampler, self.tag_by, self.tag_queries, self.constant_tags, self.tag_by_prefix)
70+
return self._extract_metrics(wmi_sampler, self.tag_by, self.tag_queries, self.constant_tags)
6171

6272
def get_wmi_properties(self):
6373
# type: () -> WMIProperties

wmi_check/tests/conftest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,11 @@ def mock_sampler_with_tag_by_prefix():
114114
{
115115
"IOReadBytesPerSec": 20455,
116116
"IDProcess": 1234,
117-
"Name": "chrome.exe",
117+
"Name": "foo",
118118
"ThreadCount": 4,
119119
"VirtualBytes": 3811,
120120
"PercentProcessorTime": 5,
121+
"Label": "bar",
121122
}
122123
]
123124
main_property_names = [
@@ -127,6 +128,7 @@ def mock_sampler_with_tag_by_prefix():
127128
"PercentProcessorTime",
128129
"IDProcess",
129130
"Name",
131+
"Label",
130132
]
131133
main_sampler = MockSampler(main_wmi_objects, main_property_names)
132134
main_sampler.class_name = 'Win32_PerfFormattedData_PerfProc_Process'

wmi_check/tests/test_wmi_check.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import copy
66
import logging
7+
import pytest
78
from unittest.mock import patch
89

910
from . import common
@@ -109,23 +110,31 @@ def test_tag_queries_without_alias(mock_sampler_with_tag_queries, aggregator, ch
109110
aggregator.assert_all_metrics_covered()
110111

111112

112-
def test_tag_by_is_correctly_prefixed(mock_sampler_with_tag_by_prefix, aggregator, check):
113+
@pytest.mark.parametrize(
114+
"tag_by,result_tags",
115+
[
116+
('Name AS wmi_name', ['wmi_name:foo']),
117+
('Name,Label AS wmi_label', ['name:foo', 'wmi_label:bar']),
118+
('name as wmi_name,label as wmi_label', ['wmi_name:foo', 'wmi_label:bar']),
119+
('nameaswmi_name', [])
120+
],
121+
)
122+
def test_tag_by_is_correctly_prefixed(mock_sampler_with_tag_by_prefix, aggregator, check, tag_by, result_tags):
113123
instance = copy.deepcopy(common.INSTANCE)
114-
instance['tag_by_prefix'] = 'wmi'
124+
instance['tag_by'] = tag_by
115125

116126
c = check(instance)
117127

118128
with patch.object(c, '_extract_metrics', wraps=c._extract_metrics) as mock_extract:
119129
c.check(instance)
120130
assert mock_extract.called
121131
# Check the arguments it was called with
122-
# _extract_metrics(self, wmi_sampler, tag_by, tag_queries, constant_tags, tag_by_prefix)
132+
# _extract_metrics(self, wmi_sampler, tag_by, tag_queries, constant_tags)
123133
call_args = mock_extract.call_args
124-
# The last positional argument (index 4) should be tag_by_prefix
125-
assert call_args[0][4] == 'wmi'
134+
assert call_args[0][1] == tag_by
126135

127-
# Verify metrics are tagged with the prefix
136+
# Verify metrics are tagged with the alias
128137
for metric in common.INSTANCE_METRICS:
129-
aggregator.assert_metric(metric, tags=['wmi_name:chrome.exe'], count=1)
138+
aggregator.assert_metric(metric, tags=result_tags, count=1)
130139

131140
aggregator.assert_all_metrics_covered()

0 commit comments

Comments
 (0)