How to Monitor Goroutine and Memory Usage in Production

Import net/http/pprof to expose live goroutine and memory metrics via a local HTTP endpoint.

Enable the built-in pprof HTTP server to monitor goroutines and memory usage in real time.

The Go runtime exposes profiling data via net/http/pprof without requiring custom instrumentation. Import the package to register handlers, then query /debug/pprof/goroutine for active goroutines or /debug/pprof/heap for memory statistics. Use GODEBUG=tracebacklabels=0 if you need to hide goroutine labels from stack traces for security reasons.

import _ "net/http/pprof"

func main() {
    go func() {
        log.Println(http.ListenAndServe("localhost:6060", nil))
    }()
    // Your application logic here
}

Visit http://localhost:6060/debug/pprof/goroutine or http://localhost:6060/debug/pprof/heap in your browser to view live metrics.