Documentation
¶
Overview ¶
Package prof 封装官方 net/http/pprof 路由和运行时 profiling 采样能力,提供大厂标准化的性能分析工具集。
功能特性 ¶
HTTP 实时分析:将 pprof 路由注册到 http.ServeMux 或 Gin 引擎, 通过浏览器或 go tool pprof 实时查看 CPU、内存、协程等 profile 数据。 支持可选鉴权中间件保护生产环境安全。
信号触发采样:通过 NewProfile() 创建采样器,结合系统信号(SIGTRAP)开关 采样。适合生产环境按需采集,不影响正常服务性能。
自适应采集:结合资源监控告警,在 CPU/内存超阈值时自动触发 profile 采样, 便于问题事后追溯。
采集类型 ¶
- CPU:CPU 使用率 profiling,分析热点函数
- Memory:堆内存分配,定位内存泄漏
- Goroutine:所有 goroutine 堆栈,排查协程泄漏
- Block:同步原语阻塞,分析锁竞争
- Mutex:互斥锁持有者,排查死锁
- ThreadCreate:线程创建,分析线程爆炸
- Trace:运行时 trace(可选),分析调度和 GC
使用方式 ¶
HTTP 方式:
mux := http.NewServeMux() prof.Register(mux, prof.WithIOWaitTime())
信号触发方式:
p := prof.NewProfile(prof.WithProfileDuration(30)) // 收到 SIGTRAP 时调用 p.StartOrStop()
安全提示 ¶
pprof 可能暴露敏感信息(源码路径、goroutine 堆栈等), 生产环境建议:
- 通过 WithAuth() 添加鉴权中间件
- 仅在内部网络暴露 pprof 端口
- 结合 K8S 网络安全策略限制访问
Index ¶
Constants ¶
const ( // DefaultDuration 默认采样时长(秒) DefaultDuration = 60 // DefaultOutputDirSuffix 默认输出目录名格式(追加到系统临时目录后) DefaultOutputDirSuffix = "_profile" // TimeFormat 采样文件时间戳格式 TimeFormat = "20060102T150405" )
const (
// DefaultPrefix 默认 pprof 路由前缀
DefaultPrefix = "/debug/pprof"
)
Variables ¶
This section is empty.
Functions ¶
func EnableTrace ¶
func EnableTrace()
EnableTrace 启用包级默认 trace 采样。 此函数影响全局默认值,仅对后续 NewProfile() 调用有效。 推荐使用 WithProfileTrace(true) 替代。
func Register ¶
func Register(mux *http.ServeMux, opts ...HTTPOption)
Register 将 pprof 路由注册到标准 http.ServeMux 中。
注册的路由(以默认前缀 /debug/pprof 为例):
- /debug/pprof/ - pprof 首页
- /debug/pprof/cmdline - 命令行参数
- /debug/pprof/profile - CPU profile(30 秒采样)
- /debug/pprof/symbol - 符号查询
- /debug/pprof/trace - 执行轨迹
- /debug/pprof/allocs - 内存分配
- /debug/pprof/block - 阻塞分析
- /debug/pprof/goroutine - 协程堆栈
- /debug/pprof/heap - 堆内存
- /debug/pprof/mutex - 互斥锁
- /debug/pprof/threadcreate - 线程创建
- /debug/pprof/profile-io - IO 等待时间(需 WithIOWaitTime)
如果设置了 WithAuth,所有 pprof 路由都将经过鉴权中间件检查。
func SetDurationSecond ¶
func SetDurationSecond(d uint32)
SetDurationSecond 设置包级默认采样时长(秒)。 此函数影响全局默认值,仅对后续 NewProfile() 调用有效。 推荐使用 WithProfileDuration() 替代。
Types ¶
type HTTPOption ¶ added in v1.4.42
type HTTPOption func(o *httpOptions)
HTTPOption 定义 HTTP pprof 注册的配置选项函数
func WithAuth ¶ added in v1.4.42
func WithAuth(authFn func(http.Handler) http.Handler) HTTPOption
WithAuth 设置 pprof 路由的鉴权中间件。 在生成环境中,pprof 可能暴露敏感信息(如源码路径、goroutine 堆栈等), 建议通过此选项添加鉴权保护。
示例:
// 简单 Token 鉴权
auth := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("X-Auth-Token") != "my-secret-token" {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
next.ServeHTTP(w, r)
})
}
prof.Register(mux, prof.WithAuth(auth))
func WithIOWaitTime ¶
func WithIOWaitTime() HTTPOption
WithIOWaitTime 启用 IO 等待时间 profile 分析。 开启后在 {prefix}/profile-io 路由提供 fgprof 分析, 它比标准 pprof 多包含 IO 等待时间,更适合分析 IO 密集型服务。
func WithPrefix ¶
func WithPrefix(prefix string) HTTPOption
WithPrefix 设置 pprof 路由前缀。 如果 prefix 为空字符串,则使用默认前缀 /debug/pprof。
type Profile ¶ added in v1.4.27
type Profile struct {
// contains filtered or unexported fields
}
Profile 表示一次 profiling 采样会话。 每次 StartOrStop() 调用会开启或停止采样,采样文件保存至输出目录。
func NewProfile ¶
func NewProfile(opts ...ProfileOption) *Profile
NewProfile 创建一个新的 Profile 采样器。 支持通过 ProfileOption 配置采样时长、trace 开关、输出目录等。
示例:
p := NewProfile(
WithProfileDuration(30),
WithProfileTrace(true),
WithProfileOutputDir("/tmp/myapp_profile"),
WithProfileErrorHandler(func(err error) {
log.Printf("profile error: %v", err)
}),
)
func (*Profile) Cleanup ¶ added in v1.4.42
func (p *Profile) Cleanup()
Cleanup 删除本次采样产生的所有 profile 文件。 通常在分析完成文件后调用。
func (*Profile) StartOrStop ¶ added in v1.4.27
func (p *Profile) StartOrStop()
StartOrStop 开关式启动/停止采样。
- 第一次调用:启动采样(如果当前状态为停止)
- 第二次调用:停止采样(如果当前状态为启动)
启动后,若在 durationSec 内未收到停止信号,自动停止采样。
type ProfileOption ¶ added in v1.4.42
type ProfileOption func(p *profileOptions)
ProfileOption 定义 Profile 的配置选项函数
func WithProfileDuration ¶ added in v1.4.42
func WithProfileDuration(sec uint32) ProfileOption
WithProfileDuration 设置采样持续时间(秒),默认 60 秒。 如果设置为 0,使用默认值。
func WithProfileErrorHandler ¶ added in v1.4.42
func WithProfileErrorHandler(fn func(error)) ProfileOption
WithProfileErrorHandler 设置采样过程中错误处理回调函数。 默认为 fmt.Println 输出到标准输出。
func WithProfileOutputDir ¶ added in v1.4.42
func WithProfileOutputDir(dir string) ProfileOption
WithProfileOutputDir 设置采样文件输出目录,默认在系统临时目录下。
func WithProfileTrace ¶ added in v1.4.42
func WithProfileTrace(enabled bool) ProfileOption
WithProfileTrace 启用或禁用 trace 采样,默认禁用。