Introduction
This article provides a comprehensive guide on collecting performance profiles from a running Consul agent using Go's pprof profiling tools. These profiles are invaluable for diagnosing and resolving common performance bottlenecks such as high CPU utilization, goroutine leaks, and excessive heap memory consumption. We will cover the preferred method using the consul debug command, and also demonstrate manual collection via curl commands or the consul-pprof.sh helper script for situations where consul debug might not be immediately feasible.
Expected Outcome
By following this guide, you will be able to:
- Understand the different
pprofendpoints exposed by Consul and their purpose. -
Manually collect various
pprofprofiles (Heap, CPU, Trace, Goroutine) from a Consul agent usingcurl. -
Automate
pprofcollection using theconsul-pprof.shscript for efficient data gathering. -
Utilize the
consul debugcommand to capture a comprehensive support bundle, includingpprofdata and debug logs, which is the preferred method for complete diagnostics. - Prepare collected
pproffiles for analysis withgo tool pprofor for submission to HashiCorp Support.
Prerequisites
To successfully collect pprof profiles, ensure you have the following:
- Access to a running Consul agent: The agent should be accessible via its HTTP API.
-
consulCLI tool: For using theconsul debugcommand. -
curlutility: For manual collection. -
goruntime andgo tool pprof: While not strictly required for collection, these are essential for analyzing the profiles. -
Consul ACL Token (if ACLs are enabled): A management or operator token with sufficient permissions to access the
/debug/pprofendpoints and executeconsul debug. -
Network connectivity: Ensure no firewalls or proxies are blocking access to the Consul agent's
debugendpoints. -
Bash environment: If using the
consul-pprof.shscript.
Use Case
Collecting pprof profiles is essential for several scenarios, including:
- Troubleshooting high CPU usage: Identify hot spots in the Consul codebase consuming excessive CPU cycles.
- Diagnosing memory leaks: Pinpoint memory accumulation issues by analyzing heap profiles.
- Detecting goroutine leaks: Identify goroutines that are unexpectedly blocked or not terminating, leading to resource exhaustion.
- Performance optimization: Gain insights into the runtime behavior of your Consul agents to proactively optimize configurations and workload patterns.
-
Providing data for support cases: When experiencing performance issues, HashiCorp Support often requests these profiles for in-depth analysis. Using
consul debugis preferred as it provides a more complete picture, including relevant logs.
Procedure
Consul exposes several pprof endpoints, each providing a distinct type of performance data. The table below provides an overview:
| Profile Endpoint | Description |
|---|---|
/debug/pprof/heap |
Memory usage snapshot, showing heap allocations. |
/debug/pprof/profile?seconds=N |
CPU usage profile over N seconds. |
/debug/pprof/trace?seconds=N |
Execution trace showing goroutine activity and blocking events. |
/debug/pprof/goroutine |
Summary of current goroutine stack traces. |
/debug/pprof/goroutine?debug=2 |
Full goroutine dump with detailed stack traces and their states. |
- Note: These endpoints are internally registered within Consul's codebase, typically in files like
api/debug.go.
Preferred Method: Using consul debug
The preferred method to capture pprof data and other valuable troubleshooting information is by using the consul debug command. This command collects a comprehensive support bundle, which includes pprof profiles, agent configuration, relevant logs, and more, providing a holistic view for diagnosing issues.
Important Prerequisite for consul debug pprof collection:
For consul debug to successfully collect pprof data, the enable_debug configuration parameter in your Consul agent's configuration file must be set to true.
-
Configuration Example (within your Consul agent config file):
enable_debug = true
-
Restart Required: If
enable_debugis not currently set totrue, you will need to restart your Consul agent for this change to take effect.
-
Restart Required: If
Reason for Preference:
The consul debug command is preferred because it gathers a complete set of diagnostic information that is invaluable for troubleshooting, including:
- Debug Logs: Crucial for understanding the agent's behavior leading up to an issue.
- Configuration: Provides insight into the agent's current settings.
- Internal State: Offers a deeper look into Consul's operational state.
Executing consul debug:
Once enable_debug is set to true and the agent is running, execute the consul debug command. You may need to specify the Consul HTTP address and token.
This command will typically create a gzipped tarball (e.g., consul-debug-2025-06-17T15-30-00Z.tar.gz) containing all the collected data, including the pprof profiles within a dedicated directory inside the bundle.
Alternative Methods: Manual Collection via curl or consul-pprof.sh
There might be situations where setting enable_debug to true and restarting the Consul agent is not feasible. For instance, if an issue is actively occurring and restarting the agent would temporarily mitigate the problem, making it unpredictable when the issue would resurface. In such cases, manually collecting pprof data without a restart can be extremely helpful.
Manual Collection via curl
For direct control over the profiling process, you can manually collect profiles using curl. This method requires direct network reachability to the Consul agent and proper authentication if ACLs are enabled.
-
Set Environment Variables: It's recommended to set environment variables for the Consul HTTP address and ACL token for easier execution. Replace
<your-management-token>with your actual token.export CONSUL_HTTP_ADDR=http://localhost:8500 export CONSUL_HTTP_TOKEN=<your-management-token>
-
Execute
curlCommands: Run the followingcurlcommands to collect the different profile types. The-sflag silences progress output,-Hadds the ACL token header, and-ospecifies the output filename.-
Heap Profile (Memory Usage Snapshot):
curl -s "${CONSUL_HTTP_ADDR}/debug/pprof/heap" \ -H "X-Consul-Token:${CONSUL_HTTP_TOKEN}" \ -o heap.prof -
CPU Profile (e.g., 30-second sample):
curl -s "${CONSUL_HTTP_ADDR}/debug/pprof/profile?seconds=30" \ -H "X-Consul-Token:${CONSUL_HTTP_TOKEN}" \ -o cpu.prof -
Trace Profile (Execution Trace with Goroutine Activity):
curl -s "${CONSUL_HTTP_ADDR}/debug/pprof/trace?seconds=30" \ -H "X-Consul-Token:${CONSUL_HTTP_TOKEN}" \ -o trace.out -
Goroutine Profile (Current Stack Trace Summary):
curl -s "${CONSUL_HTTP_ADDR}/debug/pprof/goroutine" \ -H "X-Consul-Token:${CONSUL_HTTP_TOKEN}" \ -o goroutine.prof -
Goroutine (Debug=2) Profile (Detailed Stack Traces and States):
curl -s "${CONSUL_HTTP_ADDR}/debug/pprof/goroutine?debug=2" \ -H "X-Consul-Token:${CONSUL_HTTP_TOKEN}" \ -o goroutine-raw.prof
-
Heap Profile (Memory Usage Snapshot):
Once collected, these .prof and .out files can be analyzed using the go tool pprof command or attached to support tickets.
Automated Collection Using consul-pprof.sh
For a more streamlined and automated approach, especially when collecting multiple profiles or on remote systems, the consul-pprof.sh helper script is highly recommended when consul debug is not viable. This script automates several steps:
- Validation: Checks agent availability and ACL token access.
-
Comprehensive Collection: Gathers all five
pprofprofile types. - Output Validation: Verifies the integrity of each collected profile.
-
Organized Storage: Saves all profiles in a timestamped directory under
/tmp(or a specified output directory).
Usage:
Download the consul-pprof.sh script (often available as a Gist from HashiCorp support or community resources) and make it executable:
chmod +x consul-pprof.sh ./consul-pprof.sh [OPTIONS]
Common Options:
| Flag | Description |
|---|---|
--addr <URL> |
Target Consul HTTP address (default: localhost:8500). |
--token <TOKEN> |
ACL token (can also be set via CONSUL_HTTP_TOKEN env var). |
--duration <seconds> |
Duration for CPU/Trace profiles (default: 30 seconds). |
--output-dir <DIR> |
Directory to save profiles (default: /tmp/consul-pprof-...). |
-h or --help
|
Displays the help message with all available options. |
Example:
export CONSUL_HTTP_ADDR="http://10.0.0.12:8500"
export CONSUL_HTTP_TOKEN="root"
./consul-pprof.sh \
--duration 60 \
--output-dir /tmp/my-consul-pprof
The profiles will be saved in the specified directory (e.g., /tmp/my-consul-pprof/consul-pprof-2025-06-17_15-00-00/) and automatically validated.
ACL and Debug Mode Notes for consul-pprof.sh:
- The
consul-pprof.shscript is designed to work regardless of theenable_debugsetting on the Consul agent, as it directly queries thepprofHTTP endpoints. - It primarily uses the
X-Consul-Tokenheader for authentication, allowing it to bypass some ACL restrictions if the provided token has sufficient privileges. - For the script to capture full stack traces and verbose output, ensure that the
debugendpoints are not restricted by network firewalls or Layer 7 proxies.
Additional Information
-
go tool pprof: This is the primary command-line tool for visualizing and analyzing the generatedpproffiles. It can render flame graphs, call graphs, and more.- Official
go tool pprofdocumentation - go tool pprof usage
- Official
-
Consul
debugCLI command: This is the recommended command for comprehensive data collection. It gatherspprofdata, logs, configuration, and internal state.- Official Consul
debugCLI documentation
- Official Consul
-
Consul
enable_debugConfiguration: This agent configuration parameter controls whether additional debug endpoints, includingpprofendpoints accessible byconsul debug, are enabled.- Official
enable_debugdocumentation
- Official
-
Consul HTTP API:
debug.go: The source code where Consul registers itspprofdebug endpoints. -
Go
net/http/pprofpackage: The standard Go library package that provides thepprofHTTP handlers that Consul integrates. -
consul-pprof.shscript (Gist): Ensure you have the latest version of theconsul-pprof.shscript. (It would be beneficial to link directly to a stable Gist or repository if available).
Need Help?
When opening a support ticket with HashiCorp for performance troubleshooting, please attach the generated consul debug bundle (e.g., consul-debug-*.tar.gz). If a consul debug bundle was not feasible to collect, please provide the manually generated .prof and .out files. These profiles are crucial for our engineers to effectively diagnose and assist with your Consul performance issues. Providing a detailed description of the problem and the steps leading to it will also greatly aid in the resolution process.