Skip to content

Lesson 4 of 13 · 15m

Continuous Profiling: The Fourth Signal

Traces say which span is slow; pprof, async-profiler, and Pyroscope say which function inside the span ate the CPU.

The trace says the POST /checkout span is slow. The span is yours — no DB call, no HTTP client inside it, just your code burning 400ms of CPU. Metrics and traces cannot see inside the span. A profile can. Continuous profiling is the fourth signal: a sampler that snapshots every thread's stack a hundred times a second and reports which functions ate the CPU.

Reading a profile

A CPU profile aggregates stacks into self time (samples where the function itself was running) and cumulative time (samples anywhere under it). chargeCustomer → serializeCart → json.Marshal → regexp.Compile at 34% of wall time means the regex compile inside the hot loop — the thing three reviewers missed — is now a number. Brendan Gregg's flame graph renders this readably: width equals samples, so the wide tower is where the time went, and you read top-down for causality, bottom-up for entry points.

The mechanics matter when you interpret it: a CPU sampler wakes on a timer and records each running thread's stack — what it sees is on-CPU time. A handler parked on a lock or a socket is invisible to it, which is why wall-clock/off-CPU profiling (block profile, goroutine dumps) is a separate tool for a separate question. "The span is slow but the profile is empty" means the time was spent waiting, not computing — look at the block profile, not the CPU one.

pprof is the reference implementation: Go's net/http/pprof exposes /debug/pprof/profile?seconds=30 plus heap, goroutine, block, mutex, allocs, and threadcreate endpoints, and go tool pprof -http=:8080 serves the flame graph, top-function list, and diff views in a browser. async-profiler does the equivalent for JVMs without safepoint bias — the classic JFR sampler misses frames outside safepoints, which is a real measurement error, not trivia. py-spy samples Python without touching the process.

Parca and Grafana Pyroscope run the always-on version: agents sample continuously, profiles upload to a server, and you diff yesterday against today. Grafana Labs acquired Pyroscope in March 2024 and folded it into the stack alongside Loki, Tempo, and Mimir. The diff view — CPU per function, this deploy versus last — is what turns "feels slower since Tuesday" into a named culprit.

The failure modes only profiles catch

  • The JSON serialization of a response that quietly grew to 200KB — the trace says the handler is slow; the profile says encoding/json is 60% of it.
  • GC pressure: the allocation profile shows a per-request []byte copy nobody knew about; p99 tracks the pauses that follow.
  • Lock contention: a goroutine dump shows 400 goroutines parked on one mutex — the mystery latency that survived three sprints. Note that blocked time barely shows in CPU profiles; goroutine and block profiles exist for exactly that.
  • Regex. Always worth checking for regex.

What it costs

CPU sampling adds roughly 1–5% overhead — cheap enough for production, which is the entire point of continuous over ad-hoc: a profiler you only run during an incident arrives after the interesting stacks are gone. Allocation and block profiling cost more; enable selectively. Profile data is per-process and additive — it costs CPU cycles, not time series.

Concrete practice

  • Expose pprof/async-profiler on an internal port only — /debug/pprof serves heap dumps and goroutine stacks, not just CPU profiles. On a public port it is a leak surface with a URL.
  • Profile before optimizing. The slow function is almost never the one you suspected, and the profile settles arguments the code review could not.
  • Diff profiles across deploys. "CPU +12% since Tuesday" is a regression report, not a vibe.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.