Skip to content

Commit

Permalink
Assign object name from counter path when the instance name is not de…
Browse files Browse the repository at this point in the history
…fined (#11878)

* Assign object name from counter path when the instance name is not defined

Fixes #6528

* Adressed review comments

* Fix changelog entry
  • Loading branch information
narph authored Apr 25, 2019
1 parent 1992301 commit 62dc9bf
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Allow module configurations to have variants {pull}9118[9118]
- Add `timeseries.instance` field calculation. {pull}10293[10293]
- Added new disk states and raid level to the system/raid metricset. {pull}11613[11613]
- Add check on object name in the counter path if the instance name is missing {issue}6528[6528] {pull}11878[11878]

*Packetbeat*

Expand Down
32 changes: 32 additions & 0 deletions metricbeat/module/windows/perfmon/pdh_integration_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"time"
"unsafe"

"github.com/elastic/beats/libbeat/common"

"github.com/pkg/errors"
"github.com/stretchr/testify/assert"

Expand Down Expand Up @@ -72,6 +74,36 @@ func TestData(t *testing.T) {
mbtest.WriteEventToDataJSON(t, beatEvent, "")
}

func TestCounterWithNoInstanceName(t *testing.T) {
config := map[string]interface{}{
"module": "windows",
"metricsets": []string{"perfmon"},
"perfmon.counters": []map[string]string{
{
"instance_label": "processor.name",
"measurement_label": "processor.time.total.pct",
"query": `\UDPv4\Datagrams Sent/sec`,
},
},
}

ms := mbtest.NewReportingMetricSetV2(t, config)
mbtest.ReportingFetchV2(ms)
time.Sleep(60 * time.Millisecond)

events, errs := mbtest.ReportingFetchV2(ms)
if len(errs) > 0 {
t.Fatal(errs)
}
if len(events) == 0 {
t.Fatal("no events received")
}
process := events[0].MetricSetFields["processor"].(common.MapStr)
// Check values
assert.EqualValues(t, "UDPv4", process["name"])

}

func TestQuery(t *testing.T) {
q, err := NewQuery("")
if err != nil {
Expand Down
21 changes: 16 additions & 5 deletions metricbeat/module/windows/perfmon/pdh_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var (
sizeofPdhCounterValueItem = (int)(unsafe.Sizeof(pdhCounterValueItem{}))
wildcardRegexp = regexp.MustCompile(`.*\(\*\).*`)
instanceNameRegexp = regexp.MustCompile(`.*\((.*)\).*`)
objectNameRegexp = regexp.MustCompile(`\\([^\\]+)\\`)
)

type PdhQueryHandle uintptr
Expand Down Expand Up @@ -247,13 +248,11 @@ func (q *Query) AddCounter(counterPath string, format Format, instanceName strin

// Extract the instance name from the counterPath for non-wildcard paths.
if !wildcard && instanceName == "" {
matches := instanceNameRegexp.FindStringSubmatch(counterPath)
if len(matches) != 2 {
return errors.New("query doesn't contain an instance name. In this case you have to define 'instance_name'")
instanceName, err = matchInstanceName(counterPath)
if err != nil {
return err
}
instanceName = matches[1]
}

q.counters[counterPath] = &Counter{
handle: h,
instanceName: instanceName,
Expand All @@ -268,6 +267,18 @@ func (q *Query) AddCounter(counterPath string, format Format, instanceName strin
return nil
}

// matchInstanceName will check first for instance and then for any objects names
func matchInstanceName(counterPath string) (string, error) {
matches := instanceNameRegexp.FindStringSubmatch(counterPath)
if len(matches) != 2 {
matches = objectNameRegexp.FindStringSubmatch(counterPath)
}
if len(matches) == 2 {
return matches[1], nil
}
return "", errors.New("query doesn't contain an instance name. In this case you have to define 'instance_name'")
}

func (q *Query) Execute() error {
return PdhCollectQueryData(q.handle)
}
Expand Down

0 comments on commit 62dc9bf

Please sign in to comment.