golang没有用于CPU的/debug/pprof/profile端点,但其他功能都有。

huangapple go评论89阅读模式
英文:

golang no /debug/pprof/profile endpoint for cpu while having everything else

问题

我对一个问题感到困惑,我无法对我的 Golang 程序进行性能分析,我已经在 /debug/pprof 下设置了所有其他的端点,但是没有 /debug/pprof/profile 用于 CPU 分析。有人曾经遇到过这样的问题吗?

go tool pprof http://localhost:7778/debug/pprof/profile
Fetching profile from http://localhost:7778/debug/pprof/profile
Please wait... (30s)
server response: 404 Not Found

/debug/pprof/

profiles:
19	block
31	goroutine
10	heap
0	mutex
11	threadcreate

full goroutine stack dump

我是这样设置性能分析的:

r := http.NewServeMux()
r.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))

可能的原因是什么?

更新:运行

http.ListenAndServe("localhost:4444", nil)

(即启动默认的 HTTP 服务器)可以解决我的自定义端点的问题。

英文:

I'm quite puzzled by a problem that I can't profile my golang program, I have all other endpoints under /debug/pprof but not /debug/pprof/profile for CPU profiling
Have anyone ever stumbled across such issue?

go tool pprof http://localhost:7778/debug/pprof/profile
Fetching profile from http://localhost:7778/debug/pprof/profile
Please wait... (30s)
server response: 404 Not Found

while

/debug/pprof/

profiles:
19	block
31	goroutine
10	heap
0	mutex
11	threadcreate

full goroutine stack dump

I setup profiling in this way

r := http.NewServeMux()
r.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))

What can be the cause?

Updated: running

http.ListenAndServe("localhost:4444", nil)

(i.e. starting default http server) fixes the problem for my custom endpoint

答案1

得分: 3

我遇到了和你一样的错误,我按照文档进行了注册。如果有人遇到同样的问题,解决方法是需要添加http.DefaultServeMux,所以完整的代码应该是:

r := mux.NewRouter()

r.PathPrefix("/debug/").Handler(http.DefaultServeMux)

r.HandleFunc("/debug/pprof/", pprof.Index)
r.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
r.HandleFunc("/debug/pprof/profile", pprof.Profile)
r.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
r.HandleFunc("/debug/pprof/trace", pprof.Trace)

参考链接在这里

英文:

I stumbled the same error as you, I had registered all according to the documentation. In case anyone faced the same problem, it turned out that you need to add http.DefaultServeMux, so the complete lines should be:

> r := mux.NewRouter()
>
> r.PathPrefix("/debug/").Handler(http.DefaultServeMux)
>
> r.HandleFunc("/debug/pprof/", pprof.Index)
> r.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
> r.HandleFunc("/debug/pprof/profile", pprof.Profile)
> r.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
> r.HandleFunc("/debug/pprof/trace", pprof.Trace)

reference is here

答案2

得分: 1

我找到了,我没有像在这里的init函数中那样注册所有的处理程序。
https://golang.org/src/net/http/pprof/pprof.go

英文:

Found it, I didn't register all the handlers as done in init here
https://golang.org/src/net/http/pprof/pprof.go

huangapple
  • 本文由 发表于 2017年5月19日 16:51:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/44065511.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定