forked from thingio/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
170 lines (145 loc) · 4.04 KB
/
stats.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
package vars
import (
"expvar"
"fmt"
"runtime"
kexpvar "github.com/influxdata/kapacitor/expvar"
"github.com/influxdata/kapacitor/uuid"
)
var (
// All internal stats are added as sub-maps to this top level map.
stats *kexpvar.Map
)
func init() {
// Initialze the global stats map
stats = &kexpvar.Map{}
stats.Init()
expvar.Publish(Product, stats)
}
// NewStatistic creates a new statistic in the published expvar map.
func NewStatistic(name string, tags map[string]string) (string, *kexpvar.Map) {
key := uuid.New().String()
m := &kexpvar.Map{}
m.Init()
// Set the name
nameVar := &kexpvar.String{}
nameVar.Set(name)
m.Set("name", nameVar)
// Set the tags
tagsVar := &kexpvar.Map{}
tagsVar.Init()
for k, v := range tags {
value := &kexpvar.String{}
value.Set(v)
tagsVar.Set(k, value)
}
// Always add ID tags
tagsVar.Set(ClusterIDVarName, ClusterIDVar)
tagsVar.Set(ServerIDVarName, ServerIDVar)
tagsVar.Set(HostVarName, HostVar)
m.Set("tags", tagsVar)
// Create and set the values entry used for actual stats.
statMap := &kexpvar.Map{}
statMap.Init()
m.Set("values", statMap)
// Set new statsMap on the top level map.
stats.Set(key, m)
return key, statMap
}
// DeleteStatistic removes the specified statistic from the published map.
func DeleteStatistic(key string) {
stats.Delete(key)
}
type StatsData struct {
Name string `json:"name"`
Tags map[string]string `json:"tags"`
Values map[string]interface{} `json:"values"`
}
// GetStatsData return all stats data from the published expvars.
func GetStatsData() ([]StatsData, error) {
allData := make([]StatsData, 0)
// Add Global expvars
globalData := StatsData{
Name: "kapacitor",
Values: make(map[string]interface{}),
}
allData = append(allData, globalData)
// Get all global statistics
expvar.Do(func(kv expvar.KeyValue) {
switch v := kv.Value.(type) {
case kexpvar.IntVar:
globalData.Values[kv.Key] = v.IntValue()
case kexpvar.FloatVar:
globalData.Values[kv.Key] = v.FloatValue()
case *kexpvar.Map:
if kv.Key != Product {
panic("unexpected published top level expvar.Map with key " + kv.Key)
}
}
})
// Get all other specific statistics
stats.Do(func(kv expvar.KeyValue) {
v := kv.Value.(*kexpvar.Map)
data := StatsData{
Tags: make(map[string]string),
Values: make(map[string]interface{}),
}
v.Do(func(subKV expvar.KeyValue) {
switch subKV.Key {
case "name":
data.Name = subKV.Value.(*kexpvar.String).StringValue()
case "tags":
// string-string tags map.
n := subKV.Value.(*kexpvar.Map)
n.Do(func(t expvar.KeyValue) {
data.Tags[t.Key] = t.Value.(kexpvar.StringVar).StringValue()
})
case "values":
// string-interface map.
n := subKV.Value.(*kexpvar.Map)
n.Do(func(kv expvar.KeyValue) {
switch v := kv.Value.(type) {
case kexpvar.IntVar:
data.Values[kv.Key] = v.IntValue()
case kexpvar.FloatVar:
data.Values[kv.Key] = v.FloatValue()
default:
panic(fmt.Sprintf("unknown expvar.Var type for stats %T", kv.Value))
}
})
}
})
// If no field data, don't include it in the results
if len(data.Values) == 0 {
return
}
allData = append(allData, data)
})
// Add uptime to globalData
globalData.Values[UptimeVarName] = uptime().Seconds()
// Add Go runtime stats.
data := StatsData{
Name: "runtime",
}
var rt runtime.MemStats
runtime.ReadMemStats(&rt)
data.Values = map[string]interface{}{
"Alloc": int64(rt.Alloc),
"TotalAlloc": int64(rt.TotalAlloc),
"Sys": int64(rt.Sys),
"Lookups": int64(rt.Lookups),
"Mallocs": int64(rt.Mallocs),
"Frees": int64(rt.Frees),
"HeapAlloc": int64(rt.HeapAlloc),
"HeapSys": int64(rt.HeapSys),
"HeapIdle": int64(rt.HeapIdle),
"HeapInUse": int64(rt.HeapInuse),
"HeapReleased": int64(rt.HeapReleased),
"HeapObjects": int64(rt.HeapObjects),
"PauseTotalNs": int64(rt.PauseTotalNs),
"NumGC": int64(rt.NumGC),
"NumGoroutine": int64(runtime.NumGoroutine()),
}
allData = append(allData, data)
return allData, nil
}