Skip to content

Commit 2657f71

Browse files
authored
Move pdh query to shared location in order for new modules/metricsets to reuse (elastic#15503)
* Move pdh query to shared location * Update changelog * Fix make update * mage fmt * fix changelog
1 parent d0a80a2 commit 2657f71

18 files changed

Lines changed: 69 additions & 69 deletions

CHANGELOG.next.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ TLS or Beats that accept connections over TLS and validate client certificates.
8383

8484
*Metricbeat*
8585

86+
- Move the windows pdh implementation from perfmon to a shared location in order for future modules/metricsets to make use of. {pull}15503[15503]
8687
- Add lambda metricset in aws module. {pull}15260[15260]
8788
- Expand data for the `system/memory` metricset {pull}15492[15492]
8889
- Add azure `storage` metricset in order to retrieve metric values for storage accounts. {issue}14548[14548] {pull}15342[15342]

metricbeat/module/windows/perfmon/defs_pdh_windows.go renamed to metricbeat/helper/windows/pdh/defs_pdh_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
// +build ignore
2222

23-
package perfmon
23+
package pdh
2424

2525
/*
2626
#include <pdh.h>

metricbeat/module/windows/perfmon/defs_pdh_windows_386.go renamed to metricbeat/helper/windows/pdh/defs_pdh_windows_386.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

metricbeat/module/windows/perfmon/defs_pdh_windows_amd64.go renamed to metricbeat/helper/windows/pdh/defs_pdh_windows_amd64.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
/*
19-
Package windows is a Metricbeat module that contains MetricSets.
20-
*/
21-
package windows
18+
package pdh
19+
20+
//go:generate go run mkpdh_defs.go
21+
//go:generate go run ../run.go -cmd "go tool cgo -godefs defs_pdh_windows.go" -goarch amd64 -output defs_pdh_windows_amd64.go
22+
//go:generate go run ../run.go -cmd "go tool cgo -godefs defs_pdh_windows.go" -goarch 386 -output defs_pdh_windows_386.go
23+
//go:generate go run $GOROOT/src/syscall/mksyscall_windows.go -output zpdh_windows.go pdh_windows.go
24+
//go:generate gofmt -w defs_pdh_windows_amd64.go defs_pdh_windows_386.go zpdh_windows.go

metricbeat/module/windows/perfmon/mkpdh_defs.go renamed to metricbeat/helper/windows/pdh/mkpdh_defs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const fileTemplate = `
4848
4949
// +build ignore
5050
51-
package perfmon
51+
package pdh
5252
5353
/*
5454
#include <pdh.h>

metricbeat/module/windows/perfmon/pdh_query_windows.go renamed to metricbeat/helper/windows/pdh/pdh_query_windows.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
// +build windows
1919

20-
package perfmon
20+
package pdh
2121

2222
import (
2323
"regexp"
@@ -43,8 +43,8 @@ type Counter struct {
4343

4444
// Query contains the pdh.
4545
type Query struct {
46-
handle PdhQueryHandle
47-
counters map[string]*Counter
46+
Handle PdhQueryHandle
47+
Counters map[string]*Counter
4848
}
4949

5050
// CounterValue contains the performance counter values.
@@ -60,42 +60,42 @@ func (q *Query) Open() error {
6060
if err != nil {
6161
return err
6262
}
63-
q.handle = h
64-
q.counters = make(map[string]*Counter)
63+
q.Handle = h
64+
q.Counters = make(map[string]*Counter)
6565
return nil
6666
}
6767

6868
// AddEnglishCounter adds the specified counter to the query.
6969
func (q *Query) AddEnglishCounter(counterPath string) (PdhCounterHandle, error) {
70-
h, err := PdhAddEnglishCounter(q.handle, counterPath, 0)
70+
h, err := PdhAddEnglishCounter(q.Handle, counterPath, 0)
7171
return h, err
7272
}
7373

7474
// AddCounter adds the specified counter to the query.
75-
func (q *Query) AddCounter(counterPath string, counter CounterConfig, wildcard bool) error {
76-
if _, found := q.counters[counterPath]; found {
75+
func (q *Query) AddCounter(counterPath string, instance string, format string, wildcard bool) error {
76+
if _, found := q.Counters[counterPath]; found {
7777
return nil
7878
}
7979
var err error
8080
var instanceName string
8181
// Extract the instance name from the counterPath.
82-
if counter.InstanceName == "" || wildcard {
82+
if instance == "" || wildcard {
8383
instanceName, err = matchInstanceName(counterPath)
8484
if err != nil {
8585
return err
8686
}
8787
} else {
88-
instanceName = counter.InstanceName
88+
instanceName = instance
8989
}
90-
h, err := PdhAddCounter(q.handle, counterPath, 0)
90+
h, err := PdhAddCounter(q.Handle, counterPath, 0)
9191
if err != nil {
9292
return err
9393
}
9494

95-
q.counters[counterPath] = &Counter{
95+
q.Counters[counterPath] = &Counter{
9696
handle: h,
9797
instanceName: instanceName,
98-
format: getPDHFormat(counter.Format),
98+
format: getPDHFormat(format),
9999
}
100100
return nil
101101
}
@@ -134,7 +134,7 @@ func (q *Query) RemoveUnusedCounters(counters []string) error {
134134
}
135135
}
136136
unused := make(map[string]*Counter)
137-
for counterPath, counter := range q.counters {
137+
for counterPath, counter := range q.Counters {
138138
if !matchCounter(counterPath, counters) {
139139
unused[counterPath] = counter
140140
}
@@ -147,7 +147,7 @@ func (q *Query) RemoveUnusedCounters(counters []string) error {
147147
if err != nil {
148148
return err
149149
}
150-
delete(q.counters, counterPath)
150+
delete(q.Counters, counterPath)
151151
}
152152
return nil
153153
}
@@ -163,16 +163,16 @@ func matchCounter(counterPath string, counterList []string) bool {
163163

164164
// CollectData collects the value for all counters in the query.
165165
func (q *Query) CollectData() error {
166-
return PdhCollectQueryData(q.handle)
166+
return PdhCollectQueryData(q.Handle)
167167
}
168168

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

207207
// Close closes the query and all of its counters.
208208
func (q *Query) Close() error {
209-
return PdhCloseQuery(q.handle)
209+
return PdhCloseQuery(q.Handle)
210210
}
211211

212212
// matchInstanceName will check first for instance and then for any objects names.

metricbeat/module/windows/perfmon/pdh_query_windows_test.go renamed to metricbeat/helper/windows/pdh/pdh_query_windows_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
package perfmon
18+
package pdh
1919

2020
import (
2121
"syscall"
@@ -38,8 +38,7 @@ func TestAddCounterInvalidArgWhenQueryClosed(t *testing.T) {
3838
queryPath, err := q.GetCounterPaths(validQuery)
3939
// if windows os language is ENG then err will be nil, else the GetCounterPaths will execute the AddCounter
4040
if assert.NoError(t, err) {
41-
counter := CounterConfig{Format: "float", InstanceName: "TestInstanceName"}
42-
err = q.AddCounter(queryPath[0], counter, false)
41+
err = q.AddCounter(queryPath[0], "TestInstanceName", "float", false)
4342
assert.Error(t, err, PDH_INVALID_HANDLE)
4443
} else {
4544
assert.Error(t, err, PDH_INVALID_ARGUMENT)
@@ -70,12 +69,11 @@ func TestSuccessfulQuery(t *testing.T) {
7069
t.Fatal(err)
7170
}
7271
defer q.Close()
73-
counter := CounterConfig{Format: "float", InstanceName: "TestInstanceName"}
7472
queryPath, err := q.GetCounterPaths(validQuery)
7573
if err != nil {
7674
t.Fatal(err)
7775
}
78-
err = q.AddCounter(queryPath[0], counter, false)
76+
err = q.AddCounter(queryPath[0], "TestInstanceName", "floar", false)
7977
if err != nil {
8078
t.Fatal(err)
8179
}

metricbeat/module/windows/perfmon/pdh_windows.go renamed to metricbeat/helper/windows/pdh/pdh_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
// +build windows
1919

20-
package perfmon
20+
package pdh
2121

2222
import (
2323
"strconv"

metricbeat/module/windows/perfmon/pdh_windows_test.go renamed to metricbeat/helper/windows/pdh/pdh_windows_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
package perfmon
18+
package pdh
1919

2020
import (
2121
"syscall"

0 commit comments

Comments
 (0)