-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathhandler.go
200 lines (170 loc) · 4.71 KB
/
handler.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
package stats_api
import (
"encoding/json"
"io"
"net/http"
"runtime"
"strconv"
"sync"
"time"
)
// Stats represents activity status of Go.
type Stats struct {
Time int64 `json:"time"`
// runtime
GoVersion string `json:"go_version"`
GoOs string `json:"go_os"`
GoArch string `json:"go_arch"`
CpuNum int `json:"cpu_num"`
GoroutineNum int `json:"goroutine_num"`
Gomaxprocs int `json:"gomaxprocs"`
CgoCallNum int64 `json:"cgo_call_num"`
// memory
MemoryAlloc uint64 `json:"memory_alloc"`
MemoryTotalAlloc uint64 `json:"memory_total_alloc"`
MemorySys uint64 `json:"memory_sys"`
MemoryLookups uint64 `json:"memory_lookups"`
MemoryMallocs uint64 `json:"memory_mallocs"`
MemoryFrees uint64 `json:"memory_frees"`
// stack
StackInUse uint64 `json:"memory_stack"`
// heap
HeapAlloc uint64 `json:"heap_alloc"`
HeapSys uint64 `json:"heap_sys"`
HeapIdle uint64 `json:"heap_idle"`
HeapInuse uint64 `json:"heap_inuse"`
HeapReleased uint64 `json:"heap_released"`
HeapObjects uint64 `json:"heap_objects"`
// garbage collection
GcNext uint64 `json:"gc_next"`
GcLast uint64 `json:"gc_last"`
GcNum uint32 `json:"gc_num"`
GcPerSecond float64 `json:"gc_per_second"`
GcPausePerSecond float64 `json:"gc_pause_per_second"`
GcPause []float64 `json:"gc_pause"`
}
var lastSampleTime time.Time
var lastPauseNs uint64 = 0
var lastNumGc uint32 = 0
var nsInMs float64 = float64(time.Millisecond)
var statsMux sync.Mutex
func GetStats() *Stats {
statsMux.Lock()
defer statsMux.Unlock()
var mem runtime.MemStats
runtime.ReadMemStats(&mem)
now := time.Now()
var gcPausePerSecond float64
if lastPauseNs > 0 {
pauseSinceLastSample := mem.PauseTotalNs - lastPauseNs
gcPausePerSecond = float64(pauseSinceLastSample) / nsInMs
}
lastPauseNs = mem.PauseTotalNs
countGc := int(mem.NumGC - lastNumGc)
var gcPerSecond float64
if lastNumGc > 0 {
diff := float64(countGc)
diffTime := now.Sub(lastSampleTime).Seconds()
gcPerSecond = diff / diffTime
}
if countGc > 256 {
// lagging GC pause times
countGc = 256
}
gcPause := make([]float64, countGc)
for i := 0; i < countGc; i++ {
idx := int((mem.NumGC-uint32(i))+255) % 256
pause := float64(mem.PauseNs[idx])
gcPause[i] = pause / nsInMs
}
lastNumGc = mem.NumGC
lastSampleTime = time.Now()
return &Stats{
Time: now.UnixNano(),
GoVersion: runtime.Version(),
GoOs: runtime.GOOS,
GoArch: runtime.GOARCH,
CpuNum: runtime.NumCPU(),
GoroutineNum: runtime.NumGoroutine(),
Gomaxprocs: runtime.GOMAXPROCS(0),
CgoCallNum: runtime.NumCgoCall(),
// memory
MemoryAlloc: mem.Alloc,
MemoryTotalAlloc: mem.TotalAlloc,
MemorySys: mem.Sys,
MemoryLookups: mem.Lookups,
MemoryMallocs: mem.Mallocs,
MemoryFrees: mem.Frees,
// stack
StackInUse: mem.StackInuse,
// heap
HeapAlloc: mem.HeapAlloc,
HeapSys: mem.HeapSys,
HeapIdle: mem.HeapIdle,
HeapInuse: mem.HeapInuse,
HeapReleased: mem.HeapReleased,
HeapObjects: mem.HeapObjects,
// garbage collection
GcNext: mem.NextGC,
GcLast: mem.LastGC,
GcNum: mem.NumGC,
GcPerSecond: gcPerSecond,
GcPausePerSecond: gcPausePerSecond,
GcPause: gcPause,
}
}
var newLineTerm bool = false
var prettyPrint bool = false
// NewLineTermEnabled enable termination with newline for response body.
func NewLineTermEnabled() {
newLineTerm = true
}
// NewLineTermDisabled disable termination with newline for response body.
func NewLineTermDisabled() {
newLineTerm = false
}
// PrettyPrintEnabled enable pretty-print for response body.
func PrettyPrintEnabled() {
prettyPrint = true
}
// PrettyPrintDisabled disable pritty-print for response body.
func PrettyPrintDisabled() {
prettyPrint = false
}
// Handler returns activity status of Go.
func Handler(w http.ResponseWriter, r *http.Request) {
values := r.URL.Query()
for _, c := range []string{"1", "true"} {
if values.Get("pp") == c {
prettyPrint = true
}
}
var jsonBytes []byte
var jsonErr error
if prettyPrint {
jsonBytes, jsonErr = json.MarshalIndent(GetStats(), "", " ")
} else {
jsonBytes, jsonErr = json.Marshal(GetStats())
}
var body string
if jsonErr != nil {
body = jsonErr.Error()
} else {
body = string(jsonBytes)
}
if newLineTerm {
body += "\n"
}
headers := make(map[string]string)
headers["Content-Type"] = "application/json"
headers["Content-Length"] = strconv.Itoa(len(body))
for name, value := range headers {
w.Header().Set(name, value)
}
if jsonErr != nil {
w.WriteHeader(http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusOK)
}
io.WriteString(w, body)
}