forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprocess.go
60 lines (50 loc) · 1.25 KB
/
process.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
// +build darwin linux windows
package process
import (
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/topbeat/system"
"github.com/pkg/errors"
)
func init() {
if err := mb.Registry.AddMetricSet("system", "process", New); err != nil {
panic(err)
}
}
// MetricSet that fetches process metrics.
type MetricSet struct {
mb.BaseMetricSet
stats *system.ProcStats
}
// New creates and returns a new MetricSet.
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
config := struct {
Procs []string `config:"processes"` // collect all processes by default
}{
Procs: []string{".*"},
}
if err := base.Module().UnpackConfig(&config); err != nil {
return nil, err
}
m := &MetricSet{
BaseMetricSet: base,
stats: &system.ProcStats{
ProcStats: true,
Procs: config.Procs,
},
}
err := m.stats.InitProcStats()
if err != nil {
return nil, err
}
return m, nil
}
// Fetch fetches metrics for all processes. It iterates over each PID and
// collects process metadata, CPU metrics, and memory metrics.
func (m *MetricSet) Fetch() ([]common.MapStr, error) {
procs, err := m.stats.GetProcStats()
if err != nil {
return nil, errors.Wrap(err, "process stats")
}
return procs, err
}