forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
199 lines (170 loc) · 4.88 KB
/
log.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
package log
import (
"sync"
"time"
"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/monitoring"
"github.com/elastic/beats/libbeat/monitoring/report"
)
// List of metrics that are gauges. This is used to identify metrics that should
// not be reported as deltas. Instead we log the raw value if there was any
// observable change during the interval.
//
// TODO: Replace this with a proper solution that uses the metric type from
// where it is defined. See: https://github.com/elastic/beats/issues/5433
var gauges = map[string]bool{
"libbeat.pipeline.events.active": true,
"libbeat.pipeline.clients": true,
"libbeat.config.module.running": true,
"registrar.states.current": true,
"filebeat.harvester.running": true,
"filebeat.harvester.open_files": true,
"beat.memstats.memory_total": true,
"beat.memstats.memory_alloc": true,
"beat.memstats.gc_next": true,
"beat.info.uptime.ms": true,
"beat.cpu.user.ticks": true,
"beat.cpu.user.time": true,
"beat.cpu.system.ticks": true,
"beat.cpu.system.time": true,
"beat.cpu.total.value": true,
"beat.cpu.total.ticks": true,
"beat.cpu.total.time": true,
"system.load.1": true,
"system.load.5": true,
"system.load.15": true,
"system.load.norm.1": true,
"system.load.norm.5": true,
"system.load.norm.15": true,
}
// TODO: Change this when gauges are refactored, too.
var strConsts = map[string]bool{
"beat.info.ephemeral_id": true,
}
var (
// StartTime is the time that the process was started.
StartTime = time.Now()
)
type reporter struct {
wg sync.WaitGroup
done chan struct{}
period time.Duration
registry *monitoring.Registry
// output
logger *logp.Logger
}
// MakeReporter returns a new Reporter that periodically reports metrics via
// logp. If cfg is nil defaults will be used.
func MakeReporter(beat beat.Info, cfg *common.Config) (report.Reporter, error) {
config := defaultConfig
if cfg != nil {
if err := cfg.Unpack(&config); err != nil {
return nil, err
}
}
r := &reporter{
done: make(chan struct{}),
period: config.Period,
logger: logp.NewLogger("monitoring"),
registry: monitoring.Default,
}
r.wg.Add(1)
go func() {
defer r.wg.Done()
r.snapshotLoop()
}()
return r, nil
}
func (r *reporter) Stop() {
close(r.done)
r.wg.Wait()
}
func (r *reporter) snapshotLoop() {
r.logger.Infof("Starting metrics logging every %v", r.period)
defer r.logger.Infof("Stopping metrics logging.")
defer func() {
r.logTotals(makeDeltaSnapshot(monitoring.MakeFlatSnapshot(), makeSnapshot(r.registry)))
}()
ticker := time.NewTicker(r.period)
defer ticker.Stop()
var last monitoring.FlatSnapshot
for {
select {
case <-r.done:
return
case <-ticker.C:
}
cur := makeSnapshot(r.registry)
delta := makeDeltaSnapshot(last, cur)
last = cur
r.logSnapshot(delta)
}
}
func (r *reporter) logSnapshot(s monitoring.FlatSnapshot) {
if snapshotLen(s) > 0 {
r.logger.Infow("Non-zero metrics in the last "+r.period.String(), toKeyValuePairs(s)...)
return
}
r.logger.Infof("No non-zero metrics in the last %v", r.period)
}
func (r *reporter) logTotals(s monitoring.FlatSnapshot) {
r.logger.Infow("Total non-zero metrics", toKeyValuePairs(s)...)
r.logger.Infof("Uptime: %v", time.Since(StartTime))
}
func makeSnapshot(R *monitoring.Registry) monitoring.FlatSnapshot {
mode := monitoring.Full
return monitoring.CollectFlatSnapshot(R, mode, true)
}
func makeDeltaSnapshot(prev, cur monitoring.FlatSnapshot) monitoring.FlatSnapshot {
delta := monitoring.MakeFlatSnapshot()
for k, b := range cur.Bools {
if p, ok := prev.Bools[k]; !ok || p != b {
delta.Bools[k] = b
}
}
for k, s := range cur.Strings {
if _, found := strConsts[k]; found {
delta.Strings[k] = s
} else if p, ok := prev.Strings[k]; !ok || p != s {
delta.Strings[k] = s
}
}
for k, i := range cur.Ints {
if _, found := gauges[k]; found {
delta.Ints[k] = i
} else {
if p := prev.Ints[k]; p != i {
delta.Ints[k] = i - p
}
}
}
for k, f := range cur.Floats {
if _, found := gauges[k]; found {
delta.Floats[k] = f
} else if p := prev.Floats[k]; p != f {
delta.Floats[k] = f - p
}
}
return delta
}
func snapshotLen(s monitoring.FlatSnapshot) int {
return len(s.Bools) + len(s.Floats) + len(s.Ints) + len(s.Strings)
}
func toKeyValuePairs(s monitoring.FlatSnapshot) []interface{} {
data := make(common.MapStr, snapshotLen(s))
for k, v := range s.Bools {
data.Put(k, v)
}
for k, v := range s.Floats {
data.Put(k, v)
}
for k, v := range s.Ints {
data.Put(k, v)
}
for k, v := range s.Strings {
data.Put(k, v)
}
return []interface{}{logp.Namespace("monitoring"), logp.Reflect("metrics", data)}
}