Skip to content

Commit

Permalink
serve pprof in metrics server if metrics server listens on 0.0.0.0 (#…
Browse files Browse the repository at this point in the history
…4384)

Signed-off-by: zhangzujian <[email protected]>
  • Loading branch information
zhangzujian authored Aug 12, 2024
1 parent f17033e commit 33d7a28
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
8 changes: 5 additions & 3 deletions cmd/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ func CmdMain() {
ctrl.SetLogger(klog.NewKlogr())
ctx := signals.SetupSignalHandler()
go func() {
if config.EnablePprof {
metricsAddr := util.GetDefaultListenAddr()
servePprofInMetricsServer := config.EnableMetrics && metricsAddr == "0.0.0.0"
if config.EnablePprof && !servePprofInMetricsServer {
mux := http.NewServeMux()
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
Expand Down Expand Up @@ -87,8 +89,8 @@ func CmdMain() {
}
metrics.InitKlogMetrics()
metrics.InitClientGoMetrics()
addr := util.JoinHostPort(util.GetDefaultListenAddr(), config.PprofPort)
if err := metrics.Run(ctx, config.KubeRestConfig, addr, config.SecureServing); err != nil {
addr := util.JoinHostPort(metricsAddr, config.PprofPort)
if err := metrics.Run(ctx, config.KubeRestConfig, addr, config.SecureServing, servePprofInMetricsServer); err != nil {
util.LogFatalAndExit(err, "failed to run metrics server")
}
<-ctx.Done()
Expand Down
5 changes: 3 additions & 2 deletions cmd/daemon/cniserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ func main() {
}()
}

if config.EnablePprof {
servePprofInMetricsServer := config.EnableMetrics && addr == "0.0.0.0"
if config.EnablePprof && !servePprofInMetricsServer {
mux := http.NewServeMux()
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
Expand Down Expand Up @@ -143,7 +144,7 @@ func main() {
}

listenAddr := util.JoinHostPort(addr, config.PprofPort)
if err = metrics.Run(ctx, nil, listenAddr, config.SecureServing); err != nil {
if err = metrics.Run(ctx, nil, listenAddr, config.SecureServing, servePprofInMetricsServer); err != nil {
util.LogFatalAndExit(err, "failed to run metrics server")
}
<-stopCh
Expand Down
2 changes: 1 addition & 1 deletion cmd/ovn_monitor/ovn_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func CmdMain() {

ctrl.SetLogger(klog.NewKlogr())
ctx := signals.SetupSignalHandler()
if err = metrics.Run(ctx, nil, addr, config.SecureServing); err != nil {
if err = metrics.Run(ctx, nil, addr, config.SecureServing, false); err != nil {
util.LogFatalAndExit(err, "failed to run metrics server")
}
<-ctx.Done()
Expand Down
2 changes: 1 addition & 1 deletion cmd/pinger/pinger.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func main() {
go func() {
pinger.InitPingerMetrics()
metrics.InitKlogMetrics()
if err := metrics.Run(ctx, nil, util.JoinHostPort("0.0.0.0", config.Port), false); err != nil {
if err := metrics.Run(ctx, nil, util.JoinHostPort("0.0.0.0", config.Port), false, false); err != nil {
util.LogFatalAndExit(err, "failed to run metrics server")
}
<-ctx.Done()
Expand Down
13 changes: 12 additions & 1 deletion pkg/metrics/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package metrics
import (
"context"
"fmt"
"net/http"
"net/http/pprof"

"k8s.io/client-go/rest"
"k8s.io/klog/v2"
Expand All @@ -11,7 +13,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
)

func Run(ctx context.Context, config *rest.Config, addr string, secureServing bool) error {
func Run(ctx context.Context, config *rest.Config, addr string, secureServing, withPprof bool) error {
if config == nil {
config = ctrl.GetConfigOrDie()
}
Expand All @@ -28,6 +30,15 @@ func Run(ctx context.Context, config *rest.Config, addr string, secureServing bo
if secureServing {
options.FilterProvider = filters.WithAuthenticationAndAuthorization
}
if withPprof {
options.ExtraHandlers = map[string]http.Handler{
"/debug/pprof/": http.HandlerFunc(pprof.Index),
"/debug/pprof/cmdline": http.HandlerFunc(pprof.Cmdline),
"/debug/pprof/profile": http.HandlerFunc(pprof.Profile),
"/debug/pprof/symbol": http.HandlerFunc(pprof.Symbol),
"/debug/pprof/trace": http.HandlerFunc(pprof.Trace),
}
}
svr, err := server.NewServer(options, config, client)
if err != nil {
klog.Error(err)
Expand Down

0 comments on commit 33d7a28

Please sign in to comment.