From 4b420cb6f7818a2b9b187b00441181fe41e46e91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20R=C3=BCger?= Date: Mon, 23 Sep 2024 22:12:23 +0200 Subject: [PATCH] chore: Migrate to slog --- go.mod | 6 ++---- go.sum | 8 ++------ pkg/app/server.go | 29 ++++++++++++----------------- 3 files changed, 16 insertions(+), 27 deletions(-) diff --git a/go.mod b/go.mod index 84e9d04e62..3e97a79212 100644 --- a/go.mod +++ b/go.mod @@ -6,13 +6,14 @@ require ( github.com/KimMachineGun/automemlimit v0.6.1 github.com/dgryski/go-jump v0.0.0-20211018200510-ba001c3ffce0 github.com/fsnotify/fsnotify v1.7.0 + github.com/go-logr/logr v1.4.1 github.com/gobuffalo/flect v1.0.3 github.com/google/go-cmp v0.6.0 github.com/oklog/run v1.1.0 github.com/prometheus/client_golang v1.20.4 github.com/prometheus/client_model v0.6.1 github.com/prometheus/common v0.59.1 - github.com/prometheus/exporter-toolkit v0.12.0 + github.com/prometheus/exporter-toolkit v0.13.0 github.com/robfig/cron/v3 v3.0.1 github.com/spf13/cobra v1.8.1 github.com/spf13/viper v1.19.0 @@ -38,9 +39,6 @@ require ( github.com/docker/go-units v0.4.0 // indirect github.com/emicklei/go-restful/v3 v3.11.0 // indirect github.com/evanphx/json-patch v5.6.0+incompatible // indirect - github.com/go-kit/log v0.2.1 // indirect - github.com/go-logfmt/logfmt v0.5.1 // indirect - github.com/go-logr/logr v1.4.1 // indirect github.com/go-openapi/jsonpointer v0.19.6 // indirect github.com/go-openapi/jsonreference v0.20.2 // indirect github.com/go-openapi/swag v0.22.3 // indirect diff --git a/go.sum b/go.sum index 945b5d8c8a..ef4cc1664d 100644 --- a/go.sum +++ b/go.sum @@ -30,10 +30,6 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= -github.com/go-kit/log v0.2.1 h1:MRVx0/zhvdseW+Gza6N9rVzU/IVzaeE1SFI4raAhmBU= -github.com/go-kit/log v0.2.1/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0= -github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA= -github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= github.com/go-openapi/jsonpointer v0.19.6 h1:eCs3fxoIi3Wh6vtgmLTOjdhSpiqphQ+DaPn38N2ZdrE= @@ -131,8 +127,8 @@ github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY= github.com/prometheus/common v0.59.1 h1:LXb1quJHWm1P6wq/U824uxYi4Sg0oGvNeUm1z5dJoX0= github.com/prometheus/common v0.59.1/go.mod h1:GpWM7dewqmVYcd7SmRaiWVe9SSqjf0UrwnYnpEZNuT0= -github.com/prometheus/exporter-toolkit v0.12.0 h1:DkE5RcEZR3lQA2QD5JLVQIf41dFKNsVMXFhgqcif7fo= -github.com/prometheus/exporter-toolkit v0.12.0/go.mod h1:fQH0KtTn0yrrS0S82kqppRjDDiwMfIQUwT+RBRRhwUc= +github.com/prometheus/exporter-toolkit v0.13.0 h1:lmA0Q+8IaXgmFRKw09RldZmZdnvu9wwcDLIXGmTPw1c= +github.com/prometheus/exporter-toolkit v0.13.0/go.mod h1:2uop99EZl80KdXhv/MxVI2181fMcwlsumFOqBecGkG0= github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc= github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk= github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= diff --git a/pkg/app/server.go b/pkg/app/server.go index 98bb11df01..56afc6090a 100644 --- a/pkg/app/server.go +++ b/pkg/app/server.go @@ -21,6 +21,7 @@ import ( "crypto/md5" //nolint:gosec "encoding/binary" "fmt" + "log/slog" "net" "net/http" "net/http/pprof" @@ -30,6 +31,8 @@ import ( "strings" "time" + "github.com/go-logr/logr" + "gopkg.in/yaml.v3" "k8s.io/client-go/kubernetes" _ "k8s.io/client-go/plugin/pkg/client/auth" // Initialize common client auth plugins. @@ -66,19 +69,6 @@ const ( readyzPath = "/readyz" ) -// promLogger implements promhttp.Logger -type promLogger struct{} - -func (pl promLogger) Println(v ...interface{}) { - klog.Error(v...) -} - -// promLogger implements the Logger interface -func (pl promLogger) Log(v ...interface{}) error { - klog.Info(v...) - return nil -} - // RunKubeStateMetricsWrapper runs KSM with context cancellation. func RunKubeStateMetricsWrapper(ctx context.Context, opts *options.Options) error { err := RunKubeStateMetrics(ctx, opts) @@ -93,7 +83,6 @@ func RunKubeStateMetricsWrapper(ctx context.Context, opts *options.Options) erro // Any out-of-tree custom resource metrics could be registered by newing a registry factory // which implements customresource.RegistryFactory and pass all factories into this function. func RunKubeStateMetrics(ctx context.Context, opts *options.Options) error { - promLogger := promLogger{} ksmMetricsRegistry := prometheus.NewRegistry() ksmMetricsRegistry.MustRegister(versionCollector.NewCollector("kube_state_metrics")) durationVec := promauto.With(ksmMetricsRegistry).NewHistogramVec( @@ -361,11 +350,14 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options) error { WebConfigFile: &tlsConfig, } + handler := logr.ToSlogHandler(klog.Background()) + sLogger := slog.New(handler) + // Run Telemetry server { g.Add(func() error { klog.InfoS("Started kube-state-metrics self metrics server", "telemetryAddress", telemetryListenAddress) - return web.ListenAndServe(&telemetryServer, &telemetryFlags, promLogger) + return web.ListenAndServe(&telemetryServer, &telemetryFlags, sLogger) }, func(error) { ctxShutDown, cancel := context.WithTimeout(ctx, 3*time.Second) defer cancel() @@ -376,7 +368,7 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options) error { { g.Add(func() error { klog.InfoS("Started metrics server", "metricsServerAddress", metricsServerListenAddress) - return web.ListenAndServe(&metricsServer, &metricsFlags, promLogger) + return web.ListenAndServe(&metricsServer, &metricsFlags, sLogger) }, func(error) { ctxShutDown, cancel := context.WithTimeout(ctx, 3*time.Second) defer cancel() @@ -395,8 +387,11 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options) error { func buildTelemetryServer(registry prometheus.Gatherer) *http.ServeMux { mux := http.NewServeMux() + handler := logr.ToSlogHandler(klog.Background()) + sLogger := slog.NewLogLogger(handler, slog.LevelError) + // Add metricsPath - mux.Handle(metricsPath, promhttp.HandlerFor(registry, promhttp.HandlerOpts{ErrorLog: promLogger{}})) + mux.Handle(metricsPath, promhttp.HandlerFor(registry, promhttp.HandlerOpts{ErrorLog: sLogger})) // Add readyzPath mux.Handle(readyzPath, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {