1 Star 0 Fork 0

sun/common_pkg

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
http.go 1.51 KB
一键复制 编辑 原始数据 按行查看 历史
yzsunjianguo 提交于 2023-09-19 21:35 +08:00 . init
package prof
import (
"net/http"
"net/http/pprof"
"github.com/felixge/fgprof"
)
var defaultPrefix = "/debug/pprof"
// Option set defaultPrefix func
type Option func(o *options)
type options struct {
prefix string
enableIOWaitTime bool
}
func (o *options) apply(opts ...Option) {
for _, opt := range opts {
opt(o)
}
}
// WithPrefix set route defaultPrefix
func WithPrefix(prefix string) Option {
return func(o *options) {
if prefix == "" {
return
}
o.prefix = prefix
}
}
// WithIOWaitTime enable IO wait time
func WithIOWaitTime() Option {
return func(o *options) {
o.enableIOWaitTime = true
}
}
// Register pprof server mux
func Register(mux *http.ServeMux, opts ...Option) {
o := &options{prefix: defaultPrefix}
o.apply(opts...)
mux.Handle(o.prefix+"/", http.HandlerFunc(pprof.Index))
mux.Handle(o.prefix+"/profile", http.HandlerFunc(pprof.Profile))
mux.Handle(o.prefix+"/symbol", http.HandlerFunc(pprof.Symbol))
mux.Handle(o.prefix+"/cmdline", http.HandlerFunc(pprof.Cmdline))
mux.Handle(o.prefix+"/trace", http.HandlerFunc(pprof.Trace))
mux.Handle(o.prefix+"/heap", pprof.Handler("heap"))
mux.Handle(o.prefix+"/goroutine", pprof.Handler("goroutine"))
mux.Handle(o.prefix+"/threadcreate", pprof.Handler("threadcreate"))
mux.Handle(o.prefix+"/block", pprof.Handler("block"))
mux.Handle(o.prefix+"/mutex", pprof.Handler("mutex"))
if o.enableIOWaitTime {
// Similar to /profile, add IO wait time, https://github.com/felixge/fgprof
mux.Handle(o.prefix+"/profile-io", fgprof.Handler())
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/jianguosun_admin/common_pkg.git
git@gitee.com:jianguosun_admin/common_pkg.git
jianguosun_admin
common_pkg
common_pkg
v1.0.4

搜索帮助