18

I have an existing http server which I would like to profile. I have included _ "net/http/pprof"to my imports, and I already have http server running:

router := createRouter()
server := &http.Server {
    Addr:           ":8080",
    Handler:        router,
    ReadTimeout:    15*time.Second,
    WriteTimeout:   15*time.Second,
//  MaxHeaderBytes: 4096,
}

log.Fatal(server.ListenAndServe())

When I'm trying to access http://localhost:8080/debug/pprof/ I get 404 page not found.

That's what I get when using go tool pprof on a local machine:

userver@userver:~/Desktop/gotest$ go tool pprof http://192.168.0.27:8080/
Use of uninitialized value $prefix in concatenation (.) or string at /usr/lib/go/pkg/tool/linux_amd64/pprof line 3019.
Read http://192.168.0.27:8080/pprof/symbol
Failed to get the number of symbols from http://192.168.0.27:8080/pprof/symbol

userver@userver:~/Desktop/gotest$ go tool pprof http://localhost:8080/debug/pprof/profile
Read http://localhost:8080/debug/pprof/symbol
Failed to get the number of symbols from http://localhost:8080/debug/pprof/symbol

Same for a remote client:

MacBookAir:~ apple$ go tool pprof http://192.168.0.27:8080/
Use of uninitialized value $prefix in concatenation (.) or string at /usr/local/Cellar/go/1.3.2/libexec/pkg/tool/darwin_amd64/pprof line 3027.
Read http://192.168.0.27:8080/pprof/symbol
Failed to get the number of symbols from http://192.168.0.27:8080/pprof/symbol
4

4 回答 4

27

它没有在文档中明确提及,但net/http/pprof仅将其处理程序注册为http.DefaultServeMux.

来源

func init() {
        http.Handle("/debug/pprof/", http.HandlerFunc(Index))
        http.Handle("/debug/pprof/cmdline", http.HandlerFunc(Cmdline))
        http.Handle("/debug/pprof/profile", http.HandlerFunc(Profile))
        http.Handle("/debug/pprof/symbol", http.HandlerFunc(Symbol))
        http.Handle("/debug/pprof/trace", http.HandlerFunc(Trace))
}

如果你没有使用默认的多路复用器,你只需要使用你正在使用的任何多路复用器注册任何/所有你想要的,例如mymux.HandleFunc("…", pprof.Index),等等。

或者,您可以使用所示的默认多路复用器在单独的端口(如果需要,也可能仅绑定到本地主机)上侦听。

于 2015-05-31T18:47:04.290 回答
14

如果您使用的是,github.com/gorilla/mux.Router您可以简单地将任何/debug/http.DefaultServeMux.

import _ "net/http/debug"
router := mux.NewRouter()
router.PathPrefix("/debug/").Handler(http.DefaultServeMux)
于 2018-10-09T21:52:39.683 回答
13

看起来问题出在我在我的实例中使用的一个使用*mux.Router中。github.com/gorilla/muxHandlerhttp.Server

解决方案:只需为以下服务器再启动一台服务器pprof

server := &http.Server {
    Addr:           ":8080",
    Handler:        router,
    ReadTimeout:    15*time.Second,
    WriteTimeout:   15*time.Second,
}
go func() {
    log.Println(http.ListenAndServe(":6060", nil))
}()
log.Fatal(server.ListenAndServe())
于 2015-05-31T18:09:26.300 回答
0

以下是如何将 pprof 与Gorilla mux结合使用。为了避免 HTTP 404 错误,我们需要明确指定路由,/debug/pprof/{cmd}因为 Gorilla 不做前缀路由:

router := mux.NewRouter()
router.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))
router.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
router.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
router.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
router.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
router.Handle("/debug/pprof/{cmd}", http.HandlerFunc(pprof.Index)) // special handling for Gorilla mux

err := http.ListenAndServe("127.0.0.1:9999", router)
log.Errorf("pprof server listen failed: %v", err) // note: http.ListenAndServe never returns nil
于 2022-02-08T10:49:48.217 回答