2015-05-31 17 views
7

Ho un server http esistente che vorrei profilare. Ho incluso _ "net/http/pprof" ai miei importazioni, e ho già http server in esecuzione:Impossibile usare go tool pprof con un server esistente

router := createRouter() 
server := &http.Server { 
    Addr:   ":8080", 
    Handler:  router, 
    ReadTimeout: 15*time.Second, 
    WriteTimeout: 15*time.Second, 
// MaxHeaderBytes: 4096, 
} 

log.Fatal(server.ListenAndServe()) 

Quando sto cercando di accedere http://localhost:8080/debug/pprof/ ottengo 404 page not found.

Questo è quello che ottengo quando si utilizza go tool pprof su un computer locale:

[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 

Lo stesso per un client remoto:

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 

risposta

11

Non è esplicitamente menzionato nella documentazione, ma net/http/pprof registra solo i suoi gestori con http.DefaultServeMux.

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

Se non si utilizza il mux di default non resta che registrare qualsiasi/tutti quelli che si desidera con tutto ciò mux che si sta utilizzando, ad esempio, qualcosa come mymux.HandleFunc("…", pprof.Index), ecc.

In alternativa è possibile ascoltare su una porta separata (anche possibilmente associato solo a localhost se desiderato) con il mux predefinito come you've shown.

8

Sembra che il problema era in un *mux.Router utilizzato da github.com/gorilla/mux che ho usato come Handler nell'istanza http.Server.

Soluzione: basta lanciare un altro del server solo per il 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())