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