func handler() http.Handler { info := struct { Profiles []*pprof.Profile Token string }{ Profiles: pprof.Profiles(), } h := func(w http.ResponseWriter, r *http.Request) { // get the last path, allowing this to be mounted under any prefix split := strings.Split(r.URL.Path, "/") name := split[len(split)-1] switch name { case "": // Index page. if err := indexTmpl.Execute(w, info); err != nil { fmt.Fprintf(w, "something went wrong - %s", err) return } case "cmdline": nhpprof.Cmdline(w, r) case "profile": nhpprof.Profile(w, r) case "trace": nhpprof.Trace(w, r) case "symbol": nhpprof.Symbol(w, r) default: // Provides access to all profiles under runtime/pprof nhpprof.Handler(name).ServeHTTP(w, r) } } return http.HandlerFunc(h) }
// title: profile trace handler // path: /debug/pprof/trace // method: GET // responses: // 200: Ok // 401: Unauthorized func traceHandler(w http.ResponseWriter, r *http.Request, t auth.Token) error { if !permission.Check(t, permission.PermDebug) { return permission.ErrUnauthorized } pprof.Trace(w, r) return nil }
// 根据 config.Pprof 决定是否包装调试地址 func (cfg *Config) buildPprof(h http.Handler) http.Handler { if len(cfg.Pprof) > 0 { logs.Debug("web:", "开启了调试功能,地址为:", cfg.Pprof) return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if !strings.HasPrefix(r.URL.Path, cfg.Pprof) { h.ServeHTTP(w, r) return } path := r.URL.Path[len(cfg.Pprof):] switch path { case "cmdline": pprof.Cmdline(w, r) case "profile": pprof.Profile(w, r) case "symbol": pprof.Symbol(w, r) case "trace": pprof.Trace(w, r) default: pprof.Index(w, r) } }) } return h }
func (*Pprof) Index(w http.ResponseWriter, r *http.Request, p httprouter.Params) { switch p.ByName("name") { case "profile": pprof.Profile(w, r) case "symbol": pprof.Symbol(w, r) case "trace": pprof.Trace(w, r) case "cmdline": pprof.Cmdline(w, r) default: pprof.Index(w, r) } }
//Trace profiler on wrapper RouteFunction func TraceHandler(fn restful.RouteFunction) restful.RouteFunction { return func(r *restful.Request, w *restful.Response) { pprof.Trace(w.ResponseWriter, r.Request) fn(r, w) } }
// Trace provides the pprof Trace endpoint func (p *PProf) Trace(c *Context, next NextHandler) { pprof.Trace(c.Res, c.Req) next(c) }