-
Notifications
You must be signed in to change notification settings - Fork 180
/
dotweb_sysgroup.go
123 lines (111 loc) · 3.38 KB
/
dotweb_sysgroup.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
package dotweb
import (
"fmt"
"github.com/devfeel/dotweb/core"
jsonutil "github.com/devfeel/dotweb/framework/json"
"runtime"
"runtime/debug"
"runtime/pprof"
"strings"
)
// initDotwebGroup init Dotweb route group which start with /dotweb/
func initDotwebGroup(server *HttpServer) {
gInner := server.Group("/dotweb")
gInner.GET("/debug/pprof/:key", showPProf)
gInner.GET("/debug/freemem", freeMemory)
gInner.GET("/state", showServerState)
gInner.GET("/state/interval", showIntervalData)
gInner.GET("/query/:key", showQuery)
gInner.GET("/routers", showRouters)
}
// query pprof debug info
// key:heap goroutine threadcreate block
func showPProf(ctx Context) error {
querykey := ctx.GetRouterName("key")
runtime.GC()
return pprof.Lookup(querykey).WriteTo(ctx.Response().Writer(), 1)
}
func freeMemory(ctx Context) error {
debug.FreeOSMemory()
return nil
}
func showIntervalData(ctx Context) error {
if ctx.Request().ExistsQueryKey("pretty") {
return showIntervalDataPretty(ctx)
} else {
return showIntervalDataJson(ctx)
}
}
func showIntervalDataJson(ctx Context) error {
type data struct {
Time string
RequestCount uint64
ErrorCount uint64
}
queryKey := ctx.QueryString("querykey")
d := new(data)
d.Time = queryKey
d.RequestCount = ctx.HttpServer().StateInfo().QueryIntervalRequestData(queryKey)
d.ErrorCount = ctx.HttpServer().StateInfo().QueryIntervalErrorData(queryKey)
return ctx.WriteJson(d)
}
func showIntervalDataPretty(ctx Context) error {
type data struct {
Time string
RequestCount uint64
ErrorCount uint64
}
queryKey := ctx.QueryString("querykey")
d := new(data)
d.Time = queryKey
d.RequestCount = ctx.HttpServer().StateInfo().QueryIntervalRequestData(queryKey)
d.ErrorCount = ctx.HttpServer().StateInfo().QueryIntervalErrorData(queryKey)
tableData := "<tr><td>" + d.Time + "</td><td>" + fmt.Sprint(d.RequestCount) + "</td><td>" + fmt.Sprint(d.ErrorCount) + "</td></tr>"
col := `<colgroup>
<col width="40%">
<col width="30%">
<col width="30%">
</colgroup>`
header := `<tr>
<th>Time</th>
<th>RequestCount</th>
<th>ErrorCount</th>
</tr>`
html := core.CreateTableHtml(col, "IntervalData", header, tableData)
return ctx.WriteHtml(html)
}
// snow server status
func showServerState(ctx Context) error {
return ctx.WriteHtml(ctx.HttpServer().StateInfo().ShowHtmlTableData(Version, ctx.HttpServer().DotApp.GlobalUniqueID()))
}
// query server information
func showQuery(ctx Context) error {
querykey := ctx.GetRouterName("key")
switch querykey {
case "state":
return ctx.WriteString(jsonutil.GetJsonString(ctx.HttpServer().StateInfo()))
case "":
return ctx.WriteString("please input key")
default:
return ctx.WriteString("not support key => " + querykey)
}
}
func showRouters(ctx Context) error {
data := ""
routerCount := len(ctx.HttpServer().router.GetAllRouterExpress())
for k, _ := range ctx.HttpServer().router.GetAllRouterExpress() {
method := strings.Split(k, routerExpressSplit)[0]
router := strings.Split(k, routerExpressSplit)[1]
data += "<tr><td>" + method + "</td><td>" + router + "</td></tr>"
}
col := `<colgroup>
<col width="40%">
<col width="60%">
</colgroup>`
header := `<tr>
<th>Method</th>
<th>Router</th>
</tr>`
html := core.CreateTableHtml(col, "Routers:"+fmt.Sprint(routerCount), header, data)
return ctx.WriteHtml(html)
}