Profiling
Enable Go pprof profiling endpoints for runtime performance analysis, memory inspection, and debugging.
The router package can start a dedicated HTTP profiling server using Go’s standard net/http/pprof tooling.
Profiling is useful for:
- performance analysis
- memory inspection
- CPU profiling
- goroutine debugging
- allocation tracking
- production troubleshooting
Enable Profiling
r.EnableProfiling("localhost:6060")This starts a dedicated internal HTTP server exposing standard Go profiling endpoints.
Default dashboard:
http://localhost:6060/debug/pprof/Example:
package main
import (
"net/http"
"github.com/netlifeguru/router"
)
func main() {
r := router.New()
r.EnableProfiling("localhost:6060")
r.GET("/", func(w http.ResponseWriter, req *http.Request, ctx *router.Context) {
w.Write([]byte("hello"))
})
r.ListenAndServe(":8080")
}Available Endpoints
Once enabled, the following endpoints become available:
/debug/pprof/
/debug/pprof/heap
/debug/pprof/profile
/debug/pprof/goroutine
/debug/pprof/allocs
/debug/pprof/block
/debug/pprof/mutexThese endpoints are provided by Go’s standard profiling package.
Example CPU Profile
Capture a 30-second CPU profile:
go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30Open the interactive profiling UI:
go tool pprof -http=:8081 cpu.profSecurity Notes
Profiling endpoints expose internal runtime information and should not be publicly accessible.
Recommended practices:
- bind profiling to
localhost - expose profiling only inside trusted networks
- disable profiling in public production environments
- protect profiling endpoints behind authentication or firewalls
Example:
r.EnableProfiling("127.0.0.1:6060")Notes
The profiling server runs independently from the main HTTP router server.
This allows profiling endpoints to remain isolated from public application traffic while still providing full runtime inspection capabilities.
Health Checks
Add liveness and readiness endpoints to expose service health for containers, load balancers, and orchestration platforms.
Multi-Server Support
Run multiple HTTP listeners simultaneously using a single router instance for internal APIs, public endpoints, admin panels, or multi-port deployments.