forked from niean/goperfcounter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
metrics.go
82 lines (71 loc) · 1.67 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
package goperfcounter
import (
"time"
"github.com/niean/go-metrics-lite"
"github.com/niean/goperfcounter/base"
)
var (
gpGaugeFloat64 = metrics.NewRegistry()
gpCounter = metrics.NewRegistry()
gpMeter = metrics.NewRegistry()
gpHistogram = metrics.NewRegistry()
gpDebug = metrics.NewRegistry()
gpRuntime = metrics.NewRegistry()
gpSelf = metrics.NewRegistry()
values = make(map[string]metrics.Registry) //readonly,mappings of metrics
)
func init() {
values["gauge"] = gpGaugeFloat64
values["counter"] = gpCounter
values["meter"] = gpMeter
values["histogram"] = gpHistogram
values["debug"] = gpDebug
values["runtime"] = gpRuntime
values["self"] = gpSelf
}
//
func rawMetric(types []string) map[string]interface{} {
data := make(map[string]interface{})
for _, mtype := range types {
if v, ok := values[mtype]; ok {
data[mtype] = v.Values()
}
}
return data
}
func rawMetrics() map[string]interface{} {
data := make(map[string]interface{})
for key, v := range values {
data[key] = v.Values()
}
return data
}
func rawSizes() map[string]int64 {
data := map[string]int64{}
all := int64(0)
for key, v := range values {
kv := v.Size()
all += kv
data[key] = kv
}
data["all"] = all
return data
}
func collectBase(bases []string) {
// start base collect after 30sec
time.Sleep(time.Duration(30) * time.Second)
if contains(bases, "debug") {
base.RegisterAndCaptureDebugGCStats(gpDebug, 5e9)
}
if contains(bases, "runtime") {
base.RegisterAndCaptureRuntimeMemStats(gpRuntime, 5e9)
}
}
func contains(bases []string, name string) bool {
for _, n := range bases {
if n == name {
return true
}
}
return false
}