Skip to content

Commit

Permalink
disable default profiler, start pprof on port 6060
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Bertschy <[email protected]>
  • Loading branch information
matthyx committed Jan 16, 2024
1 parent 1893c8b commit c1fa35a
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions pkg/cmd/server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ package server

import (
"fmt"
"github.com/kubescape/go-logger/helpers"
"io"
"net"
"net/http"
"net/http/pprof"

"github.com/kubescape/go-logger"
"github.com/kubescape/storage/pkg/admission/wardleinitializer"
Expand Down Expand Up @@ -98,6 +101,13 @@ func NewCommandStartWardleServer(defaults *WardleServerOptions, stopCh <-chan st
o.RecommendedOptions.AddFlags(flags)
utilfeature.DefaultMutableFeatureGate.AddFlag(flags)

// replace built-in profiling with pprof on port 6060
err := flags.Set("profiling", "false")
if err != nil {
logger.L().Warning("failed to set profiling flag to false", helpers.Error(err))
}
ServePprof()

// mute klog
// https://github.com/kubernetes/klog/issues/87
// klog.SetLogger(logr.Discard())
Expand Down Expand Up @@ -178,3 +188,25 @@ func (o WardleServerOptions) RunWardleServer(stopCh <-chan struct{}) error {

return server.GenericAPIServer.PrepareRun().Run(stopCh)
}

func ServePprof() {
if logger.L().GetLevel() == helpers.DebugLevel.String() {
logger.L().Info("starting pprof server", helpers.String("port", "6060"))
pprofMux := http.NewServeMux()
pprofMux.HandleFunc("/debug/pprof/", pprof.Index)
pprofMux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
pprofMux.HandleFunc("/debug/pprof/profile", pprof.Profile)
pprofMux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
pprofMux.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine"))
pprofMux.Handle("/debug/pprof/heap", pprof.Handler("heap"))
pprofMux.Handle("/debug/pprof/allocs", pprof.Handler("allocs"))
pprofMux.Handle("/debug/pprof/threadcreate", pprof.Handler("threadcreate"))
pprofMux.Handle("/debug/pprof/block", pprof.Handler("block"))
pprofMux.Handle("/debug/pprof/mutex", pprof.Handler("mutex"))
go func() {
if err := http.ListenAndServe(":6060", pprofMux); err != nil {
logger.L().Error("failed to start pprof server", helpers.Error(err))
}
}()
}
}

0 comments on commit c1fa35a

Please sign in to comment.