2015-05-31 29 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()) 
相關問題