Senior
How to access the pprof profiler on a production server?
sobes.tech AI
Answer from AI
To access the pprof profiler on a production server, you typically need to:
- Enable the HTTP pprof server in your Go application by importing the
net/http/pprofpackage and registering handlers, for example:
import (
_ "net/http/pprof"
"net/http"
)
func main() {
go http.ListenAndServe(":6060", nil) // runs pprof on port 6060
// other code
}
-
On the production server, restrict access to the port (e.g., 6060) only to trusted IPs or via SSH tunnel to prevent public exposure of the profiler.
-
Connect to the profiler via a browser or using the command:
curl http://localhost:6060/debug/pprof/
or analyze with go tool pprof:
go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30
It is important to ensure secure access to pprof on production to avoid exposing internal information.