@@ -21,6 +21,7 @@ import (
21
21
"crypto/md5" //nolint:gosec
22
22
"encoding/binary"
23
23
"fmt"
24
+ "log/slog"
24
25
"net"
25
26
"net/http"
26
27
"net/http/pprof"
@@ -30,6 +31,8 @@ import (
30
31
"strings"
31
32
"time"
32
33
34
+ "github.com/go-logr/logr"
35
+
33
36
"gopkg.in/yaml.v3"
34
37
"k8s.io/client-go/kubernetes"
35
38
_ "k8s.io/client-go/plugin/pkg/client/auth" // Initialize common client auth plugins.
@@ -66,19 +69,6 @@ const (
66
69
readyzPath = "/readyz"
67
70
)
68
71
69
- // promLogger implements promhttp.Logger
70
- type promLogger struct {}
71
-
72
- func (pl promLogger ) Println (v ... interface {}) {
73
- klog .Error (v ... )
74
- }
75
-
76
- // promLogger implements the Logger interface
77
- func (pl promLogger ) Log (v ... interface {}) error {
78
- klog .Info (v ... )
79
- return nil
80
- }
81
-
82
72
// RunKubeStateMetricsWrapper runs KSM with context cancellation.
83
73
func RunKubeStateMetricsWrapper (ctx context.Context , opts * options.Options ) error {
84
74
err := RunKubeStateMetrics (ctx , opts )
@@ -93,7 +83,6 @@ func RunKubeStateMetricsWrapper(ctx context.Context, opts *options.Options) erro
93
83
// Any out-of-tree custom resource metrics could be registered by newing a registry factory
94
84
// which implements customresource.RegistryFactory and pass all factories into this function.
95
85
func RunKubeStateMetrics (ctx context.Context , opts * options.Options ) error {
96
- promLogger := promLogger {}
97
86
ksmMetricsRegistry := prometheus .NewRegistry ()
98
87
ksmMetricsRegistry .MustRegister (versionCollector .NewCollector ("kube_state_metrics" ))
99
88
durationVec := promauto .With (ksmMetricsRegistry ).NewHistogramVec (
@@ -361,11 +350,14 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options) error {
361
350
WebConfigFile : & tlsConfig ,
362
351
}
363
352
353
+ handler := logr .ToSlogHandler (klog .Background ())
354
+ sLogger := slog .New (handler )
355
+
364
356
// Run Telemetry server
365
357
{
366
358
g .Add (func () error {
367
359
klog .InfoS ("Started kube-state-metrics self metrics server" , "telemetryAddress" , telemetryListenAddress )
368
- return web .ListenAndServe (& telemetryServer , & telemetryFlags , promLogger )
360
+ return web .ListenAndServe (& telemetryServer , & telemetryFlags , sLogger )
369
361
}, func (error ) {
370
362
ctxShutDown , cancel := context .WithTimeout (ctx , 3 * time .Second )
371
363
defer cancel ()
@@ -376,7 +368,7 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options) error {
376
368
{
377
369
g .Add (func () error {
378
370
klog .InfoS ("Started metrics server" , "metricsServerAddress" , metricsServerListenAddress )
379
- return web .ListenAndServe (& metricsServer , & metricsFlags , promLogger )
371
+ return web .ListenAndServe (& metricsServer , & metricsFlags , sLogger )
380
372
}, func (error ) {
381
373
ctxShutDown , cancel := context .WithTimeout (ctx , 3 * time .Second )
382
374
defer cancel ()
@@ -395,8 +387,11 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options) error {
395
387
func buildTelemetryServer (registry prometheus.Gatherer ) * http.ServeMux {
396
388
mux := http .NewServeMux ()
397
389
390
+ handler := logr .ToSlogHandler (klog .Background ())
391
+ sLogger := slog .NewLogLogger (handler , slog .LevelError )
392
+
398
393
// Add metricsPath
399
- mux .Handle (metricsPath , promhttp .HandlerFor (registry , promhttp.HandlerOpts {ErrorLog : promLogger {} }))
394
+ mux .Handle (metricsPath , promhttp .HandlerFor (registry , promhttp.HandlerOpts {ErrorLog : sLogger }))
400
395
401
396
// Add readyzPath
402
397
mux .Handle (readyzPath , http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
0 commit comments