-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathwindowsperfcounters_scraper_test.go
572 lines (529 loc) · 16.9 KB
/
windowsperfcounters_scraper_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
//go:build windows
package windowsperfcountersreceiver
import (
"context"
"errors"
"fmt"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/receiver/scrapererror"
"go.opentelemetry.io/collector/receiver/scraperhelper"
"go.uber.org/multierr"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/winperfcounters"
)
type mockPerfCounter struct {
counterValues []winperfcounters.CounterValue
path string
scrapeErr error
closeErr error
resetErr error
}
func (w *mockPerfCounter) Reset() error {
return w.resetErr
}
func (w *mockPerfCounter) Path() string {
return w.path
}
func (w *mockPerfCounter) ScrapeData() ([]winperfcounters.CounterValue, error) {
return w.counterValues, w.scrapeErr
}
func (w *mockPerfCounter) Close() error {
return w.closeErr
}
func mockPerfCounterFactoryInvocations(mpcs ...mockPerfCounter) newWatcherFunc {
invocationNum := 0
return func(string, string, string) (winperfcounters.PerfCounterWatcher, error) {
if invocationNum == len(mpcs) {
return nil, fmt.Errorf("invoked watcher %d times but only %d were setup", invocationNum+1, len(mpcs))
}
mpc := mpcs[invocationNum]
invocationNum++
return &mpc, nil
}
}
func Test_WindowsPerfCounterScraper(t *testing.T) {
type testCase struct {
name string
cfg *Config
startMessage string
startErr string
expectedMetricPath string
}
defaultConfig := createDefaultConfig().(*Config)
testCases := []testCase{
{
name: "Standard",
cfg: &Config{
MetricMetaData: map[string]MetricConfig{
"cpu.idle": {
Description: "percentage of time CPU is idle.",
Unit: "%",
Gauge: GaugeMetric{},
},
"bytes.committed": {
Description: "number of bytes committed to memory",
Unit: "By",
Gauge: GaugeMetric{},
},
"processor.time": {
Description: "amount of time processor is busy",
Unit: "%",
Gauge: GaugeMetric{},
},
},
PerfCounters: []ObjectConfig{
{Object: "Memory", Counters: []CounterConfig{{Name: "Committed Bytes", MetricRep: MetricRep{Name: "bytes.committed"}}}},
{Object: "Processor", Instances: []string{"*"}, Counters: []CounterConfig{{Name: "% Idle Time", MetricRep: MetricRep{Name: "cpu.idle"}}}},
{Object: "Processor", Instances: []string{"1", "2"}, Counters: []CounterConfig{{Name: "% Processor Time", MetricRep: MetricRep{Name: "processor.time"}}}},
},
ControllerConfig: scraperhelper.ControllerConfig{CollectionInterval: time.Minute, InitialDelay: time.Second},
},
expectedMetricPath: filepath.Join("testdata", "scraper", "standard.yaml"),
},
{
name: "SumMetric",
cfg: &Config{
MetricMetaData: map[string]MetricConfig{
"bytes.committed": {
Description: "number of bytes committed to memory",
Unit: "By",
Sum: SumMetric{Aggregation: "cumulative", Monotonic: true},
},
},
PerfCounters: []ObjectConfig{
{Object: "Memory", Counters: []CounterConfig{{Name: "Committed Bytes", MetricRep: MetricRep{Name: "bytes.committed"}}}},
},
ControllerConfig: scraperhelper.ControllerConfig{CollectionInterval: time.Minute, InitialDelay: time.Second},
},
expectedMetricPath: filepath.Join("testdata", "scraper", "sum_metric.yaml"),
},
{
name: "NoMetricDefinition",
cfg: &Config{
PerfCounters: []ObjectConfig{
{Object: "Memory", Counters: []CounterConfig{{Name: "Committed Bytes"}}},
},
ControllerConfig: scraperhelper.ControllerConfig{CollectionInterval: time.Minute, InitialDelay: time.Second},
},
expectedMetricPath: filepath.Join("testdata", "scraper", "no_metric_def.yaml"),
},
{
name: "InvalidCounter",
cfg: &Config{
PerfCounters: []ObjectConfig{
{
Object: "Memory",
Counters: []CounterConfig{{Name: "Committed Bytes", MetricRep: MetricRep{Name: "Committed Bytes"}}},
},
{
Object: "Invalid Object",
Counters: []CounterConfig{{Name: "Invalid Counter", MetricRep: MetricRep{Name: "invalid"}}},
},
},
ControllerConfig: scraperhelper.ControllerConfig{CollectionInterval: time.Minute, InitialDelay: time.Second},
},
startMessage: "some performance counters could not be initialized",
startErr: "failed to create perf counter with path \\Invalid Object\\Invalid Counter: The specified object was not found on the computer.\r\n",
},
{
name: "NoMatchingInstance",
cfg: &Config{
MetricMetaData: map[string]MetricConfig{
"no.matching.instance": {
Unit: "%",
Gauge: GaugeMetric{},
},
},
PerfCounters: []ObjectConfig{
{
Object: ".NET CLR Memory",
Instances: []string{"NoMatchingInstance*"},
Counters: []CounterConfig{{Name: "% Time in GC", MetricRep: MetricRep{Name: "no.matching.instance"}}},
},
},
ControllerConfig: scraperhelper.ControllerConfig{CollectionInterval: time.Minute, InitialDelay: time.Second},
},
expectedMetricPath: filepath.Join("testdata", "scraper", "no_matching_instance.yaml"),
},
{
name: "MetricDefinedButNoScrapedValue",
cfg: &Config{
MetricMetaData: map[string]MetricConfig{
"cpu.idle": {
Description: "percentage of time CPU is idle.",
Unit: "%",
Gauge: GaugeMetric{},
},
"no.counter": {
Description: "there is no counter or data for this metric",
Unit: "By",
Gauge: GaugeMetric{},
},
},
PerfCounters: []ObjectConfig{
{Object: "Processor", Instances: []string{"_Total"}, Counters: []CounterConfig{{Name: "% Idle Time", MetricRep: MetricRep{Name: "cpu.idle"}}}},
},
ControllerConfig: scraperhelper.ControllerConfig{CollectionInterval: time.Minute, InitialDelay: time.Second},
},
expectedMetricPath: filepath.Join("testdata", "scraper", "metric_not_scraped.yaml"),
},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
cfg := test.cfg
if cfg == nil {
cfg = defaultConfig
}
core, obs := observer.New(zapcore.WarnLevel)
logger := zap.New(core)
settings := componenttest.NewNopTelemetrySettings()
settings.Logger = logger
scraper := newScraper(cfg, settings)
err := scraper.start(context.Background(), componenttest.NewNopHost())
if test.startErr == "" {
require.Equal(t, 0, obs.Len())
} else {
require.Equal(t, 1, obs.Len())
log := obs.All()[0]
assert.Equal(t, zapcore.WarnLevel, log.Level)
assert.Equal(t, test.startMessage, log.Message)
assert.Equal(t, "error", log.Context[0].Key)
assert.EqualError(t, log.Context[0].Interface.(error), test.startErr)
return
}
require.NoError(t, err)
actualMetrics, err := scraper.scrape(context.Background())
require.NoError(t, err)
err = scraper.shutdown(context.Background())
require.NoError(t, err)
expectedMetrics, err := golden.ReadMetrics(test.expectedMetricPath)
require.NoError(t, err)
require.NoError(t, pmetrictest.CompareMetrics(expectedMetrics, actualMetrics,
// Scraping test host means static values, timestamps and instance counts are unreliable. ScopeMetrics order is also unpredictable.
// The check only takes the first instance of multi-instance counters and assumes that the other instances would be included.
pmetrictest.IgnoreSubsequentDataPoints("cpu.idle"),
pmetrictest.IgnoreSubsequentDataPoints("processor.time"),
pmetrictest.IgnoreMetricsOrder(),
pmetrictest.IgnoreScopeMetricsOrder(),
pmetrictest.IgnoreResourceMetricsOrder(),
pmetrictest.IgnoreMetricValues(),
pmetrictest.IgnoreTimestamp(),
))
})
}
}
func TestInitWatchers(t *testing.T) {
testCases := []struct {
name string
cfgs []ObjectConfig
expectedErr string
expectedPaths []string
}{
{
name: "basicPath",
cfgs: []ObjectConfig{
{
Object: "Memory",
Counters: []CounterConfig{{Name: "Committed Bytes"}},
},
},
expectedPaths: []string{"\\Memory\\Committed Bytes"},
},
{
name: "multiplePaths",
cfgs: []ObjectConfig{
{
Object: "Memory",
Counters: []CounterConfig{{Name: "Committed Bytes"}},
},
{
Object: "Memory",
Counters: []CounterConfig{{Name: "Available Bytes"}},
},
},
expectedPaths: []string{"\\Memory\\Committed Bytes", "\\Memory\\Available Bytes"},
},
{
name: "multipleIndividualCounters",
cfgs: []ObjectConfig{
{
Object: "Memory",
Counters: []CounterConfig{
{Name: "Committed Bytes"},
{Name: "Available Bytes"},
},
},
{
Object: "Memory",
Counters: []CounterConfig{},
},
},
expectedPaths: []string{"\\Memory\\Committed Bytes", "\\Memory\\Available Bytes"},
},
{
name: "invalidCounter",
cfgs: []ObjectConfig{
{
Object: "Broken",
Counters: []CounterConfig{{Name: "Broken Counter"}},
},
},
expectedErr: "failed to create perf counter with path \\Broken\\Broken Counter: The specified object was not found on the computer.\r\n",
},
{
name: "multipleInvalidCounters",
cfgs: []ObjectConfig{
{
Object: "Broken",
Counters: []CounterConfig{{Name: "Broken Counter"}},
},
{
Object: "Broken part 2",
Counters: []CounterConfig{{Name: "Broken again"}},
},
},
expectedErr: "failed to create perf counter with path \\Broken\\Broken Counter: The specified object was not found on the computer.\r\n; failed to create perf counter with path \\Broken part 2\\Broken again: The specified object was not found on the computer.\r\n",
},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
s := &scraper{cfg: &Config{PerfCounters: test.cfgs}, newWatcher: winperfcounters.NewWatcher}
watchers, errs := s.initWatchers()
if test.expectedErr != "" {
require.EqualError(t, errs, test.expectedErr)
} else {
require.NoError(t, errs)
}
for i, watcher := range watchers {
require.Equal(t, test.expectedPaths[i], watcher.Path())
}
})
}
}
func TestWatcherResetFailure(t *testing.T) {
const errMsg string = "failed to reset watcher"
mpc := mockPerfCounter{
counterValues: []winperfcounters.CounterValue{{Value: 1.0}},
resetErr: errors.New(errMsg),
}
cfg := Config{
PerfCounters: []ObjectConfig{
{
Counters: []CounterConfig{
{
MetricRep: MetricRep{
Name: "metric",
},
RecreateQuery: true,
},
},
},
},
MetricMetaData: map[string]MetricConfig{
"metric": {Description: "description", Unit: "1"},
},
}
core, _ := observer.New(zapcore.WarnLevel)
logger := zap.New(core)
settings := componenttest.NewNopTelemetrySettings()
settings.Logger = logger
s := &scraper{cfg: &cfg, settings: settings, newWatcher: mockPerfCounterFactoryInvocations(mpc)}
errs := s.start(context.Background(), componenttest.NewNopHost())
require.NoError(t, errs)
vals, err := s.scrape(context.Background())
if assert.Error(t, err) {
assert.Equal(t, errMsg, err.Error())
}
assert.NotEmpty(t, vals) // Still attempts scraping using previous query
}
func TestScrape(t *testing.T) {
testCases := []struct {
name string
cfg Config
mockPerfCounters []mockPerfCounter
}{
{
name: "metricsWithoutInstance",
cfg: Config{
PerfCounters: []ObjectConfig{
{
Counters: []CounterConfig{
{
MetricRep: MetricRep{
Name: "metric1",
},
},
{
MetricRep: MetricRep{
Name: "metric2",
Attributes: map[string]string{
"test.attribute": "test-value",
},
},
},
},
},
},
MetricMetaData: map[string]MetricConfig{
"metric1": {Description: "metric1 description", Unit: "1"},
"metric2": {Description: "metric2 description", Unit: "2"},
},
},
mockPerfCounters: []mockPerfCounter{
{counterValues: []winperfcounters.CounterValue{{Value: 1.0}}},
{counterValues: []winperfcounters.CounterValue{{Value: 2.0}}},
},
},
{
name: "metricsWithInstance",
cfg: Config{
PerfCounters: []ObjectConfig{
{
Counters: []CounterConfig{
{
MetricRep: MetricRep{
Name: "metric1",
},
},
{
MetricRep: MetricRep{
Name: "metric2",
Attributes: map[string]string{
"test.attribute": "test-value",
},
},
},
},
},
},
MetricMetaData: map[string]MetricConfig{
"metric1": {Description: "metric1 description", Unit: "1"},
"metric2": {Description: "metric2 description", Unit: "2"},
},
},
mockPerfCounters: []mockPerfCounter{
{counterValues: []winperfcounters.CounterValue{{InstanceName: "Test Instance", Value: 1.0}}},
{counterValues: []winperfcounters.CounterValue{{InstanceName: "Test Instance", Value: 2.0}}},
},
},
{
name: "metricsWithSingleCounterFailure",
cfg: Config{
PerfCounters: []ObjectConfig{
{
Counters: []CounterConfig{
{
MetricRep: MetricRep{
Name: "metric1",
},
},
{
MetricRep: MetricRep{
Name: "metric2",
Attributes: map[string]string{
"test.attribute": "test-value",
},
},
},
{
MetricRep: MetricRep{
Name: "metric3",
},
},
},
},
},
MetricMetaData: map[string]MetricConfig{
"metric1": {Description: "metric1 description", Unit: "1"},
"metric2": {Description: "metric2 description", Unit: "2"},
"metric3": {Description: "metric3 description", Unit: "3"},
},
},
mockPerfCounters: []mockPerfCounter{
{counterValues: []winperfcounters.CounterValue{{InstanceName: "Test Instance", Value: 1.0}}},
{scrapeErr: errors.New("unable to scrape metric 2")},
{scrapeErr: errors.New("unable to scrape metric 3")},
},
},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
mpcs := test.mockPerfCounters
testConfig := test.cfg
s := &scraper{cfg: &testConfig, newWatcher: mockPerfCounterFactoryInvocations(mpcs...)}
errs := s.start(context.Background(), componenttest.NewNopHost())
require.NoError(t, errs)
var expectedErrors []error
for _, mpc := range test.mockPerfCounters {
if mpc.scrapeErr != nil {
expectedErrors = append(expectedErrors, mpc.scrapeErr)
}
}
m, err := s.scrape(context.Background())
if len(expectedErrors) != 0 {
var partialErr scrapererror.PartialScrapeError
require.ErrorAs(t, err, &partialErr)
require.Equal(t, len(expectedErrors), partialErr.Failed)
expectedError := multierr.Combine(expectedErrors...)
require.Equal(t, expectedError.Error(), partialErr.Error())
} else {
require.NoError(t, err)
}
require.Equal(t, 1, m.ResourceMetrics().Len())
require.Equal(t, 1, m.ResourceMetrics().At(0).ScopeMetrics().Len())
metrics := m.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics()
metrics.Sort(func(a, b pmetric.Metric) bool {
return a.Name() < b.Name()
})
assert.Equal(t, len(test.mockPerfCounters)-len(expectedErrors), metrics.Len())
curMetricsNum := 0
for _, pc := range test.cfg.PerfCounters {
for counterIdx, counterCfg := range pc.Counters {
counterValues := test.mockPerfCounters[counterIdx].counterValues
scrapeErr := test.mockPerfCounters[counterIdx].scrapeErr
if scrapeErr != nil {
require.Empty(t, counterValues, "Invalid test case. Scrape error and counter values simultaneously.")
continue // no data for this counter.
}
metric := metrics.At(curMetricsNum)
assert.Equal(t, counterCfg.MetricRep.Name, metric.Name())
metricData := test.cfg.MetricMetaData[counterCfg.MetricRep.Name]
assert.Equal(t, metricData.Description, metric.Description())
assert.Equal(t, metricData.Unit, metric.Unit())
dps := metric.Gauge().DataPoints()
assert.Equal(t, len(counterValues), dps.Len())
for dpIdx, val := range counterValues {
assert.Equal(t, val.Value, dps.At(dpIdx).DoubleValue())
expectedAttributeLen := len(counterCfg.MetricRep.Attributes)
if val.InstanceName != "" {
expectedAttributeLen++
}
assert.Equal(t, expectedAttributeLen, dps.At(dpIdx).Attributes().Len())
dps.At(dpIdx).Attributes().Range(func(k string, v pcommon.Value) bool {
if k == instanceLabelName {
assert.Equal(t, val.InstanceName, v.Str())
return true
}
assert.Equal(t, counterCfg.MetricRep.Attributes[k], v.Str())
return true
})
}
curMetricsNum++
}
}
})
}
}