58 lines
1.7 KiB
Go
58 lines
1.7 KiB
Go
|
package snap
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/json"
|
||
|
"expvar"
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"net/http"
|
||
|
"net/http/pprof"
|
||
|
"runtime"
|
||
|
|
||
|
"github.com/gorilla/mux"
|
||
|
)
|
||
|
|
||
|
func stackHandler(w http.ResponseWriter, r *http.Request) {
|
||
|
http.Redirect(w, r, "/sovereign/debug/pprof/goroutine?debug=2", http.StatusSeeOther)
|
||
|
}
|
||
|
|
||
|
// copy pasted from http://golang.org/src/expvar/expvar.go#L308
|
||
|
func varsHandler(w http.ResponseWriter, r *http.Request) {
|
||
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||
|
|
||
|
buf := make([]byte, 0, 16384)
|
||
|
body := bytes.NewBuffer(buf)
|
||
|
|
||
|
fmt.Fprintf(body, "{")
|
||
|
expvar.Do(func(kv expvar.KeyValue) {
|
||
|
fmt.Fprintf(body, "%q: %s,", kv.Key, kv.Value)
|
||
|
})
|
||
|
|
||
|
fmt.Fprintf(body, "%q: %d", "NumGoRoutines", runtime.NumGoroutine())
|
||
|
fmt.Fprintf(body, "}")
|
||
|
|
||
|
var prettyJSON bytes.Buffer
|
||
|
err := json.Indent(&prettyJSON, body.Bytes(), "", " ")
|
||
|
if err != nil {
|
||
|
log.Println(err, string(body.Bytes()))
|
||
|
}
|
||
|
|
||
|
w.Write(prettyJSON.Bytes())
|
||
|
}
|
||
|
|
||
|
func setupDebugHandler(r *mux.Router) {
|
||
|
r.HandleFunc("/stack", stackHandler)
|
||
|
r.HandleFunc("/vars", varsHandler)
|
||
|
r.HandleFunc("/pprof/", http.HandlerFunc(pprof.Index))
|
||
|
r.HandleFunc("/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
|
||
|
r.HandleFunc("/pprof/profile", http.HandlerFunc(pprof.Profile))
|
||
|
r.HandleFunc("/pprof/symbol", http.HandlerFunc(pprof.Symbol))
|
||
|
r.HandleFunc("/pprof/trace", http.HandlerFunc(pprof.Trace))
|
||
|
r.HandleFunc("/pprof/goroutine", pprof.Handler("goroutine").ServeHTTP)
|
||
|
r.HandleFunc("/pprof/allocs", pprof.Handler("allocs").ServeHTTP)
|
||
|
r.HandleFunc("/pprof/block", pprof.Handler("block").ServeHTTP)
|
||
|
r.HandleFunc("/pprof/heap", pprof.Handler("heap").ServeHTTP)
|
||
|
r.HandleFunc("/pprof/mutex", pprof.Handler("mutex").ServeHTTP)
|
||
|
}
|