Skip to content

Middleware

pprof

Expose Go's runtime profiler behind authentication for CPU, memory, and goroutine analysis.

Pprof serves the standard net/http/pprof handlers, so go tool pprof can profile a running service. It answers requests below /debug/pprof and passes everything else through.

Profiles reveal internals and can slow a server down, so always put authentication in front:

app.UsePrefix("/debug/pprof",
middleware.BasicAuth(middleware.BasicAuthStatic("ops", os.Getenv("PPROF_PASSWORD"))),
middleware.Pprof(),
)

UsePrefix runs before routing, in order, so every profiling request is authenticated first.

Then, from your machine:

Terminal window
go tool pprof -http=:0 "https://ops:$PPROF_PASSWORD@api.example.com/debug/pprof/profile?seconds=30"
app.UsePrefix("/internal/pprof", requireAdmin, middleware.PprofWithPrefix("/internal/pprof"))