Skip to main content

Go Profiling 数据采集

概述

Go profiling 是基于 dd-trace-go 开源库采集 Golang 的性能分析数据。它将采集到的数据转换为 .pprof 文件,并将其上传到 Lighthouse 服务中,进行数据解析分析。

可采集分析的指标数据

  • goroutine: 运行的 Goroutine 的调用栈分析
  • heap: 活跃对象的内存分配情况
  • cpu: CPU性能分析
  • block: 阻塞分析
  • mutex: 互斥锁分析

Go 应用配置

基于 dd-trace-go ,采集应用性能数据发送到 Lighthouse 。 代码如下:

注意

url 通常是 Lighthouse 安装所在的机器 ip ,端口号默认是 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("<Lighthouse-ip>:8085"), // Lighthouse 所在的机器 ip,端口号默认是 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)
}
}