liveBook · Manning

Pprof

As explained in Cloud Observability in Action

net/http/pprof is a Go package that exposes runtime profiling data through HTTP handlers.

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

How Cloud Observability in Action covers it

How deep it goes
Explains HTTP exposure, interactive analysis, graph rendering, and protobuf structure, but stops short of detailed profile internals.
Examples
Uses a Go CPU workload, goroutine call graphs, pprof shell rankings, protobuf decoding, and an OpenTelemetry collector scraped by Parca.
What it assumes
Starts with a runnable Go program and demonstrates profiling before introducing the underlying protobuf representation.
What you can do after
Diagnose/debug Go and service-runtime hotspots by collecting profiles, ranking functions, rendering graphs, and querying historical samples.
How it connects
Connects profiling with Go, continuous profiling, Protocol Buffers, Graphviz, OpenTelemetry, Parca, and eBPF.

Also explained in

Compare how 2 books explain pprof →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Cloud Observability in Action. The section links point to the parts of the book it draws on.