Repository navigation
Expand file tree
/
Copy pathruntime.go
More file actions
57 lines (51 loc) · 1.94 KB
/
Copy pathruntime.go
File metadata and controls
57 lines (51 loc) · 1.94 KB
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
package metrics
import (
"runtime"
"time"
)
// sets how often the metrics Runtime will sample and
// emit stats.
var RuntimeMetricsSamplingInterval time.Duration = 30 * time.Second
// Runtime enters into a loop, sampling and outputing the runtime stats periodically.
// Usage:
// func main() {
// ...
// go metrics.Runtime()
// ...
// }
func Runtime() {
c := time.Tick(RuntimeMetricsSamplingInterval)
for _ = range c {
ReportRuntimeMetrics()
}
}
// Samples and reports current runtime status once.
func ReportRuntimeMetrics() {
var memstats runtime.MemStats
runtime.ReadMemStats(&memstats)
numGoroutines := runtime.NumGoroutine()
r := map[string]float64{
"goroutine": float64(numGoroutines),
"runtime.MemStats.Alloc": float64(memstats.Alloc),
"runtime.MemStats.Frees": float64(memstats.Frees),
"runtime.MemStats.HeapAlloc": float64(memstats.HeapAlloc),
"runtime.MemStats.HeapIdle": float64(memstats.HeapIdle),
"runtime.MemStats.HeapObjects": float64(memstats.HeapObjects),
"runtime.MemStats.HeapReleased": float64(memstats.HeapReleased),
"runtime.MemStats.HeapSys": float64(memstats.HeapSys),
"runtime.MemStats.LastGC": float64(memstats.LastGC),
"runtime.MemStats.Lookups": float64(memstats.Lookups),
"runtime.MemStats.Mallocs": float64(memstats.Mallocs),
"runtime.MemStats.MCacheInuse": float64(memstats.MCacheInuse),
"runtime.MemStats.MCacheSys": float64(memstats.MCacheSys),
"runtime.MemStats.MSpanInuse": float64(memstats.MSpanInuse),
"runtime.MemStats.MSpanSys": float64(memstats.MSpanSys),
"runtime.MemStats.NextGC": float64(memstats.NextGC),
"runtime.MemStats.NumGC": float64(memstats.NumGC),
"runtime.MemStats.PauseTotalMs": float64(memstats.PauseTotalNs) / float64(time.Millisecond),
"runtime.MemStats.StackInuse": float64(memstats.StackInuse),
}
for name, value := range r {
Gauge(name, value, nil, 1.0)
}
}