本文介绍了无法在现有服务器上使用Go工具pprof的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要配置的现有http服务器.我已将_ "net/http/pprof"包含在我的导入文件中,并且我已经在运行http服务器:

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())

当我尝试访问 http://localhost:8080/debug/pprof/我得到404 page not found.

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

这就是在本地计算机上使用go tool pprof时得到的:

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

与远程客户端相同:

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注册其处理程序.

It's not explicitly mentioned in the documentation, but net/http/pprof only registers its handlers with 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)之类的

If you're not using the default mux you just have to register any/all of those you want with whatever mux you're using, e.g. something like mymux.HandleFunc("…", pprof.Index), etc.

或者,您可以使用默认的mux在单独的端口(如果需要,也可以绑定到本地主机)上进行监听,如 .

Alternatively you can listen on a separate port (also possibly bound to only localhost if desired) with the default mux as you've shown.

这篇关于无法在现有服务器上使用Go工具pprof的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 07:35