-
Notifications
You must be signed in to change notification settings - Fork 2
/
internal.go
69 lines (52 loc) · 2.09 KB
/
internal.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
package main
import (
"runtime"
)
const InternalModuleName = "internal"
type InternalInputModule struct {
lastStats *runtime.MemStats
}
func (m *InternalInputModule) Name() string {
return InternalModuleName
}
func (m *InternalInputModule) Init(config *Config, moduleConfig *ModuleConfig) error {
return nil
}
func (m *InternalInputModule) TearDown() error {
return nil
}
func (m *InternalInputModule) GetMetrics() (*ModuleMetrics, error) {
metrics := make([]Metric, 0)
memstats := runtime.MemStats{}
runtime.ReadMemStats(&memstats)
if m.lastStats != nil {
previous := m.lastStats
lookups := memstats.Lookups - previous.Lookups
mallocs := memstats.Mallocs - previous.Mallocs
frees := memstats.Frees - previous.Frees
metrics = append(metrics, NewMetric("general.allocated", float64(memstats.Alloc)))
metrics = append(metrics, NewMetric("general.system", float64(memstats.Sys)))
metrics = append(metrics, NewMetric("general.lookups", float64(lookups)))
metrics = append(metrics, NewMetric("general.mallocs", float64(mallocs)))
metrics = append(metrics, NewMetric("general.frees", float64(frees)))
metrics = append(metrics, NewMetric("heap.allocated", float64(memstats.HeapAlloc)))
metrics = append(metrics, NewMetric("heap.system", float64(memstats.HeapSys)))
metrics = append(metrics, NewMetric("heap.idle", float64(memstats.HeapIdle)))
metrics = append(metrics, NewMetric("heap.in_use", float64(memstats.HeapInuse)))
metrics = append(metrics, NewMetric("heap.released", float64(memstats.HeapReleased)))
metrics = append(metrics, NewMetric("heap.objects", float64(memstats.HeapObjects)))
numGC := int(memstats.NumGC - previous.NumGC)
var averageGC float64
if numGC > 0 {
var total uint64
for i := 0; i < numGC; i++ {
total += memstats.PauseNs[(memstats.NumGC+255-uint32(i))%256]
}
averageGC = float64(total) / float64(numGC) / 1000000
}
metrics = append(metrics, NewMetric("gc.average_gc", averageGC))
metrics = append(metrics, NewMetric("gc.num_gc", float64(numGC)))
}
m.lastStats = &memstats
return &ModuleMetrics{Module: m.Name(), Metrics: metrics}, nil
}