使用应用程序PID而不是http端点

时间:2015-07-08 00:01:39

标签: go pprof

现在,我使用go工具pprof配置Go应用程序,如下所示:

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

我想在任意Go进程上使用pprof工具,该进程在未知端口上运行http服务器。我对该过程的唯一信息是它的PID。我需要做两件事之一:

  • 从其PID中获取Go进程端口号。
  • 直接描述正在运行的进程。例如,go tool pprof 10303之类的内容,其中PID为10303。

其中任何一种都有效吗?

1 个答案:

答案 0 :(得分:1)

Service Discovery旨在解决此类问题。 一个简单的解决方案是为每个PID创建一个tmp文件,将每个端口写入相应的文件。并在需要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()
go go tool bash:go tool http://localhost:`cat / tmp / myport_10303` / debug / pprof / profile

未经过实际测试,可能是一些语法错误

更新:

另一种不改变的方法是,使用像netstat / lsof这样的bash命令找出go进程的监听端口。像:

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

不是最好的bash脚本我认为,仅供参考。