forked from micro/micro
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stats.go
184 lines (158 loc) · 3.38 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package stats
import (
"encoding/json"
"fmt"
"html/template"
"net/http"
"runtime"
"sync"
"time"
)
type stats struct {
sync.RWMutex
Started int64 `json:"started"`
Memory string `json:"memory"`
Threads int `json:"threads"`
GC string `json:"gc_pause"`
Counters []*counter `json:"counters"`
running bool
exit chan bool
}
type counter struct {
// time created
Timestamp int64 `json:"timestamp"`
// counters
Status map[string]int `json:"status_codes"`
Total int `json:"total_reqs"`
}
var (
// 5 second window
window = time.Second * 5
// 120 seconds total
total = 24
)
func render(w http.ResponseWriter, r *http.Request, tmpl string, data interface{}) {
t, err := template.New("template").Funcs(template.FuncMap{
// "format": format,
}).Parse(layoutTemplate)
if err != nil {
http.Error(w, "Error occurred:"+err.Error(), 500)
return
}
t, err = t.Parse(tmpl)
if err != nil {
http.Error(w, "Error occurred:"+err.Error(), 500)
return
}
if err := t.ExecuteTemplate(w, "layout", data); err != nil {
http.Error(w, "Error occurred:"+err.Error(), 500)
}
}
func (s *stats) run() {
t := time.NewTicker(window)
w := 0
for {
select {
case <-s.exit:
t.Stop()
return
case <-t.C:
// roll
s.Lock()
s.Counters = append(s.Counters, &counter{
Timestamp: time.Now().Unix(),
Status: make(map[string]int),
})
if len(s.Counters) >= total {
s.Counters = s.Counters[1:]
}
w++
if w >= 2 {
var mstat runtime.MemStats
runtime.ReadMemStats(&mstat)
s.Threads = runtime.NumGoroutine()
s.Memory = fmt.Sprintf("%.2fmb", float64(mstat.Alloc)/float64(1024*1024))
s.GC = fmt.Sprintf("%.3fms", float64(mstat.PauseTotalNs)/(1000*1000))
w = 0
}
s.Unlock()
}
}
}
func (s *stats) Record(c string, t int) {
s.Lock()
counter := s.Counters[len(s.Counters)-1]
counter.Status[c] += t
counter.Total += t
s.Counters[len(s.Counters)-1] = counter
s.Unlock()
}
func (s *stats) ServeHTTP(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var code string
rw := &writer{w, 200}
h.ServeHTTP(w, r)
switch {
case rw.status >= 500:
code = "50x"
case rw.status >= 400:
code = "40x"
case rw.status >= 300:
code = "30x"
case rw.status >= 200:
code = "20x"
}
s.Record(code, 1)
})
}
func (s *stats) StatsHandler(w http.ResponseWriter, r *http.Request) {
if ct := r.Header.Get("Content-Type"); ct == "application/json" {
s.RLock()
b, err := json.Marshal(s)
s.RUnlock()
if err != nil {
http.Error(w, err.Error(), 500)
return
}
w.Header().Set("Content-Type", ct)
w.Write(b)
return
}
render(w, r, statsTemplate, nil)
}
func (s *stats) Start() error {
s.Lock()
defer s.Unlock()
if s.running {
return nil
}
s.Started = time.Now().Unix()
s.exit = make(chan bool)
go s.run()
return nil
}
func (s *stats) Stop() error {
s.Lock()
defer s.Unlock()
if !s.running {
return nil
}
close(s.exit)
s.Started = 0
return nil
}
func New() *stats {
var mstat runtime.MemStats
runtime.ReadMemStats(&mstat)
return &stats{
Threads: runtime.NumGoroutine(),
Memory: fmt.Sprintf("%.2fmb", float64(mstat.Alloc)/float64(1024*1024)),
GC: fmt.Sprintf("%.3fms", float64(mstat.PauseTotalNs)/(1000*1000)),
Counters: []*counter{
&counter{
Timestamp: time.Now().Unix(),
Status: make(map[string]int),
},
},
}
}