英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论