-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathruntime.go
99 lines (85 loc) · 3.45 KB
/
runtime.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
package prome
import (
"fmt"
"runtime"
"runtime/pprof"
"github.com/prometheus/client_golang/prometheus"
)
// Below represents the id of log which should be printed so some runtime
// information or abnornal situations can be captured.
const (
logIDMemstatsAllocGauge = "runtime_memstats_alloc"
logIDMemstatsSysGauge = "runtime_memstats_sys"
logIDMemstatsLastGCPauseNSGauge = "runtime_memstats_last_gc"
logIDRuntimeNumGoroutineGauge = "runtime_num_goroutine"
logIDOSThreadsGauge = "runtime_os_threads"
logIDRuntimeGOMaxProcsGauge = "runtime_gomaxprocs"
logIDRuntimeNumCPUGauge = "runtime_num_cpu"
)
var (
memstatsAllocGauge prometheus.Gauge
memstatsSysGauge prometheus.Gauge
memstatsLastGCPauseNSGauge prometheus.Gauge
runtimeNumGoroutineGauge prometheus.Gauge
osThreadsGauge prometheus.Gauge
runtimeGOMaxProcsGauge prometheus.Gauge
runtimeNumCPUGauge prometheus.Gauge
)
func registerRuntime(
name string,
collectors *[]prometheus.Collector, defaultLabels prometheus.Labels,
) {
memstatsAllocGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: fmt.Sprintf("%s_%s", name, logIDMemstatsAllocGauge),
ConstLabels: defaultLabels,
Help: "Runtime memstats alloc (Unit: byte)",
})
*collectors = append(*collectors, memstatsAllocGauge)
memstatsSysGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: fmt.Sprintf("%s_%s", name, logIDMemstatsSysGauge),
ConstLabels: defaultLabels,
Help: "Runtime memstats sys (Unit: byte)",
})
*collectors = append(*collectors, memstatsSysGauge)
memstatsLastGCPauseNSGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: fmt.Sprintf("%s_%s", name, logIDMemstatsLastGCPauseNSGauge),
ConstLabels: defaultLabels,
Help: "Runtime memstats last GC pause (Unit: ns)",
})
*collectors = append(*collectors, memstatsLastGCPauseNSGauge)
runtimeNumGoroutineGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: fmt.Sprintf("%s_%s", name, logIDRuntimeNumGoroutineGauge),
ConstLabels: defaultLabels,
Help: "Runtime number of goroutines",
})
*collectors = append(*collectors, runtimeNumGoroutineGauge)
osThreadsGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: fmt.Sprintf("%s_%s", name, logIDOSThreadsGauge),
ConstLabels: defaultLabels,
Help: "Runtime number of OS threads",
})
*collectors = append(*collectors, osThreadsGauge)
runtimeGOMaxProcsGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: fmt.Sprintf("%s_%s", name, logIDRuntimeGOMaxProcsGauge),
ConstLabels: defaultLabels,
Help: "Runtime GOMAXPROCS",
})
*collectors = append(*collectors, runtimeGOMaxProcsGauge)
runtimeNumCPUGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: fmt.Sprintf("%s_%s", name, logIDRuntimeNumCPUGauge),
ConstLabels: defaultLabels,
Help: "Runtime number of CPUs",
})
*collectors = append(*collectors, runtimeNumCPUGauge)
}
func updateRuntimeGuage() {
var memstats runtime.MemStats
runtime.ReadMemStats(&memstats)
memstatsAllocGauge.Set(float64(memstats.Alloc))
memstatsSysGauge.Set(float64(memstats.Sys))
memstatsLastGCPauseNSGauge.Set(float64(memstats.PauseNs[(memstats.NumGC+255)%256]))
runtimeNumGoroutineGauge.Set(float64(runtime.NumGoroutine()))
osThreadsGauge.Set(float64(pprof.Lookup("threadcreate").Count()))
runtimeGOMaxProcsGauge.Set(float64(runtime.GOMAXPROCS(0)))
runtimeNumCPUGauge.Set(float64(runtime.NumCPU()))
}