-
Notifications
You must be signed in to change notification settings - Fork 1
/
runtime.go
43 lines (37 loc) · 911 Bytes
/
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
package report
import (
"runtime"
"time"
)
// RuntimeEvery records runtime stats at the specified interval
//
// log := report.New(report.StdOutJSON(), report.Data{"service": "myAppName"})
// log.RuntimeEvery(time.Second*10)
//
func (l *Logger) RuntimeEvery(duration time.Duration) {
l.wg.Add(1)
go func() {
ticker := time.NewTicker(duration)
defer ticker.Stop()
statLoop:
for {
select {
case <-ticker.C:
l.Info("runtime", runtimeData())
case <-l.stopC:
break statLoop
}
}
l.wg.Done()
}()
}
func runtimeData() Data {
m := &runtime.MemStats{}
runtime.ReadMemStats(m)
return Data{
"runtime.stack_mb": float64(m.StackSys) / float64(1024*1024),
"runtime.heap_mb": float64(m.HeapAlloc) / float64(1024*1024),
"runtime.goroutine_count": runtime.NumGoroutine(),
"runtime.gc_pause_ms": float64(m.PauseNs[(m.NumGC+255)%256]) / msPerNs,
}
}