@@ -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 (
@@ -359,11 +348,14 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options) error {
359
348
WebConfigFile : & tlsConfig ,
360
349
}
361
350
351
+ handler := logr .ToSlogHandler (klog .Background ())
352
+ sLogger := slog .New (handler )
353
+
362
354
// Run Telemetry server
363
355
{
364
356
g .Add (func () error {
365
357
klog .InfoS ("Started kube-state-metrics self metrics server" , "telemetryAddress" , telemetryListenAddress )
366
- return web .ListenAndServe (& telemetryServer , & telemetryFlags , promLogger )
358
+ return web .ListenAndServe (& telemetryServer , & telemetryFlags , sLogger )
367
359
}, func (error ) {
368
360
ctxShutDown , cancel := context .WithTimeout (ctx , 3 * time .Second )
369
361
defer cancel ()
@@ -374,7 +366,7 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options) error {
374
366
{
375
367
g .Add (func () error {
376
368
klog .InfoS ("Started metrics server" , "metricsServerAddress" , metricsServerListenAddress )
377
- return web .ListenAndServe (& metricsServer , & metricsFlags , promLogger )
369
+ return web .ListenAndServe (& metricsServer , & metricsFlags , sLogger )
378
370
}, func (error ) {
379
371
ctxShutDown , cancel := context .WithTimeout (ctx , 3 * time .Second )
380
372
defer cancel ()
@@ -393,8 +385,11 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options) error {
393
385
func buildTelemetryServer (registry prometheus.Gatherer ) * http.ServeMux {
394
386
mux := http .NewServeMux ()
395
387
388
+ handler := logr .ToSlogHandler (klog .Background ())
389
+ sLogger := slog .NewLogLogger (handler , slog .LevelError )
390
+
396
391
// Add metricsPath
397
- mux .Handle (metricsPath , promhttp .HandlerFor (registry , promhttp.HandlerOpts {ErrorLog : promLogger {} }))
392
+ mux .Handle (metricsPath , promhttp .HandlerFor (registry , promhttp.HandlerOpts {ErrorLog : sLogger }))
398
393
399
394
// Add readyzPath
400
395
mux .Handle (readyzPath , http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
0 commit comments