pprof is a profiling tool and profile format closely integrated with Go. The net/http/pprof package exposes runtime profiles through HTTP handlers, while the broader pprof ecosystem analyzes and visualizes CPU, goroutine, heap, and related runtime data. section 9.1.1 section 9.1.2
import (
_ "net/http/pprof" #1
)
func quickTask() (result string) { #2
}
func slowTask() (result string) { #3
}
func main() {
go func() { #4
http.ListenAndServe("localhost:6060", nil)
}()
for true {
result := quickTask()
fmt.Printf("Quick task result is %s\n", result)
result = slowTask()
fmt.Printf("Slow task result is %s\n", result)
}
}
A blank import registers the profiling handlers, while a local HTTP server makes the profiles available at the debugging endpoint. section 9.1.1
A practical pprof investigation usually follows this workflow:
- Run the Go application with pprof handlers and inspect the available profile types at the local debug endpoint. section 9.1.1
- Collect a time-bounded CPU profile through the HTTP endpoint with go tool pprof. section 9.1.1
- Use the interactive shell to rank functions by flat or cumulative time and focus investigation on expensive call paths. section 9.1.1
- Render call graphs or flame graphs when a visual representation makes relationships easier to inspect; Graphviz may be needed for graph images. section 9.1.1 section 9.1.2
Finding the slower task
A sample Go program repeatedly runs quickTask and slowTask while exposing pprof on localhost:6060. A 30-second CPU capture shows slowTask consuming almost half of the sampled runtime, while quickTask accounts for less than 6%; the interactive top view makes that contrast visible. section 9.1.1 section 9.1.1
Beyond the Go HTTP handlers, pprof defines a protobuf-based representation for profiles. The format efficiently records samples, mappings, locations, functions, timing, and related metadata, but is less convenient to inspect than JSON; protoc and other protobuf tools can decode it for debugging. section 9.1.2 section 9.1.2
Profiling an OpenTelemetry collector
An OpenTelemetry collector can enable its pprof extension on port 1777, while Parca scrapes the exposed profiles and provides a searchable view such as Memory In-Use Bytes and an icicle graph. This turns pprof output into a continuous-profiling troubleshooting workflow. section 9.1.5 section 9.1.5

