net/http/pprof over UNIX sockets

191 views
Skip to first unread message

Albert Strasheim

unread,
Aug 28, 2012, 7:27:28 AM8/28/12
to golan...@googlegroups.com
Hello all

Sometimes when you have many services, it can be hard to pick port numbers where you want to expose net/http/pprof info. And sometimes you don't want to expose this info outside your server (you can set up a firewall, of course).

On Linux, another option is to use UNIX sockets with abstract paths containing the process PID instead:

Call the following function from your main:

func ListenPprof() {
path := "@/pprof/" + strconv.Itoa(os.Getpid())
l, err := net.Listen("unix", path)
if err != nil {
panic(err)
}
mux := http.NewServeMux()
mux.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))
mux.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
mux.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
mux.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
// ignore errors from Serve
go http.Serve(l, mux)
}

and then do

echo -e "GET /debug/pprof/goroutine?debug=2 HTTP/1.1\n" | socat STDIO ABSTRACT:/pprof/<pid>

This seems to work well for everything except the CPU profiles, but that's probably a socat issue I haven't figured out yet.

Maybe this is useful for someone.

Cheers

Albert
Reply all
Reply to author
Forward
0 new messages