forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
221 lines (181 loc) · 5.97 KB
/
metrics.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
// +build darwin linux windows
// +build cgo
package instance
import (
"fmt"
"os"
"runtime"
"time"
"github.com/satori/go.uuid"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/metric/system/cpu"
"github.com/elastic/beats/libbeat/metric/system/process"
"github.com/elastic/beats/libbeat/monitoring"
"github.com/elastic/beats/libbeat/monitoring/report/log"
)
var (
beatProcessStats *process.Stats
ephemeralID uuid.UUID
)
func init() {
beatMetrics := monitoring.Default.NewRegistry("beat")
monitoring.NewFunc(beatMetrics, "memstats", reportMemStats, monitoring.Report)
monitoring.NewFunc(beatMetrics, "cpu", reportBeatCPU, monitoring.Report)
monitoring.NewFunc(beatMetrics, "info", reportInfo, monitoring.Report)
systemMetrics := monitoring.Default.NewRegistry("system")
monitoring.NewFunc(systemMetrics, "cpu", reportSystemCPUUsage, monitoring.Report)
if runtime.GOOS != "windows" {
monitoring.NewFunc(systemMetrics, "load", reportSystemLoadAverage, monitoring.Report)
}
ephemeralID = uuid.NewV4()
}
func setupMetrics(name string) error {
beatProcessStats = &process.Stats{
Procs: []string{name},
EnvWhitelist: nil,
CpuTicks: true,
CacheCmdLine: true,
IncludeTop: process.IncludeTopConfig{},
}
err := beatProcessStats.Init()
return err
}
func reportMemStats(m monitoring.Mode, V monitoring.Visitor) {
var stats runtime.MemStats
runtime.ReadMemStats(&stats)
V.OnRegistryStart()
defer V.OnRegistryFinished()
monitoring.ReportInt(V, "memory_total", int64(stats.TotalAlloc))
if m == monitoring.Full {
monitoring.ReportInt(V, "memory_alloc", int64(stats.Alloc))
monitoring.ReportInt(V, "gc_next", int64(stats.NextGC))
}
rss, err := getRSSSize()
if err != nil {
logp.Err("Error while getting memory usage: %v", err)
return
}
monitoring.ReportInt(V, "rss", int64(rss))
}
func getRSSSize() (uint64, error) {
beatPID := os.Getpid()
state, err := beatProcessStats.GetOne(beatPID)
if err != nil {
return 0, fmt.Errorf("error retrieving process stats")
}
iRss, err := state.GetValue("memory.rss.bytes")
if err != nil {
return 0, fmt.Errorf("error getting Resident Set Size: %v", err)
}
rss, ok := iRss.(uint64)
if !ok {
return 0, fmt.Errorf("error converting Resident Set Size: %v", err)
}
return rss, nil
}
func reportInfo(_ monitoring.Mode, V monitoring.Visitor) {
V.OnRegistryStart()
defer V.OnRegistryFinished()
delta := time.Since(log.StartTime)
uptime := int64(delta / time.Millisecond)
monitoring.ReportNamespace(V, "uptime", func() {
monitoring.ReportInt(V, "ms", uptime)
})
monitoring.ReportString(V, "ephemeral_id", ephemeralID.String())
}
func reportBeatCPU(_ monitoring.Mode, V monitoring.Visitor) {
V.OnRegistryStart()
defer V.OnRegistryFinished()
totalCPUUsage, cpuTicks, err := getCPUUsage()
if err != nil {
logp.Err("Error retrieving CPU percentages: %v", err)
return
}
userTime, systemTime, err := process.GetOwnResourceUsageTimeInMillis()
if err != nil {
logp.Err("Error retrieving CPU usage time: %v", err)
return
}
monitoring.ReportNamespace(V, "user", func() {
monitoring.ReportInt(V, "ticks", int64(cpuTicks.User))
monitoring.ReportInt(V, "time", userTime)
})
monitoring.ReportNamespace(V, "system", func() {
monitoring.ReportInt(V, "ticks", int64(cpuTicks.System))
monitoring.ReportInt(V, "time", systemTime)
})
monitoring.ReportNamespace(V, "total", func() {
monitoring.ReportFloat(V, "value", totalCPUUsage)
monitoring.ReportInt(V, "ticks", int64(cpuTicks.Total))
monitoring.ReportInt(V, "time", userTime+systemTime)
})
}
func getCPUUsage() (float64, *process.Ticks, error) {
beatPID := os.Getpid()
state, err := beatProcessStats.GetOne(beatPID)
if err != nil {
return 0.0, nil, fmt.Errorf("error retrieving process stats")
}
iTotalCPUUsage, err := state.GetValue("cpu.total.value")
if err != nil {
return 0.0, nil, fmt.Errorf("error getting total CPU since start: %v", err)
}
totalCPUUsage, ok := iTotalCPUUsage.(float64)
if !ok {
return 0.0, nil, fmt.Errorf("error converting value of CPU usage since start")
}
iTotalCPUUserTicks, err := state.GetValue("cpu.user.ticks")
if err != nil {
return 0.0, nil, fmt.Errorf("error getting number of user CPU ticks since start: %v", err)
}
totalCPUUserTicks, ok := iTotalCPUUserTicks.(uint64)
if !ok {
return 0.0, nil, fmt.Errorf("error converting value of user CPU ticks since start")
}
iTotalCPUSystemTicks, err := state.GetValue("cpu.system.ticks")
if err != nil {
return 0.0, nil, fmt.Errorf("error getting number of system CPU ticks since start: %v", err)
}
totalCPUSystemTicks, ok := iTotalCPUSystemTicks.(uint64)
if !ok {
return 0.0, nil, fmt.Errorf("error converting value of system CPU ticks since start")
}
iTotalCPUTicks, err := state.GetValue("cpu.total.ticks")
if err != nil {
return 0.0, nil, fmt.Errorf("error getting total number of CPU ticks since start: %v", err)
}
totalCPUTicks, ok := iTotalCPUTicks.(uint64)
if !ok {
return 0.0, nil, fmt.Errorf("error converting total value of CPU ticks since start")
}
p := process.Ticks{
User: totalCPUUserTicks,
System: totalCPUSystemTicks,
Total: totalCPUTicks,
}
return totalCPUUsage, &p, nil
}
func reportSystemLoadAverage(_ monitoring.Mode, V monitoring.Visitor) {
V.OnRegistryStart()
defer V.OnRegistryFinished()
load, err := cpu.Load()
if err != nil {
logp.Err("Error retrieving load average: %v", err)
return
}
avgs := load.Averages()
monitoring.ReportFloat(V, "1", avgs.OneMinute)
monitoring.ReportFloat(V, "5", avgs.FiveMinute)
monitoring.ReportFloat(V, "15", avgs.FifteenMinute)
normAvgs := load.NormalizedAverages()
monitoring.ReportNamespace(V, "norm", func() {
monitoring.ReportFloat(V, "1", normAvgs.OneMinute)
monitoring.ReportFloat(V, "5", normAvgs.FiveMinute)
monitoring.ReportFloat(V, "15", normAvgs.FifteenMinute)
})
}
func reportSystemCPUUsage(_ monitoring.Mode, V monitoring.Visitor) {
V.OnRegistryStart()
defer V.OnRegistryFinished()
monitoring.ReportInt(V, "cores", int64(process.NumCPU))
}