简体   繁体   中英

go tool pprof using application PID instead of http endpoint

Right now, I profile Go applications using go tool pprof like this:

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

I want to use the pprof tool on an arbitrary Go process which is running a http server on an unknown port. The only information I have about the process is its PID. I need to do one of two things:

  • Get the Go processes port number from its PID.
  • Profile the running process directly. For example, something like go tool pprof 10303 where the PID is 10303.

Would either of these work?

Service Discovery is designed to resolve problems like this. A simple solution is to create a tmp file for every PID, writing each port to the according file. and read the port when you need go tool pprof .

http.ListenAndServe("localhost:" + PORT, nil)
tmpFilePath := filePath.Join(os.TempDir(), "myport_" + strconv.FormatInt(os.Getpid(), 10))
f, _ := os.OpenFile(tmpFilePath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
f.Write(byte[](strconv.FormatInt(PORT, 10)))
f.Close()

in go tool prof bash: go tool http://localhost:`cat /tmp/myport_10303`/debug/pprof/profile

not tested in real, maybe some syntax error

UPDATE:

another way not change go source is, use bash command like netstat/lsof to find out the listening port of the go process. like:

netstat -antp | grep 10303| grep LISTEN | awk '{ print $4 }' | awk -F: '{print $2}'

not best bash script I think, just for reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM