golang的debug 方法 以及pprof的使用

2020/09/08 阅读: 技术

docker 的pprof 真的需要reload吗

看下源码 api/server/server.go

// InitRouter initializes the list of routers for the server.
// This method also enables the Go profiler.
func (s *Server) InitRouter(routers ...router.Router) {
	s.routers = append(s.routers, routers...)

	m := s.createMux()
	s.routerSwapper = &routerSwapper{
		router: m,
	}
}

s.createMux()

debugRouter := debug.NewRouter()
s.routers = append(s.routers, debugRouter)
for _, r := range debugRouter.Routes() {
	f := s.makeHTTPHandler(r.Handler())
	m.Path("/debug" + r.Path()).Handler(f)
}

既然默认都是有router的,我们把sock文件绑到端口上就可以使用pprof了

wget http://www.dest-unreach.org/socat/download/socat-1.7.3.3.tar.gz
socat -d -d TCP-LISTEN:5555,fork,bind=0.0.0.0 UNIX:/var/run/docker.sock
go tool pprof http://127.0.0.1:5555/debug/pprof/heap

还有没有更丝滑的方法呢

curl --unix-socket /var/run/docker.sock http://./debug/pprof/goroutine?debug=2

目录