fork of https://github.com/sourcegraph/zoekt
1package debugserver
2
3import (
4 "html/template"
5 "net/http"
6 "net/http/pprof"
7 "sync"
8
9 "github.com/prometheus/client_golang/prometheus"
10 "github.com/prometheus/client_golang/prometheus/promauto"
11 "github.com/prometheus/client_golang/prometheus/promhttp"
12 "github.com/sourcegraph/zoekt/index"
13 "golang.org/x/net/trace"
14)
15
16var registerOnce sync.Once
17
18var debugTmpl = template.Must(template.New("name").Parse(`
19<html>
20 <head>
21 <title>/debug</title>
22 <style>
23 .debug-page{
24 display:inline-block;
25 width:12rem;
26 }
27 </style>
28 </head>
29 <body>
30 <a href="/">/<a/><span style="margin:2px">debug</span><br>
31 <br>
32 <a class="debug-page" href="vars">Vars</a><br>
33 {{if .EnablePprof}}<a class="debug-page" href="debug/pprof/">PProf</a>{{else}}PProf disabled{{end}}<br>
34 <a class="debug-page" href="metrics">Metrics</a><br>
35 <a class="debug-page" href="debug/requests">Requests</a><br>
36 <a class="debug-page" href="debug/events">Events</a><br>
37
38 {{/* links which are specific to webserver or indexserver */}}
39 {{range .DebugPages}}<a class="debug-page" href={{.Href}}>{{.Text}}</a>{{.Description}}<br>{{end}}
40
41 <br>
42 <form method="post" action="gc" style="display: inline;"><input type="submit" value="GC"></form>
43 <form method="post" action="freeosmemory" style="display: inline;"><input type="submit" value="Free OS Memory"></form>
44 </body>
45</html>
46`))
47
48type DebugPage struct {
49 Href string
50 Text string
51 Description string
52}
53
54func AddHandlers(mux *http.ServeMux, enablePprof bool, p ...DebugPage) {
55 registerOnce.Do(register)
56
57 trace.AuthRequest = func(req *http.Request) (any, sensitive bool) {
58 return true, true
59 }
60
61 index := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
62 _ = debugTmpl.Execute(w, struct {
63 DebugPages []DebugPage
64 EnablePprof bool
65 }{
66 DebugPages: p,
67 EnablePprof: enablePprof,
68 })
69 })
70 mux.Handle("/debug", index)
71 mux.Handle("/vars", http.HandlerFunc(expvarHandler))
72 mux.Handle("/gc", http.HandlerFunc(gcHandler))
73 mux.Handle("/freeosmemory", http.HandlerFunc(freeOSMemoryHandler))
74 if enablePprof {
75 mux.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))
76 mux.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
77 mux.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
78 mux.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
79 mux.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
80 }
81 mux.Handle("/debug/requests", http.HandlerFunc(trace.Traces))
82 mux.Handle("/debug/events", http.HandlerFunc(trace.Events))
83 mux.Handle("/metrics", promhttp.Handler())
84}
85
86func register() {
87 promauto.NewGaugeVec(prometheus.GaugeOpts{
88 Name: "zoekt_version",
89 }, []string{"version"}).WithLabelValues(index.Version).Set(1)
90}