Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

golang - net/http/pprof doesn't work

Tags:

go

pprof

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.

like image 283
leiyonglin Avatar asked Dec 31 '13 08:12

leiyonglin


1 Answers

You set "WriteTimeout" less then profile write time.

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

  • http://golang.org/src/pkg/runtime/pprof/pprof.go#L565
  • http://golang.org/src/pkg/runtime/pprof/pprof.go#L594

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

Sorry for my poor English.

like image 189
Specode Avatar answered Nov 08 '22 08:11

Specode