Skip to content

Commit

Permalink
Move pdh query to shared location in order for new modules/metricsets…
Browse files Browse the repository at this point in the history
… to reuse (#15503) (#16722)

* Move pdh query to shared location

* Update changelog

* Fix make update

* mage fmt

* fix changelog

(cherry picked from commit 2657f71)
  • Loading branch information
narph authored Mar 2, 2020
1 parent 976f692 commit 7884758
Show file tree
Hide file tree
Showing 18 changed files with 69 additions and 69 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d

*Metricbeat*

- Move the windows pdh implementation from perfmon to a shared location in order for future modules/metricsets to make use of. {pull}15503[15503]
- Add lambda metricset in aws module. {pull}15260[15260]
- Expand data for the `system/memory` metricset {pull}15492[15492]
- Add azure `storage` metricset in order to retrieve metric values for storage accounts. {issue}14548[14548] {pull}15342[15342]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

// +build ignore

package perfmon
package pdh

/*
#include <pdh.h>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
// specific language governing permissions and limitations
// under the License.

/*
Package windows is a Metricbeat module that contains MetricSets.
*/
package windows
package pdh

//go:generate go run mkpdh_defs.go
//go:generate go run ../run.go -cmd "go tool cgo -godefs defs_pdh_windows.go" -goarch amd64 -output defs_pdh_windows_amd64.go
//go:generate go run ../run.go -cmd "go tool cgo -godefs defs_pdh_windows.go" -goarch 386 -output defs_pdh_windows_386.go
//go:generate go run $GOROOT/src/syscall/mksyscall_windows.go -output zpdh_windows.go pdh_windows.go
//go:generate gofmt -w defs_pdh_windows_amd64.go defs_pdh_windows_386.go zpdh_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const fileTemplate = `
// +build ignore
package perfmon
package pdh
/*
#include <pdh.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

// +build windows

package perfmon
package pdh

import (
"regexp"
Expand All @@ -43,8 +43,8 @@ type Counter struct {

// Query contains the pdh.
type Query struct {
handle PdhQueryHandle
counters map[string]*Counter
Handle PdhQueryHandle
Counters map[string]*Counter
}

// CounterValue contains the performance counter values.
Expand All @@ -60,42 +60,42 @@ func (q *Query) Open() error {
if err != nil {
return err
}
q.handle = h
q.counters = make(map[string]*Counter)
q.Handle = h
q.Counters = make(map[string]*Counter)
return nil
}

// AddEnglishCounter adds the specified counter to the query.
func (q *Query) AddEnglishCounter(counterPath string) (PdhCounterHandle, error) {
h, err := PdhAddEnglishCounter(q.handle, counterPath, 0)
h, err := PdhAddEnglishCounter(q.Handle, counterPath, 0)
return h, err
}

// AddCounter adds the specified counter to the query.
func (q *Query) AddCounter(counterPath string, counter CounterConfig, wildcard bool) error {
if _, found := q.counters[counterPath]; found {
func (q *Query) AddCounter(counterPath string, instance string, format string, wildcard bool) error {
if _, found := q.Counters[counterPath]; found {
return nil
}
var err error
var instanceName string
// Extract the instance name from the counterPath.
if counter.InstanceName == "" || wildcard {
if instance == "" || wildcard {
instanceName, err = matchInstanceName(counterPath)
if err != nil {
return err
}
} else {
instanceName = counter.InstanceName
instanceName = instance
}
h, err := PdhAddCounter(q.handle, counterPath, 0)
h, err := PdhAddCounter(q.Handle, counterPath, 0)
if err != nil {
return err
}

q.counters[counterPath] = &Counter{
q.Counters[counterPath] = &Counter{
handle: h,
instanceName: instanceName,
format: getPDHFormat(counter.Format),
format: getPDHFormat(format),
}
return nil
}
Expand Down Expand Up @@ -134,7 +134,7 @@ func (q *Query) RemoveUnusedCounters(counters []string) error {
}
}
unused := make(map[string]*Counter)
for counterPath, counter := range q.counters {
for counterPath, counter := range q.Counters {
if !matchCounter(counterPath, counters) {
unused[counterPath] = counter
}
Expand All @@ -147,7 +147,7 @@ func (q *Query) RemoveUnusedCounters(counters []string) error {
if err != nil {
return err
}
delete(q.counters, counterPath)
delete(q.Counters, counterPath)
}
return nil
}
Expand All @@ -163,16 +163,16 @@ func matchCounter(counterPath string, counterList []string) bool {

// CollectData collects the value for all counters in the query.
func (q *Query) CollectData() error {
return PdhCollectQueryData(q.handle)
return PdhCollectQueryData(q.Handle)
}

// GetFormattedCounterValues returns an array of formatted values for a query.
func (q *Query) GetFormattedCounterValues() (map[string][]CounterValue, error) {
if q.counters == nil || len(q.counters) == 0 {
if q.Counters == nil || len(q.Counters) == 0 {
return nil, errors.New("no counter list found")
}
rtn := make(map[string][]CounterValue, len(q.counters))
for path, counter := range q.counters {
rtn := make(map[string][]CounterValue, len(q.Counters))
for path, counter := range q.Counters {
rtn[path] = append(rtn[path], getCounterValue(counter))
}
return rtn, nil
Expand Down Expand Up @@ -206,7 +206,7 @@ func (q *Query) ExpandWildCardPath(wildCardPath string) ([]string, error) {

// Close closes the query and all of its counters.
func (q *Query) Close() error {
return PdhCloseQuery(q.handle)
return PdhCloseQuery(q.Handle)
}

// matchInstanceName will check first for instance and then for any objects names.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package perfmon
package pdh

import (
"syscall"
Expand All @@ -38,8 +38,7 @@ func TestAddCounterInvalidArgWhenQueryClosed(t *testing.T) {
queryPath, err := q.GetCounterPaths(validQuery)
// if windows os language is ENG then err will be nil, else the GetCounterPaths will execute the AddCounter
if assert.NoError(t, err) {
counter := CounterConfig{Format: "float", InstanceName: "TestInstanceName"}
err = q.AddCounter(queryPath[0], counter, false)
err = q.AddCounter(queryPath[0], "TestInstanceName", "float", false)
assert.Error(t, err, PDH_INVALID_HANDLE)
} else {
assert.Error(t, err, PDH_INVALID_ARGUMENT)
Expand Down Expand Up @@ -70,12 +69,11 @@ func TestSuccessfulQuery(t *testing.T) {
t.Fatal(err)
}
defer q.Close()
counter := CounterConfig{Format: "float", InstanceName: "TestInstanceName"}
queryPath, err := q.GetCounterPaths(validQuery)
if err != nil {
t.Fatal(err)
}
err = q.AddCounter(queryPath[0], counter, false)
err = q.AddCounter(queryPath[0], "TestInstanceName", "floar", false)
if err != nil {
t.Fatal(err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

// +build windows

package perfmon
package pdh

import (
"strconv"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package perfmon
package pdh

import (
"syscall"
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
8 changes: 0 additions & 8 deletions metricbeat/module/windows/perfmon/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,4 @@
// specific language governing permissions and limitations
// under the License.

// Package perfmon implements a Metricbeat metricset for reading Windows
// performance counters.
package perfmon

//go:generate go run mkpdh_defs.go
//go:generate go run ../run.go -cmd "go tool cgo -godefs defs_pdh_windows.go" -goarch amd64 -output defs_pdh_windows_amd64.go
//go:generate go run ../run.go -cmd "go tool cgo -godefs defs_pdh_windows.go" -goarch 386 -output defs_pdh_windows_386.go
//go:generate go run $GOROOT/src/syscall/mksyscall_windows.go -output zpdh_windows.go pdh_windows.go
//go:generate gofmt -w defs_pdh_windows_amd64.go defs_pdh_windows_386.go zpdh_windows.go
2 changes: 1 addition & 1 deletion metricbeat/module/windows/perfmon/perfmon.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
// Fetch fetches events and reports them upstream
func (m *MetricSet) Fetch(report mb.ReporterV2) error {
// if the ignore_non_existent_counters flag is set and no valid counter paths are found the Read func will still execute, a check is done before
if len(m.reader.query.counters) == 0 {
if len(m.reader.query.Counters) == 0 {
return errors.New("no counters to read")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"testing"
"time"

"github.com/elastic/beats/metricbeat/helper/windows/pdh"

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

"github.com/pkg/errors"
Expand Down Expand Up @@ -107,7 +109,7 @@ func TestCounterWithNoInstanceName(t *testing.T) {
}

func TestQuery(t *testing.T) {
var q Query
var q pdh.Query
err := q.Open()
if err != nil {
t.Fatal(err)
Expand All @@ -118,7 +120,7 @@ func TestQuery(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = q.AddCounter(path[0], counter, false)
err = q.AddCounter(path[0], counter.InstanceName, counter.Format, false)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -178,7 +180,7 @@ func TestNonExistingCounter(t *testing.T) {
config.CounterConfig[0].Format = "float"
handle, err := NewReader(config)
if assert.Error(t, err) {
assert.EqualValues(t, PDH_CSTATUS_NO_COUNTER, errors.Cause(err))
assert.EqualValues(t, pdh.PDH_CSTATUS_NO_COUNTER, errors.Cause(err))
}

if handle != nil {
Expand All @@ -201,7 +203,7 @@ func TestIgnoreNonExistentCounter(t *testing.T) {
values, err := handle.Read()

if assert.Error(t, err) {
assert.EqualValues(t, PDH_NO_DATA, errors.Cause(err))
assert.EqualValues(t, pdh.PDH_NO_DATA, errors.Cause(err))
}

if handle != nil {
Expand All @@ -222,7 +224,7 @@ func TestNonExistingObject(t *testing.T) {
config.CounterConfig[0].Format = "float"
handle, err := NewReader(config)
if assert.Error(t, err) {
assert.EqualValues(t, PDH_CSTATUS_NO_OBJECT, errors.Cause(err))
assert.EqualValues(t, pdh.PDH_CSTATUS_NO_OBJECT, errors.Cause(err))
}

if handle != nil {
Expand All @@ -232,7 +234,7 @@ func TestNonExistingObject(t *testing.T) {
}

func TestLongOutputFormat(t *testing.T) {
var query Query
var query pdh.Query
err := query.Open()
if err != nil {
t.Fatal(err)
Expand All @@ -244,8 +246,8 @@ func TestLongOutputFormat(t *testing.T) {
t.Fatal(err)
}
assert.NotZero(t, len(path))
err = query.AddCounter(path[0], counter, false)
if err != nil && err != PDH_NO_MORE_DATA {
err = query.AddCounter(path[0], counter.InstanceName, counter.Format, false)
if err != nil && err != pdh.PDH_NO_MORE_DATA {
t.Fatal(err)
}

Expand All @@ -272,7 +274,7 @@ func TestLongOutputFormat(t *testing.T) {
}

func TestFloatOutputFormat(t *testing.T) {
var query Query
var query pdh.Query
err := query.Open()
if err != nil {
t.Fatal(err)
Expand All @@ -284,8 +286,8 @@ func TestFloatOutputFormat(t *testing.T) {
t.Fatal(err)
}
assert.NotZero(t, len(path))
err = query.AddCounter(path[0], counter, false)
if err != nil && err != PDH_NO_MORE_DATA {
err = query.AddCounter(path[0], counter.InstanceName, counter.Format, false)
if err != nil && err != pdh.PDH_NO_MORE_DATA {
t.Fatal(err)
}

Expand Down
Loading

0 comments on commit 7884758

Please sign in to comment.