pprof package for memory and CPU profiling. This section provides guidance on setting up and retrieving profiles using pprof. These profiles are valuable for troubleshooting issues and can sometimes be the only way to gain meaningful context.
Enable Profiling
To enable thepprof endpoints, start the router with the following environment variable:
The
pprof HTTP server will be accessible at http://localhost:6060. Exposing this endpoint to production environments is highly discouraged due to security risks.-
/debug/pprof/heap— Memory profile. -
/debug/pprof/profile— CPU profile. -
/debug/pprof/goroutine— Goroutine profile. -
/debug/pprof/threadcreate— Thread creation profile. -
/debug/pprof/block— Block profile.
Downloading the Appropriate Profiles
To troubleshoot issues effectively, categorize them into the following three types:CPU Utilization
Memory Utilization
Blocking and synchronization
1. CPU Utilization
To investigate CPU-related issues, you can fetch the CPU profile by running:2. Memory Utilization
To diagnose memory-related issues, you can download the heap profile:3. Blocking and Synchronization
To identify deadlocks or goroutine-related issues, you can fetch the goroutine profile:-
Block Profile: Captures blocking events caused by synchronization primitives.
-
Thread Creation Profile: Identifies issues related to excessive thread creation.
go tool pprof interactive commands such as top, list, peek, and web.
Best Practices for Capturing and Sharing Performance Profiles
By attaching these profiles, you provide invaluable information for diagnosing performance bottlenecks and crashes efficiently!Considerations Before Exporting:
- Run with a real workload: Capture profiles during actual usage scenarios to ensure meaningful data.
- Use an adequate duration: For CPU profiles, longer capture times (10–30 seconds) yield more useful information.
- Avoid noise: Terminate other background processes to minimize interference in the profiles.
Automation Script for Linux and macOS
Use the following script to automate the steps described above, and then attach the archive to an issue or send it to us via Slack.Continuous Profiling with Grafana Pyroscope
Thepprof endpoints above are ideal for ad-hoc, point-in-time investigations. For long-running deployments it is often more useful to profile the router continuously, so you can correlate a spike in CPU or memory with a deploy, a traffic pattern, or a specific time window — without having to reproduce the issue while attached to pprof.
The router integrates with Grafana Pyroscope for exactly this. When enabled, the router periodically collects profiles and pushes them to a Pyroscope server, where you can explore them in Grafana using Explore Profiles (formerly Profiles Drilldown). This works against a self-hosted Pyroscope instance or Grafana Cloud Profiles.
Unlike the
pprof endpoints, continuous profiling pushes profiles to a server you control, so there is no publicly exposed debug endpoint. The overhead is low, making it suitable for production.Enable Continuous Profiling
Add apyroscope block to your router configuration (or set the equivalent PYROSCOPE_* environment variables):
config.yaml
server_address at your Profiles endpoint and authenticate with basic auth:
config.yaml
View Profiles in Grafana
- Open Grafana and navigate to Explore → Profiles (or the Explore Profiles app).
- Select the router service by its
application_name(defaultwundergraph.cosmo.router). - Pick a profile type (e.g. CPU or memory) and a time range to drill into flame graphs for that window.
tags option to filter across instances, environments, or deployments.