2015-05-31 67 views
7

我有一个现有的http服务器,我想要配置它。我已经包含_ "net/http/pprof"我进口,我已经有HTTP服务器上运行:不能在现有服务器上使用go工具pprof

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时得到:

[email protected]:~/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 

[email protected]:~/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 

回答

11

它没有明确的文件中提到,但net/http/pprof只有http.DefaultServeMux注册其处理程序。

source

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

如果你不使用默认的复用,你只需要注册任何/所有那些你想与你使用任何MUX的,例如如mymux.HandleFunc("…", pprof.Index)

另外,您可以在默认多路复用器上侦听单独的端口(如果需要,也可能只绑定到本地主机),如you've shown

8

貌似这个问题是从github.com/gorilla/mux这在我以前使用的*mux.Router作为我的http.Server实例中的Handler

解决方案:只要启动一个更服务器只为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()) 
相关问题