Skip to content

Commit f2a55fd

Browse files
authored
Fix instance name in perfmon metricset (elastic#22261)
* mofidy doc * fix * changelog * add test
1 parent 4720359 commit f2a55fd

File tree

3 files changed

+65
-62
lines changed

3 files changed

+65
-62
lines changed

CHANGELOG.next.asciidoc

+1
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
387387
- [Kubernetes] Remove redundant dockersock volume mount {pull}22009[22009]
388388
- Revert change to report `process.memory.rss` as `process.memory.wss` on Windows. {pull}22055[22055]
389389
- Add a switch to the driver definition on SQL module to use pretty names {pull}17378[17378]
390+
- Fix instance name in perfmon metricset. {issue}22218[22218] {pull}22261[22261]
390391
- Remove io.time from windows {pull}22237[22237]
391392
- Add interval information to `monitor` metricset in azure. {pull}22152[22152]
392393

metricbeat/module/windows/perfmon/reader.go

+51-62
Original file line numberDiff line numberDiff line change
@@ -73,78 +73,23 @@ func NewReader(config Config) (*Reader, error) {
7373
config: config,
7474
}
7575
r.mapCounters(config)
76-
for i, counter := range r.counters {
77-
r.counters[i].ChildQueries = []string{}
78-
childQueries, err := query.GetCounterPaths(counter.QueryName)
79-
if err != nil {
80-
if config.IgnoreNECounters {
81-
switch err {
82-
case pdh.PDH_CSTATUS_NO_COUNTER, pdh.PDH_CSTATUS_NO_COUNTERNAME,
83-
pdh.PDH_CSTATUS_NO_INSTANCE, pdh.PDH_CSTATUS_NO_OBJECT:
84-
r.log.Infow("Ignoring non existent counter", "error", err,
85-
logp.Namespace("perfmon"), "query", counter.QueryName)
86-
continue
87-
}
88-
} else {
89-
query.Close()
90-
return nil, errors.Wrapf(err, `failed to expand counter (query="%v")`, counter.QueryName)
91-
}
92-
}
93-
// check if the pdhexpandcounterpath/pdhexpandwildcardpath functions have expanded the counter successfully.
94-
if len(childQueries) == 0 || (len(childQueries) == 1 && strings.Contains(childQueries[0], "*")) {
95-
// covering cases when PdhExpandWildCardPathW returns no counter paths or is unable to expand and the ignore_non_existent_counters flag is set
96-
if config.IgnoreNECounters {
97-
r.log.Infow("Ignoring non existent counter", "initial query", counter.QueryName,
98-
logp.Namespace("perfmon"), "expanded query", childQueries)
99-
continue
100-
}
101-
return nil, errors.Errorf(`failed to expand counter (query="%v"), no error returned`, counter.QueryName)
102-
}
103-
for _, v := range childQueries {
104-
if err := query.AddCounter(v, counter.InstanceName, counter.Format, len(childQueries) > 1); err != nil {
105-
return nil, errors.Wrapf(err, `failed to add counter (query="%v")`, counter.QueryName)
106-
}
107-
r.counters[i].ChildQueries = append(r.counters[i].ChildQueries, v)
108-
}
76+
_, err := r.getCounterPaths()
77+
if err != nil {
78+
return nil, err
10979
}
11080
return r, nil
11181
}
11282

11383
// RefreshCounterPaths will recheck for any new instances and add them to the counter list
11484
func (re *Reader) RefreshCounterPaths() error {
115-
var newCounters []string
116-
for i, counter := range re.counters {
117-
re.counters[i].ChildQueries = []string{}
118-
childQueries, err := re.query.GetCounterPaths(counter.QueryName)
119-
if err != nil {
120-
if re.config.IgnoreNECounters {
121-
switch err {
122-
case pdh.PDH_CSTATUS_NO_COUNTER, pdh.PDH_CSTATUS_NO_COUNTERNAME,
123-
pdh.PDH_CSTATUS_NO_INSTANCE, pdh.PDH_CSTATUS_NO_OBJECT:
124-
re.log.Infow("Ignoring non existent counter", "error", err,
125-
logp.Namespace("perfmon"), "query", counter.QueryName)
126-
continue
127-
}
128-
} else {
129-
return errors.Wrapf(err, `failed to expand counter (query="%v")`, counter.QueryName)
130-
}
131-
}
132-
newCounters = append(newCounters, childQueries...)
133-
// there are cases when the ExpandWildCardPath will retrieve a successful status but not an expanded query so we need to check for the size of the list
134-
if err == nil && len(childQueries) >= 1 && !strings.Contains(childQueries[0], "*") {
135-
for _, v := range childQueries {
136-
if err := re.query.AddCounter(v, counter.InstanceName, counter.Format, len(childQueries) > 1); err != nil {
137-
return errors.Wrapf(err, "failed to add counter (query='%v')", counter.QueryName)
138-
}
139-
re.counters[i].ChildQueries = append(re.counters[i].ChildQueries, v)
140-
}
141-
}
85+
newCounters, err := re.getCounterPaths()
86+
if err != nil {
87+
return errors.Wrap(err, "failed retrieving counter paths")
14288
}
143-
err := re.query.RemoveUnusedCounters(newCounters)
89+
err = re.query.RemoveUnusedCounters(newCounters)
14490
if err != nil {
14591
return errors.Wrap(err, "failed removing unused counter values")
14692
}
147-
14893
return nil
14994
}
15095

@@ -184,6 +129,39 @@ func (re *Reader) Close() error {
184129
return re.query.Close()
185130
}
186131

132+
// getCounterPaths func will process the counter paths based on the configuration options entered
133+
func (re *Reader) getCounterPaths() ([]string, error) {
134+
var newCounters []string
135+
for i, counter := range re.counters {
136+
re.counters[i].ChildQueries = []string{}
137+
childQueries, err := re.query.GetCounterPaths(counter.QueryName)
138+
if err != nil {
139+
if re.config.IgnoreNECounters {
140+
switch err {
141+
case pdh.PDH_CSTATUS_NO_COUNTER, pdh.PDH_CSTATUS_NO_COUNTERNAME,
142+
pdh.PDH_CSTATUS_NO_INSTANCE, pdh.PDH_CSTATUS_NO_OBJECT:
143+
re.log.Infow("Ignoring non existent counter", "error", err,
144+
logp.Namespace("perfmon"), "query", counter.QueryName)
145+
continue
146+
}
147+
} else {
148+
return newCounters, errors.Wrapf(err, `failed to expand counter (query="%v")`, counter.QueryName)
149+
}
150+
}
151+
newCounters = append(newCounters, childQueries...)
152+
// there are cases when the ExpandWildCardPath will retrieve a successful status but not an expanded query so we need to check for the size of the list
153+
if err == nil && len(childQueries) >= 1 && !strings.Contains(childQueries[0], "*") {
154+
for _, v := range childQueries {
155+
if err := re.query.AddCounter(v, counter.InstanceName, counter.Format, isWildcard(childQueries, counter.InstanceName)); err != nil {
156+
return newCounters, errors.Wrapf(err, "failed to add counter (query='%v')", counter.QueryName)
157+
}
158+
re.counters[i].ChildQueries = append(re.counters[i].ChildQueries, v)
159+
}
160+
}
161+
}
162+
return newCounters, nil
163+
}
164+
187165
func (re *Reader) getCounter(query string) (bool, PerfCounter) {
188166
for _, counter := range re.counters {
189167
for _, childQuery := range counter.ChildQueries {
@@ -310,3 +288,14 @@ func replaceUpperCase(src string) string {
310288
return newStr
311289
})
312290
}
291+
292+
// isWildcard function checks if users has configured a wildcard inside the instance configuration option and if the wildcard has been resulted in a valid number of queries
293+
func isWildcard(queries []string, instance string) bool {
294+
if len(queries) > 1 {
295+
return true
296+
}
297+
if len(queries) == 1 && strings.Contains(instance, "*") {
298+
return true
299+
}
300+
return false
301+
}

metricbeat/module/windows/perfmon/reader_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,16 @@ func TestMapCounterPathLabel(t *testing.T) {
158158
assert.Equal(t, result, "metrics.logicaldisk_avg_disk_sec_per_transfer")
159159

160160
}
161+
162+
func TestIsWildcard(t *testing.T) {
163+
queries := []string{"\\Process(chrome)\\% User Time", "\\Process(chrome#1)\\% User Time", "\\Process(svchost)\\% User Time"}
164+
instance := "*"
165+
result := isWildcard(queries, instance)
166+
assert.True(t, result)
167+
queries = []string{"\\Process(chrome)\\% User Time"}
168+
result = isWildcard(queries, instance)
169+
assert.True(t, result)
170+
instance = "chrome"
171+
result = isWildcard(queries, instance)
172+
assert.False(t, result)
173+
}

0 commit comments

Comments
 (0)