-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathscraper.go
287 lines (242 loc) · 9.52 KB
/
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
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
//go:build windows
package iisreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/iisreceiver"
import (
"context"
"fmt"
"regexp"
"strings"
"time"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/scraper/scrapererror"
"go.uber.org/multierr"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/winperfcounters"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/iisreceiver/internal/metadata"
)
type iisReceiver struct {
params component.TelemetrySettings
config *Config
consumer consumer.Metrics
totalWatcherRecorders []watcherRecorder
siteWatcherRecorders []watcherRecorder
appPoolWatcherRecorders []watcherRecorder
queueMaxAgeWatchers []instanceWatcher
rb *metadata.ResourceBuilder
mb *metadata.MetricsBuilder
// for mocking
newWatcher func(string, string, string) (winperfcounters.PerfCounterWatcher, error)
newWatcherFromPath func(string) (winperfcounters.PerfCounterWatcher, error)
expandWildcardPath func(string) ([]string, error)
}
// watcherRecorder is a struct containing perf counter watcher along with corresponding value recorder.
type watcherRecorder struct {
watcher winperfcounters.PerfCounterWatcher
recorder recordFunc
}
// instanceWatcher is a struct containing a perf counter watcher, along with the single instance the watcher records.
type instanceWatcher struct {
watcher winperfcounters.PerfCounterWatcher
instance string
}
// newIisReceiver returns an iisReceiver
func newIisReceiver(settings receiver.Settings, cfg *Config, consumer consumer.Metrics) *iisReceiver {
return &iisReceiver{
params: settings.TelemetrySettings,
config: cfg,
consumer: consumer,
rb: metadata.NewResourceBuilder(cfg.ResourceAttributes),
mb: metadata.NewMetricsBuilder(cfg.MetricsBuilderConfig, settings),
newWatcher: winperfcounters.NewWatcher,
newWatcherFromPath: winperfcounters.NewWatcherFromPath,
expandWildcardPath: winperfcounters.ExpandWildCardPath,
}
}
// start builds the paths to the watchers
func (rcvr *iisReceiver) start(_ context.Context, _ component.Host) error {
errs := &scrapererror.ScrapeErrors{}
rcvr.totalWatcherRecorders = rcvr.buildWatcherRecorders(totalPerfCounterRecorders, errs)
rcvr.siteWatcherRecorders = rcvr.buildWatcherRecorders(sitePerfCounterRecorders, errs)
rcvr.appPoolWatcherRecorders = rcvr.buildWatcherRecorders(appPoolPerfCounterRecorders, errs)
rcvr.queueMaxAgeWatchers = rcvr.buildMaxQueueItemAgeWatchers(errs)
return errs.Combine()
}
// scrape pulls counter values from the watchers
func (rcvr *iisReceiver) scrape(_ context.Context) (pmetric.Metrics, error) {
var errs error
now := pcommon.NewTimestampFromTime(time.Now())
// Maintain maps of site -> {val, recordFunc} and app -> {val, recordFunc}
// so that we can emit all metrics for a particular instance (site or app_pool) at once,
// keeping them in a single resource metric.
siteToRecorders := map[string][]valRecorder{}
rcvr.scrapeInstanceMetrics(rcvr.siteWatcherRecorders, siteToRecorders)
rcvr.emitInstanceMap(now, siteToRecorders, rcvr.rb.SetIisSite)
appToRecorders := map[string][]valRecorder{}
rcvr.scrapeInstanceMetrics(rcvr.appPoolWatcherRecorders, appToRecorders)
rcvr.scrapeMaxQueueAgeMetrics(appToRecorders)
rcvr.emitInstanceMap(now, appToRecorders, rcvr.rb.SetIisApplicationPool)
rcvr.scrapeTotalMetrics(now)
return rcvr.mb.Emit(), errs
}
func (rcvr *iisReceiver) scrapeTotalMetrics(now pcommon.Timestamp) {
for _, wr := range rcvr.totalWatcherRecorders {
counterValues, err := wr.watcher.ScrapeData()
if err != nil {
rcvr.params.Logger.Warn("some performance counters could not be scraped; ", zap.Error(err))
continue
}
value := 0.0
for _, counterValue := range counterValues {
value += counterValue.Value
}
wr.recorder(rcvr.mb, now, value)
}
// resource for total metrics is empty
// this makes it so that the order that the scrape functions are called doesn't matter
rcvr.mb.EmitForResource()
}
type valRecorder struct {
val float64
record recordFunc
}
func (rcvr *iisReceiver) scrapeInstanceMetrics(wrs []watcherRecorder, instanceToRecorders map[string][]valRecorder) {
for _, wr := range wrs {
counterValues, err := wr.watcher.ScrapeData()
if err != nil {
rcvr.params.Logger.Warn("some performance counters could not be scraped; ", zap.Error(err))
continue
}
// This avoids recording the _Total instance.
// The _Total instance may be the only instance, because some instances require elevated permissions
// to list and scrape. In these cases, the per-instance metric is not available, and should not be recorded.
if len(counterValues) == 1 && counterValues[0].InstanceName == "" {
rcvr.params.Logger.Warn("Performance counter was scraped, but only the _Total instance was available, skipping metric...", zap.String("path", wr.watcher.Path()))
continue
}
for _, cv := range counterValues {
instanceToRecorders[cv.InstanceName] = append(instanceToRecorders[cv.InstanceName],
valRecorder{
val: cv.Value,
record: wr.recorder,
})
}
}
}
var negativeDenominatorError = "A counter with a negative denominator value was detected.\r\n"
func (rcvr *iisReceiver) scrapeMaxQueueAgeMetrics(appToRecorders map[string][]valRecorder) {
for _, wr := range rcvr.queueMaxAgeWatchers {
counterValues, err := wr.watcher.ScrapeData()
var value float64
switch {
case err != nil && strings.HasSuffix(err.Error(), negativeDenominatorError):
// This error occurs when there are no items in the queue;
// in this case, we would like to emit a 0 instead of logging an error (this is an expected scenario).
value = 0
case err != nil:
rcvr.params.Logger.Warn("some performance counters could not be scraped; ", zap.Error(err))
continue
case len(counterValues) == 0:
// No counters scraped
continue
default:
value = counterValues[0].Value
}
appToRecorders[wr.instance] = append(appToRecorders[wr.instance],
valRecorder{
val: value,
record: recordMaxQueueItemAge,
})
}
}
// emitInstanceMap records all metrics for each instance, then emits them all as a single resource metric
func (rcvr *iisReceiver) emitInstanceMap(now pcommon.Timestamp, instanceToRecorders map[string][]valRecorder, resourceSetter func(string)) {
for instanceName, recorders := range instanceToRecorders {
for _, recorder := range recorders {
recorder.record(rcvr.mb, now, recorder.val)
}
resourceSetter(instanceName)
rcvr.mb.EmitForResource(metadata.WithResource(rcvr.rb.Emit()))
}
}
// shutdown closes the watchers
func (rcvr iisReceiver) shutdown(_ context.Context) error {
var errs error
errs = multierr.Append(errs, closeWatcherRecorders(rcvr.totalWatcherRecorders))
errs = multierr.Append(errs, closeWatcherRecorders(rcvr.siteWatcherRecorders))
errs = multierr.Append(errs, closeWatcherRecorders(rcvr.appPoolWatcherRecorders))
errs = multierr.Append(errs, closeInstanceWatchers(rcvr.queueMaxAgeWatchers))
return errs
}
func (rcvr *iisReceiver) buildWatcherRecorders(confs []perfCounterRecorderConf, scrapeErrors *scrapererror.ScrapeErrors) []watcherRecorder {
wrs := []watcherRecorder{}
for _, pcr := range confs {
for perfCounterName, recorder := range pcr.recorders {
w, err := rcvr.newWatcher(pcr.object, pcr.instance, perfCounterName)
if err != nil {
scrapeErrors.AddPartial(1, err)
continue
}
wrs = append(wrs, watcherRecorder{w, recorder})
}
}
return wrs
}
var maxQueueItemAgeInstanceRegex = regexp.MustCompile(`\\HTTP Service Request Queues\((?P<instance>[^)]+)\)\\MaxQueueItemAge$`)
// buildMaxQueueItemAgeWatchers builds a watcher for each individual instance of the MaxQueueItemAge counter.
// This is done in order to capture the error when scraping each individual instance, because we want to ignore
// negative denominator errors.
func (rcvr *iisReceiver) buildMaxQueueItemAgeWatchers(scrapeErrors *scrapererror.ScrapeErrors) []instanceWatcher {
wrs := []instanceWatcher{}
paths, err := rcvr.expandWildcardPath(`\HTTP Service Request Queues(*)\MaxQueueItemAge`)
if err != nil {
scrapeErrors.AddPartial(1, fmt.Errorf("failed to expand wildcard path for MaxQueueItemAge: %w", err))
return wrs
}
for _, path := range paths {
matches := maxQueueItemAgeInstanceRegex.FindStringSubmatch(path)
if len(matches) != 2 {
scrapeErrors.AddPartial(1, fmt.Errorf("failed to extract instance from %q", path))
continue
}
instanceName := matches[1]
if instanceName == "_Total" {
// skip total instance
continue
}
watcher, err := rcvr.newWatcherFromPath(path)
if err != nil {
scrapeErrors.AddPartial(1, fmt.Errorf("failed to create watcher from %q: %w", path, err))
continue
}
wrs = append(wrs, instanceWatcher{
instance: instanceName,
watcher: watcher,
})
}
return wrs
}
func closeWatcherRecorders(wrs []watcherRecorder) error {
var errs error
for _, wr := range wrs {
err := wr.watcher.Close()
if err != nil {
errs = multierr.Append(errs, err)
}
}
return errs
}
func closeInstanceWatchers(wrs []instanceWatcher) error {
var errs error
for _, wr := range wrs {
err := wr.watcher.Close()
if err != nil {
errs = multierr.Append(errs, err)
}
}
return errs
}