[英]Can't use go tool pprof with an existing server
我有一个现有的 http 服务器,我想对其进行分析。 我已将_ .net/http/pprof"
添加到我的导入中,并且我已经有 http 服务器在运行:
router := createRouter()
server := &http.Server {
Addr: ":8080",
Handler: router,
ReadTimeout: 15*time.Second,
WriteTimeout: 15*time.Second,
// MaxHeaderBytes: 4096,
}
log.Fatal(server.ListenAndServe())
当我尝试访问http://localhost:8080/debug/pprof/ 时,我得到404 page not found
。
这就是我在本地机器上使用go tool pprof
时得到的结果:
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
远程客户端也一样:
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
文档中未明确提及它,但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)
等。
或者,您可以使用默认的mux在单独的端口(如果需要,也可以绑定到localhost)上侦听,如所示 。
像问题看起来都在一个*mux.Router
从使用github.com/gorilla/mux
我作为Handler
在我http.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())
如果您使用的是github.com/gorilla/mux.Router
,则可以将任何带有/debug/
前缀的请求传递给http.DefaultServeMux
。
import _ "net/http/debug"
router := mux.NewRouter()
router.PathPrefix("/debug/").Handler(http.DefaultServeMux)
以下是如何将 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.