-
Notifications
You must be signed in to change notification settings - Fork 42
/
server_status.go
59 lines (52 loc) · 1.44 KB
/
server_status.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
package server
import (
"github.com/gocraft/web"
"os"
"os/user"
"runtime"
"sync"
)
var (
serverStatusGetwdWarnOnce sync.Once
serverStatusHostnameWarnOnce sync.Once
serverStatusUserCurrentWarnOnce sync.Once
)
type serverStatus struct {
*APIContext
}
func setUpServerStatusRouter(prefix string, router *web.Router) {
root := router.Subrouter(serverStatus{}, "")
root.Get("/runtime_status", (*serverStatus).RuntimeStatus)
}
func (ss *serverStatus) RuntimeStatus(rw web.ResponseWriter, req *web.Request) {
res := map[string]interface{}{
"num_goroutine": runtime.NumGoroutine(),
"num_cgo_call": runtime.NumCgoCall(),
"gomaxprocs": runtime.GOMAXPROCS(0),
"goroot": runtime.GOROOT(),
"num_cpu": runtime.NumCPU(),
"goversion": runtime.Version(),
"pid": os.Getpid(),
}
logOnce := func(name string, once *sync.Once) {
once.Do(func() {
ss.APIContext.Log().Warnf("runtime status '%v' isn't supported on this environment (this log is only written once)", name)
})
}
if dir, err := os.Getwd(); err != nil {
logOnce("working_directory", &serverStatusGetwdWarnOnce)
} else {
res["working_directory"] = dir
}
if host, err := os.Hostname(); err != nil {
logOnce("hostname", &serverStatusHostnameWarnOnce)
} else {
res["hostname"] = host
}
if user, err := user.Current(); err != nil {
logOnce("user", &serverStatusUserCurrentWarnOnce)
} else {
res["user"] = user.Username
}
ss.Render(res)
}