-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathstatsview.go
128 lines (109 loc) · 3.34 KB
/
statsview.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
package statsview
import (
"context"
"fmt"
"net/http"
"net/http/pprof"
"time"
"github.com/go-echarts/go-echarts/v2/components"
"github.com/go-echarts/go-echarts/v2/templates"
"github.com/rs/cors"
"github.com/go-echarts/statsview/statics"
"github.com/go-echarts/statsview/viewer"
)
// ViewManager
type ViewManager struct {
ctx context.Context
cancel context.CancelFunc
srv *http.Server
smgr *viewer.StatsMgr
views []viewer.Viewer
}
// Register registers views to the ViewManager
func (vm *ViewManager) Register(views ...viewer.Viewer) {
vm.views = append(vm.views, views...)
}
// Start runs a http server and begin to collect metrics
func (vm *ViewManager) Start() error {
return vm.srv.ListenAndServe()
}
// Stop shutdown the http server gracefully
func (vm *ViewManager) Stop() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
vm.srv.Shutdown(ctx)
vm.cancel()
}
func init() {
templates.PageTpl = `
{{- define "page" }}
<!DOCTYPE html>
<html>
{{- template "header" . }}
<body>
<p> 🚀 <a href="https://github.com/go-echarts/statsview"><b>StatsView</b></a> <em>is a real-time Golang runtime stats visualization profiler</em></p>
<style> .box { justify-content:center; display:flex; flex-wrap:wrap } </style>
<div class="box"> {{- range .Charts }} {{ template "base" . }} {{- end }} </div>
</body>
</html>
{{ end }}
`
}
// New creates a new ViewManager instance
func New() *ViewManager {
page := components.NewPage()
page.PageTitle = "Statsview"
page.AssetsHost = fmt.Sprintf("http://%s/debug/statsview/statics/", viewer.LinkAddr())
page.Assets.JSAssets.Add("jquery.min.js")
mgr := &ViewManager{
srv: &http.Server{
Addr: viewer.Addr(),
ReadTimeout: time.Minute,
WriteTimeout: time.Minute,
MaxHeaderBytes: 1 << 20,
},
}
mgr.ctx, mgr.cancel = context.WithCancel(context.Background())
mgr.Register(
viewer.NewGoroutinesViewer(),
viewer.NewHeapViewer(),
viewer.NewStackViewer(),
viewer.NewGCNumViewer(),
viewer.NewGCSizeViewer(),
viewer.NewGCCPUFractionViewer(),
)
smgr := viewer.NewStatsMgr(mgr.ctx)
for _, v := range mgr.views {
v.SetStatsMgr(smgr)
}
mux := http.NewServeMux()
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
for _, v := range mgr.views {
page.AddCharts(v.View())
mux.HandleFunc("/debug/statsview/view/"+v.Name(), v.Serve)
}
mux.HandleFunc("/debug/statsview", func(w http.ResponseWriter, _ *http.Request) {
page.Render(w)
})
staticsRoute := func(s string) string {
return "/debug/statsview/statics/" + s
}
mux.HandleFunc(staticsRoute("echarts.min.js"), func(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte(statics.EchartJS))
})
mux.HandleFunc(staticsRoute("jquery.min.js"), func(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte(statics.JqueryJS))
})
mux.HandleFunc(staticsRoute("themes/westeros.js"), func(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte(statics.WesterosJS))
})
mux.HandleFunc(staticsRoute("themes/macarons.js"), func(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte(statics.MacaronsJS))
})
mgr.srv.Handler = cors.AllowAll().Handler(mux)
return mgr
}