golang – net/http/pprof不起作用

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

golang - net/http/pprof doesn't work

问题

我有一个客户端的HTTP服务:

s := &http.Server{
    Addr:         config.Port,
    Handler:      Controller.Log(http.DefaultServeMux),
    ReadTimeout:  3 * time.Second,
    WriteTimeout: 3 * time.Second,
}

http.HandleFunc("/exapmle/router/", exampleFunc)

err := s.ListenAndServe()
if err != nil {
    log.Critical(err)
    os.Exit(1)
}

它不起作用:

go tool pprof http://localhost:8201/debug/pprof/profile

返回错误:

Failed to fetch http://localhost:8201/debug/pprof/profile?seconds=30

谢谢。

编辑:
我认为问题是我重写了默认的HTTP服务器,net/http/pprof包注入了HTTP处理程序:

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

这个处理程序在我的代码中不起作用。

英文:

I have customer http service:

    s := &http.Server{
            Addr:         config.Port,
            Handler:      Controller.Log(http.DefaultServeMux),
            ReadTimeout:  3 * time.Second,
            WriteTimeout: 3 * time.Second,
    }

    http.HandleFunc("/exapmle/router/", exampleFunc)

    err := s.ListenAndServe()
    if err != nil {
            log.Critical(err)
            os.Exit(1)
    }

It's does't work:

go tool pprof http://localhost:8201/debug/pprof/profile

return error:

Failed to fetch http://localhost:8201/debug/pprof/profile?seconds=30

Thanks.

edit:
I think the problem is i rewrite default http server, net/http/pprof package inject http handler:

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

the handler does not work in my code.

答案1

得分: 10

你将"WriteTimeout"设置为小于配置文件写入时间。

在执行pprof.StartCPUProfile()时,它已经开始写入,请参考:

因此,http.WriteTimeout必须大于配置文件写入时间。

对于我的英语不好,我感到很抱歉。

英文:

You set "WriteTimeout" less then profile write time.

on pprof.StartCPUProfile() execute, It already start write, see:

So http.WriteTimeout must be greater than profile write time.

Sorry for my poor English.

huangapple
  • 本文由 发表于 2013年12月31日 16:26:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/20854237.html
匿名

发表评论

匿名网友

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

确定