forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
122 lines (98 loc) · 3.38 KB
/
config.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package windowsperfcountersreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/windowsperfcountersreceiver"
import (
"fmt"
"go.opentelemetry.io/collector/receiver/scraperhelper"
"go.uber.org/multierr"
)
// Config defines configuration for WindowsPerfCounters receiver.
type Config struct {
scraperhelper.ControllerConfig `mapstructure:",squash"`
MetricMetaData map[string]MetricConfig `mapstructure:"metrics"`
PerfCounters []ObjectConfig `mapstructure:"perfcounters"`
}
// MetricsConfig defines the configuration for a metric to be created.
type MetricConfig struct {
Unit string `mapstructure:"unit"`
Description string `mapstructure:"description"`
Gauge GaugeMetric `mapstructure:"gauge"`
Sum SumMetric `mapstructure:"sum"`
}
type GaugeMetric struct {
}
type SumMetric struct {
Aggregation string `mapstructure:"aggregation"`
Monotonic bool `mapstructure:"monotonic"`
}
// ObjectConfig defines configuration for a perf counter object.
type ObjectConfig struct {
Object string `mapstructure:"object"`
Instances []string `mapstructure:"instances"`
Counters []CounterConfig `mapstructure:"counters"`
}
// CounterConfig defines the individual counter in an object.
type CounterConfig struct {
Name string `mapstructure:"name"`
MetricRep `mapstructure:",squash"`
}
type MetricRep struct {
Name string `mapstructure:"metric"`
Attributes map[string]string `mapstructure:"attributes"`
}
func (c *Config) Validate() error {
var errs error
if c.CollectionInterval <= 0 {
errs = multierr.Append(errs, fmt.Errorf("collection_interval must be a positive duration"))
}
if len(c.PerfCounters) == 0 {
errs = multierr.Append(errs, fmt.Errorf("must specify at least one perf counter"))
}
for name, metric := range c.MetricMetaData {
if metric.Unit == "" {
metric.Unit = "1"
}
if (metric.Sum != SumMetric{}) {
if (metric.Gauge != GaugeMetric{}) {
errs = multierr.Append(errs, fmt.Errorf("metric %q provides both a sum config and a gauge config", name))
}
if metric.Sum.Aggregation != "cumulative" && metric.Sum.Aggregation != "delta" {
errs = multierr.Append(errs, fmt.Errorf("sum metric %q includes an invalid aggregation", name))
}
}
}
var perfCounterMissingObjectName bool
for _, pc := range c.PerfCounters {
if pc.Object == "" {
perfCounterMissingObjectName = true
continue
}
if len(pc.Counters) == 0 {
errs = multierr.Append(errs, fmt.Errorf("perf counter for object %q does not specify any counters", pc.Object))
}
for _, counter := range pc.Counters {
if counter.MetricRep.Name == "" {
continue
}
foundMatchingMetric := false
for name := range c.MetricMetaData {
if counter.MetricRep.Name == name {
foundMatchingMetric = true
}
}
if !foundMatchingMetric {
errs = multierr.Append(errs, fmt.Errorf("perf counter for object %q includes an undefined metric", pc.Object))
}
}
for _, instance := range pc.Instances {
if instance == "" {
errs = multierr.Append(errs, fmt.Errorf("perf counter for object %q includes an empty instance", pc.Object))
break
}
}
}
if perfCounterMissingObjectName {
errs = multierr.Append(errs, fmt.Errorf("must specify object name for all perf counters"))
}
return errs
}