This topic compares the continuous profiling feature of Application Monitoring for Go with Go pprof.
Background information
Go pprof is a powerful profiling tool library built into the Go language. During software development, developers often encounter difficult problems:
The program runs slowly with high CPU usage.
The service's memory usage grows over time, leading to an out-of-memory (OOM) error.
The program's response slows down, as if it is stuck.
Many goroutines start but do not appear to function correctly.
Guessing the root cause of these problems based on intuition or logs is often time-consuming and inaccurate. pprof provides a data-driven, visual way to answer these key questions:
Where is CPU time spent? Find the functions that consume the most computing resources.
What is using the memory? Find the objects and code paths that allocate the most memory.
Why is the program blocked? Analyze the synchronization causes, such as locks and channels, that make goroutines wait.
What are the goroutines doing? View the call stacks of all coroutines to locate goroutine leaks.
pprof works by sampling data with low overhead. It can generate several types of analysis reports, called profiles:
CPU Profile (profile): This is the most common profile. It captures snapshots of the call stack for the currently executing function at a fixed frequency. The default frequency is 100 Hz, or 100 times per second. Analyzing these samples helps identify which functions use the most CPU time.
Heap Profile (heap): Analyzes the program's memory usage. It records allocation information for all live objects on the heap. This helps find the code that allocates large amounts of memory without releasing it.
Goroutine Profile (goroutine): Provides a snapshot of the current state of all goroutines. It shows the number of goroutines and their respective call stacks. This is helpful for debugging goroutine leaks, where goroutines start but do not exit correctly.
Block Profile (block): Records the time spent on synchronization operations that cause goroutines to block or wait. Examples include waiting to read from or write to an unbuffered channel, or waiting for a mutex lock held by another goroutine. This helps find bottlenecks in concurrent programs.
Mutex Profile (mutex): Reports on mutex lock contention. It shows which locks have the longest wait times and the highest contention.
Trace (trace): A more powerful tool that records nearly all program execution events over a period, such as 1 second. These events include goroutine creation, blocking, and waking, system calls, and garbage collection (GC). It generates a detailed event stream to analyze latency issues and complex concurrent interactions.
Comparison
The Go runtime provides pprof features. You can integrate pprof into an application as shown in the following example. Then, you can access port 6060 of the application to obtain and analyze pprof data, such as flame graphs.
package main
import (
"log"
"net/http"
_ "net/http/pprof" // Important: Import the pprof package anonymously.
)
func main() {
// ... Your other business handlers...
http.HandleFunc("/work", func(w http.ResponseWriter, r *http.Request) {
// Simulate some work
// ...
w.Write([]byte("Work done!"))
})
log.Println("Server started on :6060")
// Start the HTTP server
// The pprof handler is automatically registered to the default http.ServeMux.
log.Fatal(http.ListenAndServe(":6060", nil))
}Compared to integrating pprof through code, continuous profiling for Go applications provides more capabilities ():
You do not need to modify code or occupy a port. For more information, see Monitor a Go application.
You can enable or disable the feature on demand through configuration.
No other components are required to pull and analyze data. You can view data directly in Application Monitoring to eliminate extra steps.
It provides code hot spots that associate profiling with traces. For more information, see Use code hot spots to diagnose slow calls in Go applications.
In addition to continuous profiling, Application Monitoring for Go also provides non-intrusive metric statistics and tracing analysis. For more information, see Monitor a Go application in a Container Service for Kubernetes (ACK) cluster.