-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
process_scraper.go
438 lines (346 loc) · 14.5 KB
/
process_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
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package processscraper // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/processscraper"
import (
"context"
"errors"
"fmt"
"runtime"
"time"
"github.com/shirou/gopsutil/v3/common"
"github.com/shirou/gopsutil/v3/process"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/scrapererror"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterset"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/processscraper/internal/handlecount"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/processscraper/internal/metadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/processscraper/ucal"
)
const (
cpuMetricsLen = 1
memoryMetricsLen = 2
memoryUtilizationMetricsLen = 1
diskMetricsLen = 1
pagingMetricsLen = 1
threadMetricsLen = 1
contextSwitchMetricsLen = 1
fileDescriptorMetricsLen = 1
handleMetricsLen = 1
signalMetricsLen = 1
metricsLen = cpuMetricsLen + memoryMetricsLen + diskMetricsLen + memoryUtilizationMetricsLen + pagingMetricsLen + threadMetricsLen + contextSwitchMetricsLen + fileDescriptorMetricsLen + signalMetricsLen
)
// scraper for Process Metrics
type scraper struct {
settings receiver.CreateSettings
config *Config
mb *metadata.MetricsBuilder
includeFS filterset.FilterSet
excludeFS filterset.FilterSet
scrapeProcessDelay time.Duration
ucals map[int32]*ucal.CPUUtilizationCalculator
// for mocking
getProcessCreateTime func(p processHandle, ctx context.Context) (int64, error)
getProcessHandles func(context.Context) (processHandles, error)
handleCountManager handlecount.Manager
}
// newProcessScraper creates a Process Scraper
func newProcessScraper(settings receiver.CreateSettings, cfg *Config) (*scraper, error) {
scraper := &scraper{
settings: settings,
config: cfg,
getProcessCreateTime: processHandle.CreateTimeWithContext,
getProcessHandles: getProcessHandlesInternal,
scrapeProcessDelay: cfg.ScrapeProcessDelay,
ucals: make(map[int32]*ucal.CPUUtilizationCalculator),
handleCountManager: handlecount.NewManager(),
}
var err error
if len(cfg.Include.Names) > 0 {
scraper.includeFS, err = filterset.CreateFilterSet(cfg.Include.Names, &cfg.Include.Config)
if err != nil {
return nil, fmt.Errorf("error creating process include filters: %w", err)
}
}
if len(cfg.Exclude.Names) > 0 {
scraper.excludeFS, err = filterset.CreateFilterSet(cfg.Exclude.Names, &cfg.Exclude.Config)
if err != nil {
return nil, fmt.Errorf("error creating process exclude filters: %w", err)
}
}
return scraper, nil
}
func (s *scraper) start(context.Context, component.Host) error {
s.mb = metadata.NewMetricsBuilder(s.config.MetricsBuilderConfig, s.settings)
return nil
}
func (s *scraper) scrape(ctx context.Context) (pmetric.Metrics, error) {
var errs scrapererror.ScrapeErrors
data, err := s.getProcessMetadata()
if err != nil {
var partialErr scrapererror.PartialScrapeError
if !errors.As(err, &partialErr) {
return pmetric.NewMetrics(), err
}
errs.AddPartial(partialErr.Failed, partialErr)
}
presentPIDs := make(map[int32]struct{}, len(data))
ctx = context.WithValue(ctx, common.EnvKey, s.config.EnvMap)
for _, md := range data {
presentPIDs[md.pid] = struct{}{}
now := pcommon.NewTimestampFromTime(time.Now())
if err = s.scrapeAndAppendCPUTimeMetric(ctx, now, md.handle, md.pid); err != nil {
errs.AddPartial(cpuMetricsLen, fmt.Errorf("error reading cpu times for process %q (pid %v): %w", md.executable.name, md.pid, err))
}
if err = s.scrapeAndAppendMemoryUsageMetrics(ctx, now, md.handle); err != nil {
errs.AddPartial(memoryMetricsLen, fmt.Errorf("error reading memory info for process %q (pid %v): %w", md.executable.name, md.pid, err))
}
if err = s.scrapeAndAppendMemoryUtilizationMetric(ctx, now, md.handle); err != nil {
errs.AddPartial(memoryUtilizationMetricsLen, fmt.Errorf("error reading memory utilization for process %q (pid %v): %w", md.executable.name, md.pid, err))
}
if err = s.scrapeAndAppendDiskMetrics(ctx, now, md.handle); err != nil && !s.config.MuteProcessIOError {
errs.AddPartial(diskMetricsLen, fmt.Errorf("error reading disk usage for process %q (pid %v): %w", md.executable.name, md.pid, err))
}
if err = s.scrapeAndAppendPagingMetric(ctx, now, md.handle); err != nil {
errs.AddPartial(pagingMetricsLen, fmt.Errorf("error reading memory paging info for process %q (pid %v): %w", md.executable.name, md.pid, err))
}
if err = s.scrapeAndAppendThreadsMetrics(ctx, now, md.handle); err != nil {
errs.AddPartial(threadMetricsLen, fmt.Errorf("error reading thread info for process %q (pid %v): %w", md.executable.name, md.pid, err))
}
if err = s.scrapeAndAppendContextSwitchMetrics(ctx, now, md.handle); err != nil {
errs.AddPartial(contextSwitchMetricsLen, fmt.Errorf("error reading context switch counts for process %q (pid %v): %w", md.executable.name, md.pid, err))
}
if err = s.scrapeAndAppendOpenFileDescriptorsMetric(ctx, now, md.handle); err != nil {
errs.AddPartial(fileDescriptorMetricsLen, fmt.Errorf("error reading open file descriptor count for process %q (pid %v): %w", md.executable.name, md.pid, err))
}
if err = s.scrapeAndAppendHandlesMetric(ctx, now, int64(md.pid)); err != nil {
errs.AddPartial(handleMetricsLen, fmt.Errorf("error reading handle count for process %q (pid %v): %w", md.executable.name, md.pid, err))
}
if err = s.scrapeAndAppendSignalsPendingMetric(ctx, now, md.handle); err != nil {
errs.AddPartial(signalMetricsLen, fmt.Errorf("error reading pending signals for process %q (pid %v): %w", md.executable.name, md.pid, err))
}
s.mb.EmitForResource(metadata.WithResource(md.buildResource(s.mb.NewResourceBuilder())),
metadata.WithStartTimeOverride(pcommon.Timestamp(md.createTime*1e6)))
}
// Cleanup any [ucal.CPUUtilizationCalculator]s for PIDs that are no longer present
for pid := range s.ucals {
if _, ok := presentPIDs[pid]; !ok {
delete(s.ucals, pid)
}
}
return s.mb.Emit(), errs.Combine()
}
// getProcessMetadata returns a slice of processMetadata, including handles,
// for all currently running processes. If errors occur obtaining information
// for some processes, an error will be returned, but any processes that were
// successfully obtained will still be returned.
func (s *scraper) getProcessMetadata() ([]*processMetadata, error) {
ctx := context.WithValue(context.Background(), common.EnvKey, s.config.EnvMap)
handles, err := s.getProcessHandles(ctx)
if err != nil {
return nil, err
}
var errs scrapererror.ScrapeErrors
if err := s.refreshHandleCounts(); err != nil {
errs.Add(err)
}
data := make([]*processMetadata, 0, handles.Len())
for i := 0; i < handles.Len(); i++ {
pid := handles.Pid(i)
handle := handles.At(i)
exe, err := getProcessExecutable(ctx, handle)
if err != nil {
if !s.config.MuteProcessExeError {
errs.AddPartial(1, fmt.Errorf("error reading process executable for pid %v: %w", pid, err))
}
}
name, err := getProcessName(ctx, handle, exe)
if err != nil {
if !s.config.MuteProcessNameError {
errs.AddPartial(1, fmt.Errorf("error reading process name for pid %v: %w", pid, err))
}
continue
}
executable := &executableMetadata{name: name, path: exe}
// filter processes by name
if (s.includeFS != nil && !s.includeFS.Matches(executable.name)) ||
(s.excludeFS != nil && s.excludeFS.Matches(executable.name)) {
continue
}
command, err := getProcessCommand(ctx, handle)
if err != nil {
errs.AddPartial(0, fmt.Errorf("error reading command for process %q (pid %v): %w", executable.name, pid, err))
}
username, err := handle.UsernameWithContext(ctx)
if err != nil {
if !s.config.MuteProcessUserError {
errs.AddPartial(0, fmt.Errorf("error reading username for process %q (pid %v): %w", executable.name, pid, err))
}
}
createTime, err := s.getProcessCreateTime(handle, ctx)
if err != nil {
errs.AddPartial(0, fmt.Errorf("error reading create time for process %q (pid %v): %w", executable.name, pid, err))
// set the start time to now to avoid including this when a scrape_process_delay is set
createTime = time.Now().UnixMilli()
}
if s.scrapeProcessDelay.Milliseconds() > (time.Now().UnixMilli() - createTime) {
continue
}
parentPid, err := parentPid(ctx, handle, pid)
if err != nil {
errs.AddPartial(0, fmt.Errorf("error reading parent pid for process %q (pid %v): %w", executable.name, pid, err))
}
md := &processMetadata{
pid: pid,
parentPid: parentPid,
executable: executable,
command: command,
username: username,
handle: handle,
createTime: createTime,
}
data = append(data, md)
}
return data, errs.Combine()
}
func (s *scraper) scrapeAndAppendCPUTimeMetric(ctx context.Context, now pcommon.Timestamp, handle processHandle, pid int32) error {
if !s.config.MetricsBuilderConfig.Metrics.ProcessCPUTime.Enabled && !s.config.MetricsBuilderConfig.Metrics.ProcessCPUUtilization.Enabled {
return nil
}
times, err := handle.TimesWithContext(ctx)
if err != nil {
return err
}
if s.config.MetricsBuilderConfig.Metrics.ProcessCPUTime.Enabled {
s.recordCPUTimeMetric(now, times)
}
if !s.config.MetricsBuilderConfig.Metrics.ProcessCPUUtilization.Enabled {
return nil
}
if _, ok := s.ucals[pid]; !ok {
s.ucals[pid] = &ucal.CPUUtilizationCalculator{}
}
err = s.ucals[pid].CalculateAndRecord(now, times, s.recordCPUUtilization)
return err
}
func (s *scraper) scrapeAndAppendMemoryUsageMetrics(ctx context.Context, now pcommon.Timestamp, handle processHandle) error {
if !(s.config.MetricsBuilderConfig.Metrics.ProcessMemoryUsage.Enabled || s.config.MetricsBuilderConfig.Metrics.ProcessMemoryVirtual.Enabled) {
return nil
}
mem, err := handle.MemoryInfoWithContext(ctx)
if err != nil {
return err
}
s.mb.RecordProcessMemoryUsageDataPoint(now, int64(mem.RSS))
s.mb.RecordProcessMemoryVirtualDataPoint(now, int64(mem.VMS))
return nil
}
func (s *scraper) scrapeAndAppendMemoryUtilizationMetric(ctx context.Context, now pcommon.Timestamp, handle processHandle) error {
if !s.config.MetricsBuilderConfig.Metrics.ProcessMemoryUtilization.Enabled {
return nil
}
memoryPercent, err := handle.MemoryPercentWithContext(ctx)
if err != nil {
return err
}
s.mb.RecordProcessMemoryUtilizationDataPoint(now, float64(memoryPercent))
return nil
}
func (s *scraper) scrapeAndAppendDiskMetrics(ctx context.Context, now pcommon.Timestamp, handle processHandle) error {
if !(s.config.MetricsBuilderConfig.Metrics.ProcessDiskIo.Enabled || s.config.MetricsBuilderConfig.Metrics.ProcessDiskOperations.Enabled) || runtime.GOOS == "darwin" {
return nil
}
io, err := handle.IOCountersWithContext(ctx)
if err != nil {
if s.config.MuteProcessIOError {
return nil
}
return err
}
s.mb.RecordProcessDiskIoDataPoint(now, int64(io.ReadBytes), metadata.AttributeDirectionRead)
s.mb.RecordProcessDiskIoDataPoint(now, int64(io.WriteBytes), metadata.AttributeDirectionWrite)
s.mb.RecordProcessDiskOperationsDataPoint(now, int64(io.ReadCount), metadata.AttributeDirectionRead)
s.mb.RecordProcessDiskOperationsDataPoint(now, int64(io.WriteCount), metadata.AttributeDirectionWrite)
return nil
}
func (s *scraper) scrapeAndAppendPagingMetric(ctx context.Context, now pcommon.Timestamp, handle processHandle) error {
if !s.config.MetricsBuilderConfig.Metrics.ProcessPagingFaults.Enabled {
return nil
}
pageFaultsStat, err := handle.PageFaultsWithContext(ctx)
if err != nil {
return err
}
s.mb.RecordProcessPagingFaultsDataPoint(now, int64(pageFaultsStat.MajorFaults), metadata.AttributePagingFaultTypeMajor)
s.mb.RecordProcessPagingFaultsDataPoint(now, int64(pageFaultsStat.MinorFaults), metadata.AttributePagingFaultTypeMinor)
return nil
}
func (s *scraper) scrapeAndAppendThreadsMetrics(ctx context.Context, now pcommon.Timestamp, handle processHandle) error {
if !s.config.MetricsBuilderConfig.Metrics.ProcessThreads.Enabled {
return nil
}
threads, err := handle.NumThreadsWithContext(ctx)
if err != nil {
return err
}
s.mb.RecordProcessThreadsDataPoint(now, int64(threads))
return nil
}
func (s *scraper) scrapeAndAppendContextSwitchMetrics(ctx context.Context, now pcommon.Timestamp, handle processHandle) error {
if !s.config.MetricsBuilderConfig.Metrics.ProcessContextSwitches.Enabled {
return nil
}
contextSwitches, err := handle.NumCtxSwitchesWithContext(ctx)
if err != nil {
return err
}
s.mb.RecordProcessContextSwitchesDataPoint(now, contextSwitches.Involuntary, metadata.AttributeContextSwitchTypeInvoluntary)
s.mb.RecordProcessContextSwitchesDataPoint(now, contextSwitches.Voluntary, metadata.AttributeContextSwitchTypeVoluntary)
return nil
}
func (s *scraper) scrapeAndAppendOpenFileDescriptorsMetric(ctx context.Context, now pcommon.Timestamp, handle processHandle) error {
if !s.config.MetricsBuilderConfig.Metrics.ProcessOpenFileDescriptors.Enabled {
return nil
}
fds, err := handle.NumFDsWithContext(ctx)
if err != nil {
return err
}
s.mb.RecordProcessOpenFileDescriptorsDataPoint(now, int64(fds))
return nil
}
func (s *scraper) refreshHandleCounts() error {
if !s.config.MetricsBuilderConfig.Metrics.ProcessHandles.Enabled {
return nil
}
return s.handleCountManager.Refresh()
}
func (s *scraper) scrapeAndAppendHandlesMetric(_ context.Context, now pcommon.Timestamp, pid int64) error {
if !s.config.MetricsBuilderConfig.Metrics.ProcessHandles.Enabled {
return nil
}
count, err := s.handleCountManager.GetProcessHandleCount(pid)
if err != nil {
return err
}
s.mb.RecordProcessHandlesDataPoint(now, int64(count))
return nil
}
func (s *scraper) scrapeAndAppendSignalsPendingMetric(ctx context.Context, now pcommon.Timestamp, handle processHandle) error {
if !s.config.MetricsBuilderConfig.Metrics.ProcessSignalsPending.Enabled {
return nil
}
rlimitStats, err := handle.RlimitUsageWithContext(ctx, true)
if err != nil {
return err
}
for _, rlimitStat := range rlimitStats {
if rlimitStat.Resource == process.RLIMIT_SIGPENDING {
s.mb.RecordProcessSignalsPendingDataPoint(now, int64(rlimitStat.Used))
break
}
}
return nil
}