1
0

add pprof support

This commit is contained in:
2023-04-07 16:54:10 +08:00
parent 9ba33d64d3
commit 586abdfd7b
2 changed files with 12 additions and 0 deletions

View File

@@ -27,6 +27,7 @@ func main() {
flag.TextVar(&level, "log-level", zapcore.InfoLevel, "logger level")
flag.StringVar(&conf.MetricsBindAddress, "metrics-address", "127.0.0.1:8080", "The address the metric endpoint binds to.")
flag.BoolVar(&conf.LeaderElection, "enable-leader-election", false, "Enable leader election for controller manager")
flag.BoolVar(&conf.PProf, "enable-pprof", false, "Enable profile debug")
flag.Parse()
// 初始化日志格式
ctrl.SetLogger(zap.New(func(o *zap.Options) {

View File

@@ -2,6 +2,8 @@ package k8s
import (
"fmt"
"net/http"
"net/http/pprof"
v1 "monitor/pkg/apis/v1"
@@ -16,6 +18,7 @@ import (
type Config struct {
ctrl.Options
PProf bool
}
type Manager struct {
@@ -41,6 +44,14 @@ func NewManager(conf Config) (*Manager, error) {
return nil, fmt.Errorf("unable to add scheme %w", err)
}
if conf.PProf {
mgr.AddMetricsExtraHandler("/debug/pprof/", http.HandlerFunc(pprof.Index))
mgr.AddMetricsExtraHandler("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
mgr.AddMetricsExtraHandler("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
mgr.AddMetricsExtraHandler("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
mgr.AddMetricsExtraHandler("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
}
return &Manager{mgr: mgr}, nil
}