Skip to content

Commit

Permalink
metrics: provide an empty server when enablemetrics is false
Browse files Browse the repository at this point in the history
Signed-off-by: zhaocongqi <[email protected]>
  • Loading branch information
zhaocongqi committed Dec 30, 2024
1 parent 56b29be commit 1c2877e
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 17 deletions.
29 changes: 25 additions & 4 deletions cmd/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,33 @@ func CmdMain() {
if config.EnableMetrics {
metrics.InitKlogMetrics()
metrics.InitClientGoMetrics()
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")
}
} else {
klog.Info("metrics server is disabled")
listerner, err := net.ListenTCP("tcp", &net.TCPAddr{IP: net.ParseIP(metricsAddr), Port: int(config.PprofPort)})
if err != nil {
util.LogFatalAndExit(err, "failed to listen on %s", util.JoinHostPort(metricsAddr, config.PprofPort))
}
svr := manager.Server{
Name: "metrics",
Server: &http.Server{
Handler: http.NewServeMux(),
MaxHeaderBytes: 1 << 20,
IdleTimeout: 90 * time.Second,
ReadHeaderTimeout: 32 * time.Second,
},
Listener: listerner,
}
go func() {
if err = svr.Start(ctx); err != nil {
util.LogFatalAndExit(err, "failed to run metrics server")
}
}()
}

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
29 changes: 25 additions & 4 deletions cmd/daemon/cniserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,33 @@ func main() {
if config.EnableMetrics {
daemon.InitMetrics()
metrics.InitKlogMetrics()
listenAddr := util.JoinHostPort(addr, config.PprofPort)
if err = metrics.Run(ctx, nil, listenAddr, config.SecureServing, servePprofInMetricsServer); err != nil {
util.LogFatalAndExit(err, "failed to run metrics server")
}
} else {
klog.Info("metrics server is disabled")
listerner, err := net.ListenTCP("tcp", &net.TCPAddr{IP: net.ParseIP(addr), Port: int(config.PprofPort)})
if err != nil {
util.LogFatalAndExit(err, "failed to listen on %s", util.JoinHostPort(addr, config.PprofPort))
}
svr := manager.Server{
Name: "metrics",
Server: &http.Server{
Handler: http.NewServeMux(),
MaxHeaderBytes: 1 << 20,
IdleTimeout: 90 * time.Second,
ReadHeaderTimeout: 32 * time.Second,
},
Listener: listerner,
}
go func() {
if err = svr.Start(ctx); err != nil {
util.LogFatalAndExit(err, "failed to run metrics server")
}
}()
}

listenAddr := util.JoinHostPort(addr, config.PprofPort)
if err = metrics.Run(ctx, nil, listenAddr, config.SecureServing, servePprofInMetricsServer); err != nil {
util.LogFatalAndExit(err, "failed to run metrics server")
}
<-stopCh
}

Expand Down
32 changes: 29 additions & 3 deletions cmd/ovn_monitor/ovn_monitor.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package ovn_monitor

import (
"net"
"net/http"
"os"
"strings"
"time"

"k8s.io/klog/v2"
"kernel.org/pub/linux/libs/security/libcap/cap"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"

"github.com/kubeovn/kube-ovn/pkg/metrics"
Expand Down Expand Up @@ -47,9 +51,31 @@ func CmdMain() {
go exporter.TryClientConnection()
}
exporter.StartOvnMetrics()
if err = metrics.Run(ctx, nil, addr, config.SecureServing, false); err != nil {
util.LogFatalAndExit(err, "failed to run metrics server")
}
} else {
klog.Info("metrics server is disabled")
listerner, err := net.ListenTCP("tcp", &net.TCPAddr{IP: net.ParseIP(addr), Port: int(port)})
if err != nil {
util.LogFatalAndExit(err, "failed to listen on %s", util.JoinHostPort(addr, port))
}
svr := manager.Server{
Name: "metrics",
Server: &http.Server{
Handler: http.NewServeMux(),
MaxHeaderBytes: 1 << 20,
IdleTimeout: 90 * time.Second,
ReadHeaderTimeout: 32 * time.Second,
},
Listener: listerner,
}
go func() {
if err = svr.Start(ctx); err != nil {
util.LogFatalAndExit(err, "failed to run metrics server")
}
}()
}
if err = metrics.Run(ctx, nil, addr, config.SecureServing, false); err != nil {
util.LogFatalAndExit(err, "failed to run metrics server")
}

<-ctx.Done()
}
6 changes: 3 additions & 3 deletions cmd/pinger/pinger.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ func CmdMain() {
if config.EnableMetrics {
pinger.InitPingerMetrics()
metrics.InitKlogMetrics()
}
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")
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
6 changes: 3 additions & 3 deletions cmd/speaker/speaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ func CmdMain() {
go func() {
if config.EnableMetrics {
metrics.InitKlogMetrics()
}
if err = metrics.Run(ctx, nil, util.JoinHostPort("0.0.0.0", config.PprofPort), false, false); err != nil {
util.LogFatalAndExit(err, "failed to run metrics server")
if err = metrics.Run(ctx, nil, util.JoinHostPort("0.0.0.0", config.PprofPort), false, false); err != nil {
util.LogFatalAndExit(err, "failed to run metrics server")
}
}
<-ctx.Done()
}()
Expand Down

0 comments on commit 1c2877e

Please sign in to comment.