无法使用go工具pprof与现有服务器

Max*_*ysh 13 go pprof gorilla

我有一个现有的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())
Run Code Online (Sandbox Code Playgroud)

当我试图访问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
Run Code Online (Sandbox Code Playgroud)

对于远程客户端也是如此:

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
Run Code Online (Sandbox Code Playgroud)

Dav*_*e C 19

它没有在文档中明确提到,但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))
}
Run Code Online (Sandbox Code Playgroud)

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

或者,您可以使用默认的多路复用器监听单独的端口(如果需要,也可能仅绑定到localhost),如您所示.

  • 我有一个问题:一旦进入 `/debug/pprof/` 有几个链接会导致 404 页面。特别是“heap”和“allocs”404。正如上面所示,我没有看到“net/http/pprof/pprof.go”的“init()”函数中提供的这些路径的路由。我如何使这些内容易于访问?你知道我怎样才能得到那些@Dave C吗? (2认同)

Max*_*ysh 12

看起来,问题是在*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())
Run Code Online (Sandbox Code Playgroud)


Dev*_*las 6

如果您使用的是github.com/gorilla/mux.Router你可以简单的手头宽裕前缀的任何请求/debug/http.DefaultServeMux

import _ "net/http/debug"
router := mux.NewRouter()
router.PathPrefix("/debug/").Handler(http.DefaultServeMux)
Run Code Online (Sandbox Code Playgroud)