Course contents · Lesson 17 of 22

Continuous Profiling: Always-On Performance Insight

Profiles · Lesson 17 of 22 · 10 min Engineer

At 02:10 the checkout service's CPU jumps to 95% for twelve minutes, then settles. By the time you are awake and logged in, the spike is over, and a profile you take now shows a calm, healthy program. Continuous profiling solves this problem. A profiler runs all the time in production and keeps the results, so you can open the flame graph for 02:10 the next morning, as if you had been profiling at that very moment.

What continuous profiling is

Continuous profiling means running a sampling profiler on production services all the time, at a low rate, and sending every profile to a central server. The server stores each profile with its time range and with labels, key-value pairs such as service_name="checkout" or version="1.4.2". Later you ask for "checkout, CPU, from 02:05 to 02:25" and get one flame graph merged from just those profiles.

The profiles are the same kind you met in the profiling lesson, sent in small pieces. The Grafana Pyroscope SDK for Go, for example, uses Go's built-in CPU profiler at 100 Hz and uploads a new profile every 15 seconds by default. Each upload covers only the last 15 seconds, so the server can rebuild any window from those pieces.

Flowchart: an app with a profiler SDK pushes a profile every 15 seconds and an eBPF agent on the host samples every process at 19 Hz; both send to a Pyroscope server that stores profiles by time and labels, and a UI queries any time window and draws a flame graph
Profiles arrive in 15-second slices tagged with labels, so any service, version and time range can be reassembled into a flame graph later.

Behind the UI is an HTTP API. The query below asks a local Pyroscope server for the last 15 minutes of its own CPU profile. The long name is the profile type: process CPU time, measured in nanoseconds. The part in braces is a label selector, the same style you may know from Prometheus.

$ curl --get \
    --data-urlencode 'query=process_cpu:cpu:nanoseconds:cpu:nanoseconds{service_name="pyroscope"}' \
    --data-urlencode 'from=now-15m' \
    http://localhost:4040/pyroscope/render

The JSON reply has a flamebearer field, which holds the merged flame graph as arrays of function names and sample values, and a timeline field with the sample totals over time. The UI draws its timeline and flame graph from exactly these two fields.

What it costs

A profiler is only welcome in production if it is cheap. Most of its cost comes from walking the call stack at each sample, then compressing and uploading the profile. Samples are taken per unit of CPU time, never per request, so the overhead stays a roughly fixed fraction of CPU however much traffic you serve. A higher rate or deeper stacks raise it. Grafana's documentation puts Pyroscope's CPU overhead at about 2 to 5%, depending on these factors, and the client's memory use at usually under 50 MB per pod.

eBPF agents keep the rate low. Grafana Alloy's eBPF profiler samples 19 times a second by default instead of 100. That is enough because profiles from many hosts and many minutes add up to plenty of samples. Odd rates like 19 or 99 also avoid falling into step with work that runs on round intervals. Extra profile types cost more: lock and block profiles record events, and heap profiles add memory bookkeeping.

Treat published numbers as a starting point, not a promise. Turn profiling on for one instance, compare its CPU and latency with a neighbour that has it off, and only then roll it out everywhere. Remember the other bills too: network for uploads, and storage on the server for every profile you keep.

Collecting profiles: SDKs or eBPF

There are two ways to get profiles to the server. The first is a language SDK, a library you add to your program that pushes profiles itself. With the Pyroscope Go SDK, the setup is one call at startup:

import (
	"log"

	"github.com/grafana/pyroscope-go"
)

func main() {
	_, err := pyroscope.Start(pyroscope.Config{
		ApplicationName: "checkout",
		ServerAddress:   "http://pyroscope:4040",
		Tags:            map[string]string{"env": "prod", "version": "1.4.2"},
		ProfileTypes: []pyroscope.ProfileType{
			pyroscope.ProfileCPU,
			pyroscope.ProfileAllocSpace,
			pyroscope.ProfileInuseSpace,
		},
	})
	if err != nil {
		log.Fatal(err)
	}
	// start the HTTP server as usual
}

ApplicationName becomes the service_name label and Tags become extra labels. The SDK can send CPU, memory, goroutine, lock and block profiles, and the upload interval can be changed with UploadRate. SDKs exist for Go, Java, Python, Ruby, .NET, Node.js and Rust.

The second way is an eBPF agent. eBPF lets small, kernel-checked programs run inside the Linux kernel, so one agent per host can sample the stacks of every process without touching their code. It needs Linux, root or specific capabilities such as CAP_BPF and CAP_PERFMON, and readable symbols to turn addresses into function names. It records CPU stacks, and some agents can optionally record off-CPU waits too, but it cannot see memory allocations, so heap profiles still need an SDK.

What you can do with it

Compare before and after a deploy. Put the version in a label and a diff view shows what changed. Pyroscope's diff compares each function's share of the total, not raw time, so that busier traffic does not paint everything red. Red means a function's share grew, and green means it shrank.

Two flame graphs: v1.4.1 before a deploy with parseJSON at 66%, and v1.4.2 after as a diff where validateCoupon and regexp.Compile are new red frames under applyDiscount, which grew to 48%, while parseJSON fell to 45% in green
The new red frames point at the line to fix, while green on parseJSON only means its share fell, not that it got faster.

Find costs that never showed in tests. Real traffic has payload sizes, customers and feature flags that a test run lacks, so production profiles often show hot spots you have never seen. Track memory leaks over days. Graph a function's in-use memory across a week and a leak shows up as a line that climbs and never falls back.

CPU timeline for the checkout service from 14:00 to 15:00 with a spike to over 90% around 14:30; the selected window from 14:25 to 14:37 produces a flame graph dominated by main.exportOrders and gzip compression
Selecting just the spike merges only those profiles, so a cost that lasted twelve minutes is not diluted by the calm hour around it.

Jump from a trace to its code. With span profiles, the SDK labels each CPU sample with the ID of the request's entry trace span in that service (its local root span). A tracing UI such as Grafana Tempo can then open the flame graph for one slow request. Go takes one sample per 10 ms of CPU, so work shorter than that may collect no samples, and time spent waiting is off-CPU and never appears.

Choosing a tool, and questions to ask first

Two widely used open-source servers are Grafana Pyroscope and Parca, both with eBPF agents. Most commercial observability platforms offer continuous profiling as well. OpenTelemetry is standardising profiles as a signal of its own, and Alloy's eBPF profiler is built on the OpenTelemetry eBPF profiler.

Before turning it on, answer these questions:

  • Overhead: what did you measure on one instance, and with which profile types enabled?
  • Labels: are they low-cardinality? Never use user or request IDs; the cardinality lesson explains why the same rule applies here.
  • Privileges: is a privileged eBPF agent acceptable on your hosts, or should you use SDKs?
  • Data: function names, file paths and labels reveal how your code works. Who can see them, and how long do you keep them?

Check yourself

Why is a profile taken by hand after an incident often useless?

The spike is over, so the profiler records the calm program; continuous profiling has the profile from the moment of the spike.

Traffic doubled after a deploy. Why does a diff view compare shares rather than raw CPU time?

Raw time would grow everywhere with traffic, so comparing shares shows which functions changed relative to the rest.

An eBPF agent already runs on every host. Does that give you heap profiles for a Go service?

No: the agent samples CPU stacks, not allocations, so heap profiles need the Go SDK or Go's own pprof endpoint.