2017-07-03 06:52:27 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"expvar"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/pprof"
|
2020-10-26 04:41:41 +00:00
|
|
|
"runtime"
|
2017-07-03 06:52:27 +00:00
|
|
|
|
2017-07-19 01:05:19 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-07-03 06:52:27 +00:00
|
|
|
"golang.org/x/net/trace"
|
|
|
|
)
|
|
|
|
|
|
|
|
func setupDebugHandlers(addr string) error {
|
|
|
|
m := http.NewServeMux()
|
|
|
|
m.Handle("/debug/vars", expvar.Handler())
|
|
|
|
m.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))
|
|
|
|
m.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
|
|
|
|
m.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
|
|
|
|
m.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
|
|
|
|
m.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
|
2020-10-26 04:41:41 +00:00
|
|
|
m.Handle("/debug/requests", http.HandlerFunc(trace.Traces))
|
|
|
|
m.Handle("/debug/events", http.HandlerFunc(trace.Events))
|
|
|
|
|
|
|
|
m.Handle("/debug/gc", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
runtime.GC()
|
|
|
|
logrus.Debugf("triggered GC from debug endpoint")
|
|
|
|
}))
|
2017-07-03 06:52:27 +00:00
|
|
|
|
|
|
|
// setting debugaddr is opt-in. permission is defined by listener address
|
|
|
|
trace.AuthRequest = func(_ *http.Request) (bool, bool) {
|
|
|
|
return true, true
|
|
|
|
}
|
|
|
|
|
|
|
|
l, err := net.Listen("tcp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logrus.Debugf("debug handlers listening at %s", addr)
|
|
|
|
go http.Serve(l, m)
|
|
|
|
return nil
|
|
|
|
}
|