-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathwindowsperfcounters_scraper.go
217 lines (178 loc) · 5.74 KB
/
windowsperfcounters_scraper.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
//go:build windows
package windowsperfcountersreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/windowsperfcountersreceiver"
import (
"context"
"time"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/scraper/scrapererror"
"go.uber.org/multierr"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/winperfcounters"
)
const instanceLabelName = "instance"
type perfCounterMetricWatcher struct {
winperfcounters.PerfCounterWatcher
MetricRep
recreate bool
}
type newWatcherFunc func(string, string, string) (winperfcounters.PerfCounterWatcher, error)
// windowsPerfCountersScraper is the type that scrapes various host metrics.
type windowsPerfCountersScraper struct {
cfg *Config
settings component.TelemetrySettings
watchers []perfCounterMetricWatcher
// for mocking
newWatcher newWatcherFunc
}
func newScraper(cfg *Config, settings component.TelemetrySettings) *windowsPerfCountersScraper {
return &windowsPerfCountersScraper{cfg: cfg, settings: settings, newWatcher: winperfcounters.NewWatcher}
}
func (s *windowsPerfCountersScraper) start(context.Context, component.Host) error {
watchers, err := s.initWatchers()
if err != nil {
s.settings.Logger.Warn("some performance counters could not be initialized", zap.Error(err))
}
s.watchers = watchers
return nil
}
func (s *windowsPerfCountersScraper) initWatchers() ([]perfCounterMetricWatcher, error) {
var errs error
var watchers []perfCounterMetricWatcher
for _, objCfg := range s.cfg.PerfCounters {
for _, instance := range instancesFromConfig(objCfg) {
for _, counterCfg := range objCfg.Counters {
pcw, err := s.newWatcher(objCfg.Object, instance, counterCfg.Name)
if err != nil {
errs = multierr.Append(errs, err)
continue
}
watcher := perfCounterMetricWatcher{
PerfCounterWatcher: pcw,
MetricRep: MetricRep{Name: pcw.Path()},
recreate: counterCfg.RecreateQuery,
}
if counterCfg.MetricRep.Name != "" {
watcher.MetricRep.Name = counterCfg.MetricRep.Name
if counterCfg.MetricRep.Attributes != nil {
watcher.MetricRep.Attributes = counterCfg.MetricRep.Attributes
}
}
watchers = append(watchers, watcher)
}
}
}
return watchers, errs
}
func (s *windowsPerfCountersScraper) shutdown(context.Context) error {
var errs error
for _, watcher := range s.watchers {
err := watcher.Close()
if err != nil {
errs = multierr.Append(errs, err)
}
}
return errs
}
func (s *windowsPerfCountersScraper) scrape(context.Context) (pmetric.Metrics, error) {
md := pmetric.NewMetrics()
metricSlice := md.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics()
now := pcommon.NewTimestampFromTime(time.Now())
var errs error
metricSlice.EnsureCapacity(len(s.watchers))
metrics := map[string]pmetric.Metric{}
for name, metricCfg := range s.cfg.MetricMetaData {
builtMetric := metricSlice.AppendEmpty()
builtMetric.SetName(name)
builtMetric.SetDescription(metricCfg.Description)
builtMetric.SetUnit(metricCfg.Unit)
if (metricCfg.Sum != SumMetric{}) {
builtMetric.SetEmptySum().SetIsMonotonic(metricCfg.Sum.Monotonic)
switch metricCfg.Sum.Aggregation {
case "cumulative":
builtMetric.Sum().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
case "delta":
builtMetric.Sum().SetAggregationTemporality(pmetric.AggregationTemporalityDelta)
}
} else {
builtMetric.SetEmptyGauge()
}
metrics[name] = builtMetric
}
scrapeFailures := 0
for _, watcher := range s.watchers {
counterVals, err := watcher.ScrapeData()
if err != nil {
errs = multierr.Append(errs, err)
scrapeFailures++
continue
}
if watcher.recreate {
err := watcher.Reset()
if err != nil {
errs = multierr.Append(errs, err)
}
}
for _, val := range counterVals {
var metric pmetric.Metric
if builtmetric, ok := metrics[watcher.MetricRep.Name]; ok {
metric = builtmetric
} else {
metric = metricSlice.AppendEmpty()
metric.SetName(watcher.MetricRep.Name)
metric.SetUnit("1")
metric.SetEmptyGauge()
}
initializeMetricDps(metric, now, val, watcher.MetricRep.Attributes)
}
}
// Drop metrics with no datapoints. This happens when configured counters don't exist on the host.
// This may result in a Metrics message with no metrics if all counters are missing.
metricSlice.RemoveIf(func(m pmetric.Metric) bool {
switch m.Type() {
case pmetric.MetricTypeGauge:
return m.Gauge().DataPoints().Len() == 0
case pmetric.MetricTypeSum:
return m.Sum().DataPoints().Len() == 0
default:
return false
}
})
if scrapeFailures != 0 && scrapeFailures != len(s.watchers) {
errs = scrapererror.NewPartialScrapeError(errs, scrapeFailures)
}
return md, errs
}
func initializeMetricDps(metric pmetric.Metric, now pcommon.Timestamp, counterValue winperfcounters.CounterValue,
attributes map[string]string,
) {
var dps pmetric.NumberDataPointSlice
if metric.Type() == pmetric.MetricTypeGauge {
dps = metric.Gauge().DataPoints()
} else {
dps = metric.Sum().DataPoints()
}
dp := dps.AppendEmpty()
if counterValue.InstanceName != "" {
dp.Attributes().PutStr(instanceLabelName, counterValue.InstanceName)
}
for attKey, attVal := range attributes {
dp.Attributes().PutStr(attKey, attVal)
}
dp.SetTimestamp(now)
dp.SetDoubleValue(counterValue.Value)
}
func instancesFromConfig(oc ObjectConfig) []string {
if len(oc.Instances) == 0 {
return []string{""}
}
for _, instance := range oc.Instances {
if instance == "*" {
return []string{"*"}
}
}
return oc.Instances
}