-
Notifications
You must be signed in to change notification settings - Fork 0
/
periodic.go
219 lines (192 loc) · 6.56 KB
/
periodic.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package stats
import (
"runtime"
"time"
metric "github.com/Sidddddarth/metrics"
)
// GaugeFunc is an interface that implements the setting of a gauge value
// in a stats system. It should be expected that key will contain multiple
// parts separated by the '.' character in the form used by statsd (e.x.
// "mem.heap.alloc")
type gaugeFunc func(key string, val uint64)
// runtimeStatsCollector implements the periodic grabbing of informational data from the
// runtime package and outputting the values to a GaugeFunc.
type runtimeStatsCollector struct {
// PauseDur represents the interval in between each set of stats output.
// Defaults to 10 seconds.
PauseDur time.Duration
// EnableCPU determines whether CPU statistics will be output. Defaults to true.
EnableCPU bool
// EnableMem determines whether memory statistics will be output. Defaults to true.
EnableMem bool
// EnableGC determines whether garbage collection statistics will be output. EnableMem
// must also be set to true for this to take affect. Defaults to true.
EnableGC bool
// done, when closed, is used to signal runtimeStatsCollector that is should stop collecting
// statistics and the Run function should return. If done is set, upon shutdown
// all gauges will be sent a final zero value to reset their values to 0.
done chan struct{}
gaugeFunc gaugeFunc
}
// New creates a new runtimeStatsCollector that will periodically output statistics to gaugeFunc. It
// will also set the values of the exported fields to the described defaults. The values
// of the exported defaults can be changed at any point before Run is called.
func newRuntimeStatsCollector(gaugeFunc gaugeFunc) runtimeStatsCollector {
return runtimeStatsCollector{
PauseDur: 10 * time.Second,
EnableCPU: true,
EnableMem: true,
EnableGC: true,
gaugeFunc: gaugeFunc,
done: make(chan struct{}),
}
}
// Run gathers statistics from package runtime and outputs them to the configured GaugeFunc every
// PauseDur. This function will not return until Done has been closed (or never if Done is nil),
// therefore it should be called in its own goroutine.
func (c runtimeStatsCollector) run() {
defer c.zeroStats()
c.outputStats()
// Gauges are a 'snapshot' rather than a histogram. Pausing for some interval
// aims to get a 'recent' snapshot out before statsd flushes metrics.
tick := time.NewTicker(c.PauseDur)
defer tick.Stop()
for {
select {
case <-c.done:
return
case <-tick.C:
c.outputStats()
}
}
}
type cpuStats struct {
NumGoroutine uint64
NumCgoCall uint64
}
// zeroStats sets all the stat guages to zero. On shutdown we want to zero them out so they don't persist
// at their last value until we start back up.
func (c runtimeStatsCollector) zeroStats() {
if c.EnableCPU {
cStats := cpuStats{}
c.outputCPUStats(&cStats)
}
if c.EnableMem {
mStats := runtime.MemStats{}
c.outputMemStats(&mStats)
if c.EnableGC {
c.outputGCStats(&mStats)
}
}
}
func (c runtimeStatsCollector) outputStats() {
if c.EnableCPU {
cStats := cpuStats{
NumGoroutine: uint64(runtime.NumGoroutine()),
NumCgoCall: uint64(runtime.NumCgoCall()),
}
c.outputCPUStats(&cStats)
}
if c.EnableMem {
m := &runtime.MemStats{}
runtime.ReadMemStats(m)
c.outputMemStats(m)
if c.EnableGC {
c.outputGCStats(m)
}
}
}
func (c runtimeStatsCollector) outputCPUStats(s *cpuStats) {
c.gaugeFunc("cpu.goroutines", s.NumGoroutine)
c.gaugeFunc("cpu.cgo_calls", s.NumCgoCall)
}
func (c runtimeStatsCollector) outputMemStats(m *runtime.MemStats) {
// General
c.gaugeFunc("mem.alloc", m.Alloc)
c.gaugeFunc("mem.total", m.TotalAlloc)
c.gaugeFunc("mem.sys", m.Sys)
c.gaugeFunc("mem.lookups", m.Lookups)
c.gaugeFunc("mem.malloc", m.Mallocs)
c.gaugeFunc("mem.frees", m.Frees)
// Heap
c.gaugeFunc("mem.heap.alloc", m.HeapAlloc)
c.gaugeFunc("mem.heap.sys", m.HeapSys)
c.gaugeFunc("mem.heap.idle", m.HeapIdle)
c.gaugeFunc("mem.heap.inuse", m.HeapInuse)
c.gaugeFunc("mem.heap.released", m.HeapReleased)
c.gaugeFunc("mem.heap.objects", m.HeapObjects)
// Stack
c.gaugeFunc("mem.stack.inuse", m.StackInuse)
c.gaugeFunc("mem.stack.sys", m.StackSys)
c.gaugeFunc("mem.stack.mspan_inuse", m.MSpanInuse)
c.gaugeFunc("mem.stack.mspan_sys", m.MSpanSys)
c.gaugeFunc("mem.stack.mcache_inuse", m.MCacheInuse)
c.gaugeFunc("mem.stack.mcache_sys", m.MCacheSys)
c.gaugeFunc("mem.othersys", m.OtherSys)
}
func (c runtimeStatsCollector) outputGCStats(m *runtime.MemStats) {
c.gaugeFunc("mem.gc.sys", m.GCSys)
c.gaugeFunc("mem.gc.next", m.NextGC)
c.gaugeFunc("mem.gc.last", m.LastGC)
c.gaugeFunc("mem.gc.pause_total", m.PauseTotalNs)
c.gaugeFunc("mem.gc.pause", m.PauseNs[(m.NumGC+255)%256])
c.gaugeFunc("mem.gc.count", uint64(m.NumGC))
c.gaugeFunc("mem.gc.cpu_percent", uint64(100*m.GCCPUFraction))
}
// metricStatsCollector implements the periodic grabbing of informational data from the
// metric package and outputting the values as stats
type metricStatsCollector struct {
stats Stats
metricManager metric.Manager
// PauseDur represents the interval in between each set of stats output.
// Defaults to 60 seconds.
pauseDur time.Duration
// Done, when closed, is used to signal metricStatsCollector that is should stop collecting
// statistics and the run function should return.
done chan struct{}
}
// newMetricStatsCollector creates a new metricStatsCollector.
func newMetricStatsCollector(stats Stats, metricManager metric.Manager) metricStatsCollector {
return metricStatsCollector{
stats: stats,
metricManager: metricManager,
pauseDur: 60 * time.Second,
done: make(chan struct{}),
}
}
// run gathers statistics from package metric and outputs them as
func (c metricStatsCollector) run() {
c.outputStats()
// Gauges are a 'snapshot' rather than a histogram. Pausing for some interval
// aims to get a 'recent' snapshot out before statsd flushes metrics.
tick := time.NewTicker(c.pauseDur)
defer tick.Stop()
for {
select {
case <-c.done:
return
case <-tick.C:
c.outputStats()
}
}
}
func (c metricStatsCollector) outputStats() {
if c.metricManager == nil {
return
}
c.metricManager.GetRegistry(metric.PUBLISHED_METRICS).Range(func(key, value interface{}) bool {
m := key.(metric.Measurement)
switch value := value.(type) {
case metric.Gauge:
c.stats.NewTaggedStat(m.GetName(), GaugeType, Tags(m.GetTags())).
Gauge(value.Value())
case metric.Counter:
c.stats.NewTaggedStat(m.GetName(), CountType, Tags(m.GetTags())).
Count(int(value.Value()))
case metric.MovingAverage:
c.stats.NewTaggedStat(m.GetName(), GaugeType, Tags(m.GetTags())).
Gauge(value.Value())
}
return true
})
}