我有一个现有的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())当我试图访问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远程客户端也是如此:
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发布于 2015-06-01 02:47:04
文档中没有明确提到这一点,但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))
}如果你没有使用默认的多路复用器,你只需要在你正在使用的任何多路复用器中注册任何/所有你想要的多路复用器,例如mymux.HandleFunc("…", pprof.Index)等。
或者,您可以使用默认多路复用器you've shown侦听单独的端口(如果需要,也可以仅绑定到本地主机)。
发布于 2018-10-10 05:52:40
如果您使用的是github.com/gorilla/mux.Router,则可以简单地将任何以/debug/为前缀的请求传递给http.DefaultServeMux。
import _ "net/http/debug"
router := mux.NewRouter()
router.PathPrefix("/debug/").Handler(http.DefaultServeMux)发布于 2015-06-01 02:09:26
看起来问题出在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())https://stackoverflow.com/questions/30560859
复制相似问题