2016-09-25 08:39:20 +01:00
|
|
|
// Package pprof implement a debug endpoint for getting profiles using the
|
|
|
|
|
// go pprof tooling.
|
2016-04-28 10:26:58 +01:00
|
|
|
package pprof
|
|
|
|
|
|
|
|
|
|
import (
|
2016-04-29 07:28:35 +01:00
|
|
|
"net"
|
2016-04-28 10:26:58 +01:00
|
|
|
"net/http"
|
2016-04-29 07:28:35 +01:00
|
|
|
pp "net/http/pprof"
|
2016-04-28 10:26:58 +01:00
|
|
|
)
|
|
|
|
|
|
2016-09-23 09:14:12 +01:00
|
|
|
type handler struct {
|
2017-04-24 10:27:26 -04:00
|
|
|
addr string
|
|
|
|
|
ln net.Listener
|
|
|
|
|
mux *http.ServeMux
|
2016-04-28 10:26:58 +01:00
|
|
|
}
|
|
|
|
|
|
2016-09-23 09:14:12 +01:00
|
|
|
func (h *handler) Startup() error {
|
2017-04-24 10:27:26 -04:00
|
|
|
ln, err := net.Listen("tcp", h.addr)
|
2016-09-21 17:01:19 +01:00
|
|
|
if err != nil {
|
2018-04-19 07:41:56 +01:00
|
|
|
log.Errorf("Failed to start pprof handler: %s", err)
|
2016-04-29 07:28:35 +01:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 17:01:19 +01:00
|
|
|
h.ln = ln
|
|
|
|
|
|
2016-04-29 07:28:35 +01:00
|
|
|
h.mux = http.NewServeMux()
|
|
|
|
|
h.mux.HandleFunc(path+"/", pp.Index)
|
|
|
|
|
h.mux.HandleFunc(path+"/cmdline", pp.Cmdline)
|
|
|
|
|
h.mux.HandleFunc(path+"/profile", pp.Profile)
|
|
|
|
|
h.mux.HandleFunc(path+"/symbol", pp.Symbol)
|
|
|
|
|
h.mux.HandleFunc(path+"/trace", pp.Trace)
|
|
|
|
|
|
2016-04-28 10:26:58 +01:00
|
|
|
go func() {
|
2016-04-29 07:28:35 +01:00
|
|
|
http.Serve(h.ln, h.mux)
|
2016-04-28 10:26:58 +01:00
|
|
|
}()
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2016-04-29 07:28:35 +01:00
|
|
|
|
2016-09-23 09:14:12 +01:00
|
|
|
func (h *handler) Shutdown() error {
|
2016-04-29 07:28:35 +01:00
|
|
|
if h.ln != nil {
|
|
|
|
|
return h.ln.Close()
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
path = "/debug/pprof"
|
|
|
|
|
)
|