首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在现有服务器上使用go工具pprof

无法在现有服务器上使用go工具pprof
EN

Stack Overflow用户
提问于 2015-06-01 02:09:26
回答 4查看 9.9K关注 0票数 18

我有一个现有的http服务器,我想要配置。我已经将_ "net/http/pprof"包含到我的导入中,并且我已经有了正在运行的http服务器:

代码语言:javascript
复制
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时得到的结果:

代码语言:javascript
复制
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

远程客户端也是如此:

代码语言:javascript
复制
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
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2015-06-01 02:47:04

文档中没有明确提到这一点,但net/http/pprof只向http.DefaultServeMux注册其处理程序。

source

代码语言:javascript
复制
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侦听单独的端口(如果需要,也可以仅绑定到本地主机)。

票数 27
EN

Stack Overflow用户

发布于 2018-10-10 05:52:40

如果您使用的是github.com/gorilla/mux.Router,则可以简单地将任何以/debug/为前缀的请求传递给http.DefaultServeMux

代码语言:javascript
复制
import _ "net/http/debug"
router := mux.NewRouter()
router.PathPrefix("/debug/").Handler(http.DefaultServeMux)
票数 14
EN

Stack Overflow用户

发布于 2015-06-01 02:09:26

看起来问题出在github.com/gorilla/mux中使用的*mux.Router中,我将其用作http.Server实例中的Handler

解决方案:只需为pprof再启动一台服务器

代码语言:javascript
复制
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())
票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30560859

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档