Skip to content

Commit f50205a

Browse files
authored
Merge pull request #2508 from mrueg/switch-to-slog
chore: Migrate to slog for exporter-toolkit
2 parents 826134a + 4b420cb commit f50205a

File tree

3 files changed

+16
-27
lines changed

3 files changed

+16
-27
lines changed

go.mod

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ require (
66
github.com/KimMachineGun/automemlimit v0.6.1
77
github.com/dgryski/go-jump v0.0.0-20211018200510-ba001c3ffce0
88
github.com/fsnotify/fsnotify v1.7.0
9+
github.com/go-logr/logr v1.4.1
910
github.com/gobuffalo/flect v1.0.3
1011
github.com/google/go-cmp v0.6.0
1112
github.com/oklog/run v1.1.0
1213
github.com/prometheus/client_golang v1.20.4
1314
github.com/prometheus/client_model v0.6.1
1415
github.com/prometheus/common v0.59.1
15-
github.com/prometheus/exporter-toolkit v0.12.0
16+
github.com/prometheus/exporter-toolkit v0.13.0
1617
github.com/robfig/cron/v3 v3.0.1
1718
github.com/spf13/cobra v1.8.1
1819
github.com/spf13/viper v1.19.0
@@ -38,9 +39,6 @@ require (
3839
github.com/docker/go-units v0.4.0 // indirect
3940
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
4041
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
41-
github.com/go-kit/log v0.2.1 // indirect
42-
github.com/go-logfmt/logfmt v0.5.1 // indirect
43-
github.com/go-logr/logr v1.4.1 // indirect
4442
github.com/go-openapi/jsonpointer v0.19.6 // indirect
4543
github.com/go-openapi/jsonreference v0.20.2 // indirect
4644
github.com/go-openapi/swag v0.22.3 // indirect

go.sum

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk
3030
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
3131
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
3232
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
33-
github.com/go-kit/log v0.2.1 h1:MRVx0/zhvdseW+Gza6N9rVzU/IVzaeE1SFI4raAhmBU=
34-
github.com/go-kit/log v0.2.1/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0=
35-
github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA=
36-
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
3733
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
3834
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
3935
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
131127
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
132128
github.com/prometheus/common v0.59.1 h1:LXb1quJHWm1P6wq/U824uxYi4Sg0oGvNeUm1z5dJoX0=
133129
github.com/prometheus/common v0.59.1/go.mod h1:GpWM7dewqmVYcd7SmRaiWVe9SSqjf0UrwnYnpEZNuT0=
134-
github.com/prometheus/exporter-toolkit v0.12.0 h1:DkE5RcEZR3lQA2QD5JLVQIf41dFKNsVMXFhgqcif7fo=
135-
github.com/prometheus/exporter-toolkit v0.12.0/go.mod h1:fQH0KtTn0yrrS0S82kqppRjDDiwMfIQUwT+RBRRhwUc=
130+
github.com/prometheus/exporter-toolkit v0.13.0 h1:lmA0Q+8IaXgmFRKw09RldZmZdnvu9wwcDLIXGmTPw1c=
131+
github.com/prometheus/exporter-toolkit v0.13.0/go.mod h1:2uop99EZl80KdXhv/MxVI2181fMcwlsumFOqBecGkG0=
136132
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
137133
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
138134
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=

pkg/app/server.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"crypto/md5" //nolint:gosec
2222
"encoding/binary"
2323
"fmt"
24+
"log/slog"
2425
"net"
2526
"net/http"
2627
"net/http/pprof"
@@ -30,6 +31,8 @@ import (
3031
"strings"
3132
"time"
3233

34+
"github.com/go-logr/logr"
35+
3336
"gopkg.in/yaml.v3"
3437
"k8s.io/client-go/kubernetes"
3538
_ "k8s.io/client-go/plugin/pkg/client/auth" // Initialize common client auth plugins.
@@ -66,19 +69,6 @@ const (
6669
readyzPath = "/readyz"
6770
)
6871

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-
8272
// RunKubeStateMetricsWrapper runs KSM with context cancellation.
8373
func RunKubeStateMetricsWrapper(ctx context.Context, opts *options.Options) error {
8474
err := RunKubeStateMetrics(ctx, opts)
@@ -93,7 +83,6 @@ func RunKubeStateMetricsWrapper(ctx context.Context, opts *options.Options) erro
9383
// Any out-of-tree custom resource metrics could be registered by newing a registry factory
9484
// which implements customresource.RegistryFactory and pass all factories into this function.
9585
func RunKubeStateMetrics(ctx context.Context, opts *options.Options) error {
96-
promLogger := promLogger{}
9786
ksmMetricsRegistry := prometheus.NewRegistry()
9887
ksmMetricsRegistry.MustRegister(versionCollector.NewCollector("kube_state_metrics"))
9988
durationVec := promauto.With(ksmMetricsRegistry).NewHistogramVec(
@@ -361,11 +350,14 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options) error {
361350
WebConfigFile: &tlsConfig,
362351
}
363352

353+
handler := logr.ToSlogHandler(klog.Background())
354+
sLogger := slog.New(handler)
355+
364356
// Run Telemetry server
365357
{
366358
g.Add(func() error {
367359
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)
369361
}, func(error) {
370362
ctxShutDown, cancel := context.WithTimeout(ctx, 3*time.Second)
371363
defer cancel()
@@ -376,7 +368,7 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options) error {
376368
{
377369
g.Add(func() error {
378370
klog.InfoS("Started metrics server", "metricsServerAddress", metricsServerListenAddress)
379-
return web.ListenAndServe(&metricsServer, &metricsFlags, promLogger)
371+
return web.ListenAndServe(&metricsServer, &metricsFlags, sLogger)
380372
}, func(error) {
381373
ctxShutDown, cancel := context.WithTimeout(ctx, 3*time.Second)
382374
defer cancel()
@@ -395,8 +387,11 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options) error {
395387
func buildTelemetryServer(registry prometheus.Gatherer) *http.ServeMux {
396388
mux := http.NewServeMux()
397389

390+
handler := logr.ToSlogHandler(klog.Background())
391+
sLogger := slog.NewLogLogger(handler, slog.LevelError)
392+
398393
// Add metricsPath
399-
mux.Handle(metricsPath, promhttp.HandlerFor(registry, promhttp.HandlerOpts{ErrorLog: promLogger{}}))
394+
mux.Handle(metricsPath, promhttp.HandlerFor(registry, promhttp.HandlerOpts{ErrorLog: sLogger}))
400395

401396
// Add readyzPath
402397
mux.Handle(readyzPath, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {

0 commit comments

Comments
 (0)