Go Profiling
Overview
Go profiling is based on the dd-trace-go open-source library to collect performance analysis data for Golang. It converts the collected data into .pprof
files and uploads them to the Lighthouse service for data parsing and analysis.
Collectable Metrics
- goroutine: Call stack analysis of running Goroutines
- heap: Memory allocation of active objects
- cpu: CPU performance analysis
- block: Blocking analysis
- mutex: Mutex lock analysis
Go Application Configuration
Based on dd-trace-go, collect application performance data and send it to Lighthouse. Code example:
Note
The URL is typically the IP address of the machine where Lighthouse is installed, with a default port of 8085.
package main
import (
"log"
"time"
"gopkg.in/DataDog/dd-trace-go.v1/profiler"
)
func main() {
err := profiler.Start(
profiler.WithService("service"), //service name
profiler.WithEnv("env"),
profiler.WithVersion("1.2.0"),
profiler.WithTags("key:1", "key:2"),
profiler.WithAgentAddr("localhost:8085"), // IP address of the machine where Lighthouse is installed, default port is 8085
profiler.WithHostname("light-profiling"), //hostname
profiler.WithProfileTypes(
profiler.CPUProfile,
profiler.HeapProfile,
// The profiles below are disabled by default to keep overhead
// low, but can be enabled as needed.
//profiler.BlockProfile,
//profiler.MutexProfile,
//profiler.GoroutineProfile,
),
)
if err != nil {
log.Fatal(err)
}
defer profiler.Stop()
// your code here
demo()
}
func demo() {
index := 0
for {
fmt.Printf("hello world: %v", index)
index++
time.Sleep(20 * time.Second)
}
}