Skip to content

Commit 8eec12f

Browse files
authored
[receiver/windowsperfcounter] Move configs from pkg/winperfcounters (#10022)
This refactoring reduces coupling between `windowsperfcounterreceiver` and `pkg/winperfcounters` packages by moving config structs specific to `windowsperfcounterreceiver` along with other logic related to the configs from `pkg/winperfcounters`.
1 parent 97c74ec commit 8eec12f

File tree

17 files changed

+252
-378
lines changed

17 files changed

+252
-378
lines changed

pkg/winperfcounters/config.go

Lines changed: 0 additions & 33 deletions
This file was deleted.

pkg/winperfcounters/config_windows.go

Lines changed: 0 additions & 32 deletions
This file was deleted.

pkg/winperfcounters/go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ go 1.17
44

55
require (
66
github.com/stretchr/testify v1.7.1
7-
go.uber.org/multierr v1.8.0
87
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9
98
)
109

@@ -13,7 +12,6 @@ require (
1312
github.com/kr/pretty v0.3.0 // indirect
1413
github.com/pmezard/go-difflib v1.0.0 // indirect
1514
github.com/rogpeppe/go-internal v1.6.2 // indirect
16-
go.uber.org/atomic v1.9.0 // indirect
1715
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
1816
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
1917
)

pkg/winperfcounters/go.sum

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/winperfcounters/watcher.go

Lines changed: 6 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ package winperfcounters // import "github.com/open-telemetry/opentelemetry-colle
2020
import (
2121
"fmt"
2222

23-
"go.uber.org/multierr"
24-
2523
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/winperfcounters/internal/pdh"
24+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/winperfcounters/internal/third_party/telegraf/win_perf_counters"
2625
)
2726

27+
// TODO: This package became redundant as a wrapper. pkg/winperfcounters/internal/pdh can be moved here.
28+
2829
var _ PerfCounterWatcher = (*Watcher)(nil)
2930

3031
// PerfCounterWatcher represents how to scrape data
@@ -35,16 +36,11 @@ type PerfCounterWatcher interface {
3536
ScrapeData() ([]CounterValue, error)
3637
// Close all counters/handles related to the query and free all associated memory.
3738
Close() error
38-
// GetMetricRep gets the representation of the metric the watcher is connected to
39-
GetMetricRep() MetricRep
4039
}
4140

42-
const instanceLabelName = "instance"
41+
type CounterValue = win_perf_counters.CounterValue
4342

44-
type Watcher struct {
45-
Counter *pdh.PerfCounter
46-
MetricRep
47-
}
43+
type Watcher = pdh.PerfCounter
4844

4945
// NewWatcher creates new PerfCounterWatcher by provided parts of its path.
5046
func NewWatcher(object, instance, counterName string) (PerfCounterWatcher, error) {
@@ -53,72 +49,7 @@ func NewWatcher(object, instance, counterName string) (PerfCounterWatcher, error
5349
if err != nil {
5450
return nil, fmt.Errorf("failed to create perf counter with path %v: %w", path, err)
5551
}
56-
return Watcher{Counter: counter}, nil
57-
}
58-
59-
func (w Watcher) Path() string {
60-
return w.Counter.Path()
61-
}
62-
63-
func (w Watcher) ScrapeData() ([]CounterValue, error) {
64-
scrapedCounterValues, err := w.Counter.ScrapeData()
65-
if err != nil {
66-
return []CounterValue{}, err
67-
}
68-
69-
counterValues := []CounterValue{}
70-
for _, counterValue := range scrapedCounterValues {
71-
metric := w.GetMetricRep()
72-
if counterValue.InstanceName != "" {
73-
if metric.Attributes == nil {
74-
metric.Attributes = map[string]string{instanceLabelName: counterValue.InstanceName}
75-
}
76-
metric.Attributes[instanceLabelName] = counterValue.InstanceName
77-
}
78-
counterValues = append(counterValues, CounterValue{MetricRep: metric, Value: counterValue.Value})
79-
}
80-
return counterValues, nil
81-
}
82-
83-
func (w Watcher) Close() error {
84-
return w.Counter.Close()
85-
}
86-
87-
func (w Watcher) GetMetricRep() MetricRep {
88-
return w.MetricRep
89-
}
90-
91-
// BuildPaths creates watchers and their paths from configs.
92-
func (objCfg ObjectConfig) BuildPaths() ([]PerfCounterWatcher, error) {
93-
var errs error
94-
var watchers []PerfCounterWatcher
95-
96-
for _, instance := range objCfg.instances() {
97-
for _, counterCfg := range objCfg.Counters {
98-
counterPath := counterPath(objCfg.Object, instance, counterCfg.Name)
99-
100-
c, err := pdh.NewPerfCounter(counterPath, true)
101-
if err != nil {
102-
errs = multierr.Append(errs, fmt.Errorf("counter %v: %w", counterPath, err))
103-
} else {
104-
newWatcher := Watcher{Counter: c}
105-
106-
if counterCfg.MetricRep.Name != "" {
107-
metricCfg := MetricRep{Name: counterCfg.MetricRep.Name}
108-
if counterCfg.Attributes != nil {
109-
metricCfg.Attributes = counterCfg.Attributes
110-
}
111-
newWatcher.MetricRep = metricCfg
112-
} else {
113-
newWatcher.MetricRep.Name = c.Path()
114-
}
115-
116-
watchers = append(watchers, newWatcher)
117-
}
118-
}
119-
}
120-
121-
return watchers, errs
52+
return counter, nil
12253
}
12354

12455
func counterPath(object, instance, counterName string) string {
@@ -128,8 +59,3 @@ func counterPath(object, instance, counterName string) string {
12859

12960
return fmt.Sprintf("\\%s%s\\%s", object, instance, counterName)
13061
}
131-
132-
type CounterValue struct {
133-
MetricRep
134-
Value float64
135-
}

pkg/winperfcounters/watcher_test.go

Lines changed: 21 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -21,136 +21,46 @@ import (
2121
"testing"
2222

2323
"github.com/stretchr/testify/require"
24-
"go.uber.org/multierr"
2524
)
2625

27-
// Test_PathBuilder tests that paths are built correctly given a ObjectConfig
28-
func Test_PathBuilder(t *testing.T) {
26+
func TestCounterPath(t *testing.T) {
2927
testCases := []struct {
30-
name string
31-
cfgs []ObjectConfig
32-
expectedErr string
33-
expectedPaths []string
28+
name string
29+
object string
30+
instance string
31+
counterName string
32+
expectedPath string
3433
}{
3534
{
36-
name: "basicPath",
37-
cfgs: []ObjectConfig{
38-
{
39-
Object: "Memory",
40-
Counters: []CounterConfig{{Name: "Committed Bytes"}},
41-
},
42-
},
43-
expectedPaths: []string{"\\Memory\\Committed Bytes"},
35+
name: "basicPath",
36+
object: "Memory",
37+
counterName: "Committed Bytes",
38+
expectedPath: "\\Memory\\Committed Bytes",
4439
},
4540
{
46-
name: "multiplePaths",
47-
cfgs: []ObjectConfig{
48-
{
49-
Object: "Memory",
50-
Counters: []CounterConfig{{Name: "Committed Bytes"}},
51-
},
52-
{
53-
Object: "Memory",
54-
Counters: []CounterConfig{{Name: "Available Bytes"}},
55-
},
56-
},
57-
expectedPaths: []string{"\\Memory\\Committed Bytes", "\\Memory\\Available Bytes"},
58-
},
59-
{
60-
name: "multipleIndividualCounters",
61-
cfgs: []ObjectConfig{
62-
{
63-
Object: "Memory",
64-
Counters: []CounterConfig{
65-
{Name: "Committed Bytes"},
66-
{Name: "Available Bytes"},
67-
},
68-
},
69-
{
70-
Object: "Memory",
71-
Counters: []CounterConfig{},
72-
},
73-
},
74-
expectedPaths: []string{"\\Memory\\Committed Bytes", "\\Memory\\Available Bytes"},
75-
},
76-
{
77-
name: "invalidCounter",
78-
cfgs: []ObjectConfig{
79-
{
80-
Object: "Broken",
81-
Counters: []CounterConfig{{Name: "Broken Counter"}},
82-
},
83-
},
84-
85-
expectedErr: "counter \\Broken\\Broken Counter: The specified object was not found on the computer.\r\n",
86-
},
87-
{
88-
name: "multipleInvalidCounters",
89-
cfgs: []ObjectConfig{
90-
{
91-
Object: "Broken",
92-
Counters: []CounterConfig{{Name: "Broken Counter"}},
93-
},
94-
{
95-
Object: "Broken part 2",
96-
Counters: []CounterConfig{{Name: "Broken again"}},
97-
},
98-
},
99-
expectedErr: "counter \\Broken\\Broken Counter: The specified object was not found on the computer.\r\n; counter \\Broken part 2\\Broken again: The specified object was not found on the computer.\r\n",
41+
name: "basicPathWithInstance",
42+
object: "Web Service",
43+
instance: "_Total",
44+
counterName: "Current Connections",
45+
expectedPath: "\\Web Service(_Total)\\Current Connections",
10046
},
10147
}
10248

10349
for _, test := range testCases {
10450
t.Run(test.name, func(t *testing.T) {
105-
var errs error
106-
allWatchers := []PerfCounterWatcher{}
107-
for _, cfg := range test.cfgs {
108-
watchers, err := cfg.BuildPaths()
109-
if err != nil {
110-
errs = multierr.Append(errs, err)
111-
continue
112-
}
113-
allWatchers = append(allWatchers, watchers...)
114-
}
115-
116-
if test.expectedErr != "" {
117-
require.EqualError(t, errs, test.expectedErr)
118-
return
119-
}
120-
121-
actualPaths := []string{}
122-
for _, watcher := range allWatchers {
123-
actualPaths = append(actualPaths, watcher.Path())
124-
}
125-
126-
require.Equal(t, test.expectedPaths, actualPaths)
51+
path := counterPath(test.object, test.instance, test.counterName)
52+
require.Equal(t, test.expectedPath, path)
12753
})
12854
}
12955
}
13056

13157
// Test_Scraping_Wildcard tests that wildcard instances pull out values
13258
func Test_Scraping_Wildcard(t *testing.T) {
133-
counterVals := []CounterValue{}
134-
var errs error
135-
136-
cfg := ObjectConfig{
137-
Object: "LogicalDisk",
138-
Instances: []string{"*"},
139-
Counters: []CounterConfig{{Name: "Free Megabytes"}},
140-
}
141-
watchers, err := cfg.BuildPaths()
59+
watcher, err := NewWatcher("LogicalDisk", "*", "Free Megabytes")
14260
require.NoError(t, err)
14361

144-
for _, watcher := range watchers {
145-
value, err := watcher.ScrapeData()
146-
if err != nil {
147-
errs = multierr.Append(errs, err)
148-
continue
149-
}
150-
151-
require.NoError(t, err)
152-
counterVals = append(counterVals, value...)
153-
}
62+
values, err := watcher.ScrapeData()
63+
require.NoError(t, err)
15464

155-
require.GreaterOrEqual(t, len(counterVals), 3)
65+
require.GreaterOrEqual(t, len(values), 3)
15666
}

receiver/activedirectorydsreceiver/counters.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -243,20 +243,5 @@ const (
243243
type defaultWatcherCreater struct{}
244244

245245
func (defaultWatcherCreater) Create(counterName string) (winperfcounters.PerfCounterWatcher, error) {
246-
conf := winperfcounters.ObjectConfig{
247-
Object: object,
248-
Instances: []string{instanceName},
249-
Counters: []winperfcounters.CounterConfig{
250-
{
251-
Name: counterName,
252-
},
253-
},
254-
}
255-
256-
watchers, err := conf.BuildPaths()
257-
if err != nil {
258-
return nil, err
259-
}
260-
261-
return watchers[0], nil
246+
return winperfcounters.NewWatcher(object, instanceName, counterName)
262247
}

0 commit comments

Comments
 (0)