import (
_ "net/http/pprof"
"net/http"
)
//...
return http.ListenAndServe(":8081", nil)
import (
"net/http"
"net/http/pprof"
)
//...
mux := http.NewServeMux()
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
// ...
server := &http.Server{
Addr: ":8081",
Handler: mux,
}
return server.ListenAndServe()
kubectl -n my-ns port-forward svc/my-app 8081:8081
curl -s http://127.0.0.1:8081/debug/pprof/heap > ./heap.out
As an alternative you can use speedscope.
go tool pprof -http=:8080 ./heap.out
inuse_space
: Amount of memory allocated and not released yet (Important).inuse_objects
: Amount of objects allocated and not released yet.alloc_space
: Total amount of memory allocated (regardless of released).alloc_objects
: Total amount of objects allocated (regardless of released).
kubectl -n my-ns port-forward svc/my-app 8081:8081
By default will be 30s
of CPU profile:
curl -s http://127.0.0.1:8081/debug/pprof/profile > ./cpu.out
Customize the time with ?seconds=N
:
curl -s http://127.0.0.1:8081/debug/pprof/profile?seconds=60 > ./cpu.out
As an alternative you can use speedscope.
go tool pprof -http=:8080 ./cpu.out
kubectl -n my-ns port-forward svc/my-app 8081:8081
By default will be 1s
of CPU trace profile:
curl -s http://127.0.0.1:8081/debug/pprof/trace > ./cpu-trace.out
Customize the time with ?seconds=N
:
curl -s http://127.0.0.1:8081/debug/pprof/trace?seconds=60 > ./cpu-trace.out
go tool trace -http=:8080 ./cpu-trace.out
This site recommends only exposing pprof on
loopback
which I think would improve this solution.https://docs.boostsecurity.io/rules/code-debugging-interface-publicly-exposed.html