From cff9a1abc22d8b6d11b3a873468aa131685b917e Mon Sep 17 00:00:00 2001 From: Tobias Gesellchen Date: Tue, 10 Sep 2024 22:41:34 +0200 Subject: [PATCH] Migrate from klog to log/slog --- couchdb-exporter.go | 64 +++++++++++++++++++------------------- go.mod | 4 --- go.sum | 8 ----- kitlog/klog_logger.go | 22 ------------- kitlog/klog_logger_test.go | 60 ----------------------------------- lib/collector-v1.go | 7 ++--- lib/collector-v2.go | 9 +++--- lib/collector.go | 4 +-- lib/couchdb-client.go | 21 ++++++------- lib/exporter.go | 7 +++-- 10 files changed, 55 insertions(+), 151 deletions(-) delete mode 100644 kitlog/klog_logger.go delete mode 100644 kitlog/klog_logger_test.go diff --git a/couchdb-exporter.go b/couchdb-exporter.go index 61fcafea..d0e315e3 100644 --- a/couchdb-exporter.go +++ b/couchdb-exporter.go @@ -1,13 +1,11 @@ package main import ( - "flag" "fmt" - "github.com/gesellix/couchdb-prometheus-exporter/v30/kitlog" "log" + "log/slog" "net/http" "os" - "strconv" "strings" "time" @@ -16,7 +14,6 @@ import ( "github.com/prometheus/exporter-toolkit/web" "github.com/urfave/cli/v2" "github.com/urfave/cli/v2/altsrc" - "k8s.io/klog/v2" "github.com/gesellix/couchdb-prometheus-exporter/v30/fileutil" "github.com/gesellix/couchdb-prometheus-exporter/v30/lib" @@ -217,6 +214,9 @@ func ofString(i string) *string { } func main() { + logger := slog.New(slog.NewJSONHandler(os.Stdout, nil)) + slog.SetDefault(logger) + var appAction = func(c *cli.Context) error { var databases []string if exporterConfig.databases != "" { @@ -242,7 +242,7 @@ func main() { http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) { _, err := fmt.Fprint(w, "OK") if err != nil { - klog.Error(err) + slog.Error(fmt.Sprintf("%v", err)) } }) redirectToMetricsHandler := func(w http.ResponseWriter, r *http.Request) { @@ -256,7 +256,7 @@ func main() { `)) if err != nil { - klog.Error(err) + slog.Error(fmt.Sprintf("%v", err)) } } landingPageHandler, err := web.NewLandingPage(web.LandingConfig{ @@ -293,15 +293,15 @@ func main() { http.HandleFunc("/", landingPageHandler.ServeHTTP) } - klog.Infof("Starting exporter version %s at '%s' to read from CouchDB at '%s'", version, webConfig.listenAddress, exporterConfig.couchdbURI) + slog.Info(fmt.Sprintf("Starting exporter version %s at '%s' to read from CouchDB at '%s'", version, webConfig.listenAddress, exporterConfig.couchdbURI)) server := &http.Server{Addr: webConfig.listenAddress} flags := web.FlagConfig{ WebListenAddresses: &([]string{webConfig.listenAddress}), WebSystemdSocket: ofBool(false), WebConfigFile: ofString(webConfigFile), } - if err := web.ListenAndServe(server, &flags, kitlog.NewKlogLogger()); err != nil { - klog.Error("msg", "Failed to start the server", "err", err) + if err := web.ListenAndServe(server, &flags, logger); err != nil { + slog.Error("Failed to start the server", "err", err) os.Exit(1) } return nil @@ -316,11 +316,11 @@ func main() { app.Before = beforeApp(appFlags) app.Action = appAction - defer klog.Flush() + //defer klog.Flush() err := app.Run(os.Args) if err != nil { - klog.Fatal(err) + slog.Error(fmt.Sprintf("%v", err)) } } @@ -333,27 +333,27 @@ func beforeApp(appFlags []cli.Flag) cli.BeforeFunc { if err := altsrc.InitInputSourceWithContext(appFlags, inputSource)(context); err != nil { return err } - return initKlogFlags(context, loggingConfig) + return nil } } -func initKlogFlags(_ *cli.Context, loggingConfig loggingConfigType) error { - klogFlags := flag.NewFlagSet("klog", flag.ContinueOnError) - klog.InitFlags(klogFlags) - - flags := map[string]string{ - "logtostderr": strconv.FormatBool(loggingConfig.toStderr), - "alsologtostderr": strconv.FormatBool(loggingConfig.alsoToStderr), - "stderrthreshold": strconv.Itoa(loggingConfig.stderrThreshold), - "v": strconv.Itoa(loggingConfig.verbosity), - "log_dir": loggingConfig.logDir, - } - for k, v := range flags { - if err := klogFlags.Set(k, v); err != nil { - return err - } - } - - klog.Infof("adopted logging config: %+v\n", loggingConfig) - return nil -} +//func initKlogFlags(_ *cli.Context, loggingConfig loggingConfigType) error { +// klogFlags := flag.NewFlagSet("klog", flag.ContinueOnError) +// klog.InitFlags(klogFlags) +// +// flags := map[string]string{ +// "logtostderr": strconv.FormatBool(loggingConfig.toStderr), +// "alsologtostderr": strconv.FormatBool(loggingConfig.alsoToStderr), +// "stderrthreshold": strconv.Itoa(loggingConfig.stderrThreshold), +// "v": strconv.Itoa(loggingConfig.verbosity), +// "log_dir": loggingConfig.logDir, +// } +// for k, v := range flags { +// if err := klogFlags.Set(k, v); err != nil { +// return err +// } +// } +// +// klog.Infof("adopted logging config: %+v\n", loggingConfig) +// return nil +//} diff --git a/go.mod b/go.mod index 5b00b519..474c82d4 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,6 @@ toolchain go1.22.3 require ( github.com/gesellix/couchdb-cluster-config/v17 v17.4.0 - github.com/go-kit/log v0.2.1 github.com/okeuday/erlang_go/v2 v2.0.7 github.com/prometheus/client_golang v1.20.3 github.com/prometheus/client_model v0.6.1 @@ -14,7 +13,6 @@ require ( github.com/stretchr/testify v1.9.0 github.com/urfave/cli/v2 v2.27.4 google.golang.org/protobuf v1.34.2 - k8s.io/klog/v2 v2.130.1 ) require ( @@ -24,8 +22,6 @@ require ( github.com/coreos/go-systemd/v22 v22.5.0 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect github.com/davecgh/go-spew v1.1.1 // indirect - github.com/go-logfmt/logfmt v0.6.0 // indirect - github.com/go-logr/logr v1.4.1 // indirect github.com/jpillora/backoff v1.0.0 // indirect github.com/klauspost/compress v1.17.9 // indirect github.com/mdlayher/socket v0.4.1 // indirect diff --git a/go.sum b/go.sum index 56761cd0..49a0805c 100644 --- a/go.sum +++ b/go.sum @@ -12,12 +12,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gesellix/couchdb-cluster-config/v17 v17.4.0 h1:RM5NqfIOjlUBy6nc4YrSv3fU3EbKfN4/eGJSzbjwFlo= github.com/gesellix/couchdb-cluster-config/v17 v17.4.0/go.mod h1:7bmn+B5+ZVVVD8pjD38mJO8BBzUIxlCkREbxq7Hy0EM= -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.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4= -github.com/go-logfmt/logfmt v0.6.0/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/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= @@ -84,5 +78,3 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk= -k8s.io/klog/v2 v2.130.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE= diff --git a/kitlog/klog_logger.go b/kitlog/klog_logger.go deleted file mode 100644 index 5a67a102..00000000 --- a/kitlog/klog_logger.go +++ /dev/null @@ -1,22 +0,0 @@ -// Package kitlog provides a klog adapter to the -// go-kit log.Logger interface. -package kitlog - -import ( - kitlog "github.com/go-kit/log" - "k8s.io/klog/v2" -) - -type klogLogger struct { -} - -// NewKlogLogger returns a go-kit log.Logger that sends log events to a k8s.io/klog logger. -func NewKlogLogger() kitlog.Logger { - return &klogLogger{} -} - -// Log the keyvals to the INFO log. -func (l klogLogger) Log(keyvals ...interface{}) error { - klog.InfoS("-", keyvals...) - return nil -} diff --git a/kitlog/klog_logger_test.go b/kitlog/klog_logger_test.go deleted file mode 100644 index bf31e535..00000000 --- a/kitlog/klog_logger_test.go +++ /dev/null @@ -1,60 +0,0 @@ -package kitlog_test - -import ( - "bytes" - "errors" - "flag" - "strconv" - "strings" - "testing" - - "github.com/gesellix/couchdb-prometheus-exporter/v30/kitlog" - "k8s.io/klog/v2" -) - -func TestKlogLogger(t *testing.T) { - t.Parallel() - - klogFlags := flag.NewFlagSet("klog", flag.ContinueOnError) - klog.InitFlags(klogFlags) - klogFlags.Set("skip_headers", strconv.FormatBool(true)) - klog.LogToStderr(false) - buf := new(bytes.Buffer) - klog.SetOutput(buf) - logger := kitlog.NewKlogLogger() - - if err := logger.Log("hello", "world"); err != nil { - t.Fatal(err) - } - if want, have := "hello=\"world\"\n", strings.Split(buf.String(), " ")[1]; want != have { - t.Errorf("want %#v, have %#v", want, have) - } - - buf.Reset() - if err := logger.Log("a", 1, "err", errors.New("error")); err != nil { - t.Fatal(err) - } - if want, have := "a=1 err=\"error\"", strings.TrimSpace(strings.SplitAfterN(buf.String(), " ", 2)[1]); want != have { - t.Errorf("want %#v, have %#v", want, have) - } - - buf.Reset() - if err := logger.Log("a", 1, "b"); err != nil { - t.Fatal(err) - } - if want, have := "a=1 b=\"(MISSING)\"", strings.TrimSpace(strings.SplitAfterN(buf.String(), " ", 2)[1]); want != have { - t.Errorf("want %#v, have %#v", want, have) - } - - buf.Reset() - if err := logger.Log("my_map", mymap{0: 0}); err != nil { - t.Fatal(err) - } - if want, have := "my_map=\"special_behavior\"", strings.TrimSpace(strings.Split(buf.String(), " ")[1]); want != have { - t.Errorf("want %#v, have %#v", want, have) - } -} - -type mymap map[int]int - -func (m mymap) String() string { return "special_behavior" } diff --git a/lib/collector-v1.go b/lib/collector-v1.go index 21001816..c6685979 100644 --- a/lib/collector-v1.go +++ b/lib/collector-v1.go @@ -3,9 +3,8 @@ package lib import ( "encoding/json" "fmt" + "log/slog" "strconv" - - "k8s.io/klog/v2" ) func (e *Exporter) collectV1(stats Stats, exposedHttpStatusCodes []string, collectorConfig CollectorConfig) error { @@ -13,7 +12,7 @@ func (e *Exporter) collectV1(stats Stats, exposedHttpStatusCodes []string, colle for name, nodeStats := range stats.StatsByNodeName { //fmt.Printf("%s -> %v\n", name, stats) - //klog.Info(fmt.Sprintf("name: %s -> stats: %v\n", name, stats)) + //slog.Info(fmt.Sprintf("name: %s -> stats: %v\n", name, stats)) e.nodeUp.WithLabelValues(name).Set(nodeStats.Up) e.nodeInfo.WithLabelValues(name, nodeStats.NodeInfo.Version, nodeStats.NodeInfo.Vendor.Name).Set(1) @@ -63,7 +62,7 @@ func (e *Exporter) collectV1(stats Stats, exposedHttpStatusCodes []string, colle var intSeq int64 err := json.Unmarshal(stats.DatabaseStatsByDbName[dbName].UpdateSeq, &intSeq) if err != nil { - klog.Warningf("%v", err) + slog.Warn(fmt.Sprintf("%v", err)) continue } dbUpdateSeq := intSeq diff --git a/lib/collector-v2.go b/lib/collector-v2.go index 45dc1635..ed0cc16f 100644 --- a/lib/collector-v2.go +++ b/lib/collector-v2.go @@ -3,9 +3,8 @@ package lib import ( "encoding/json" "fmt" + "log/slog" "strconv" - - "k8s.io/klog/v2" ) func (e *Exporter) collectV2(stats Stats, exposedHttpStatusCodes []string, collectorConfig CollectorConfig) error { @@ -13,7 +12,7 @@ func (e *Exporter) collectV2(stats Stats, exposedHttpStatusCodes []string, colle for name, nodeStats := range stats.StatsByNodeName { // fmt.Printf("%s -> %v\n", name, stats) - // klog.Info(fmt.Sprintf("name: %s -> stats: %v\n", name, stats)) + // slog.Info(fmt.Sprintf("name: %s -> stats: %v\n", name, stats)) e.nodeUp.WithLabelValues(name).Set(nodeStats.Up) e.nodeInfo.WithLabelValues(name, nodeStats.NodeInfo.Version, nodeStats.NodeInfo.Vendor.Name).Set(1) @@ -159,7 +158,7 @@ func (e *Exporter) collectV2(stats Stats, exposedHttpStatusCodes []string, colle var stringSeq string err := json.Unmarshal(stats.DatabaseStatsByDbName[dbName].UpdateSeq, &stringSeq) if err != nil { - klog.Warningf("%v", err) + slog.Warn(fmt.Sprintf("%v", err)) continue } dbUpdateSeq, err := DecodeUpdateSeq(stringSeq) @@ -176,7 +175,7 @@ func (e *Exporter) collectV2(stats Stats, exposedHttpStatusCodes []string, colle for _, dbRangeSeq := range dbUpdateSeq { if viewRangeSeq.Range[0].Cmp(dbRangeSeq.Range[0]) == 0 { age := dbRangeSeq.Seq - viewRangeSeq.Seq - // klog.Infof("dbRangeSeq.Seq %d, viewRangeSeq.Seq %d, age %d", dbRangeSeq.Seq, viewRangeSeq.Seq, age) + // slog.Infof("dbRangeSeq.Seq %d, viewRangeSeq.Seq %d, age %d", dbRangeSeq.Seq, viewRangeSeq.Seq, age) e.viewStaleness.WithLabelValues( dbName, designDoc, diff --git a/lib/collector.go b/lib/collector.go index 30be20f8..37c009a7 100644 --- a/lib/collector.go +++ b/lib/collector.go @@ -2,10 +2,10 @@ package lib import ( "fmt" + "log/slog" "time" "github.com/prometheus/client_golang/prometheus" - "k8s.io/klog/v2" ) const AllDbs = "_all_dbs" @@ -443,7 +443,7 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) { e.mutex.RLock() // To protect metrics from concurrent collects. defer e.mutex.RUnlock() if err := e.collect(ch); err != nil { - klog.Error(fmt.Sprintf("Error collecting stats: %s", err)) + slog.Error(fmt.Sprintf("Error collecting stats: %s", err)) } return } diff --git a/lib/couchdb-client.go b/lib/couchdb-client.go index 8bf316c0..d8d824a5 100644 --- a/lib/couchdb-client.go +++ b/lib/couchdb-client.go @@ -6,14 +6,13 @@ import ( "errors" "fmt" "io" + "log/slog" "net/http" "net/url" "strconv" "strings" "sync" "sync/atomic" - - "k8s.io/klog/v2" ) type BasicAuth struct { @@ -91,7 +90,7 @@ func (c *CouchdbClient) GetNodeNames() ([]string, error) { return nil, err } // for i, name := range membership.ClusterNodes { - // klog.Infof("node[%d]: %s\n", i, name) + // slog.Infof("node[%d]: %s\n", i, name) // } return membership.ClusterNodes, nil } @@ -120,7 +119,7 @@ func (c *CouchdbClient) getStatsByNodeName(urisByNodeName map[string]string) (ma } stats.Up = 0 - klog.Error(fmt.Errorf("continuing despite error: %v", err)) + slog.Error(fmt.Sprintf("continuing despite error: %v", err)) continue } @@ -158,7 +157,7 @@ func (c *CouchdbClient) getSystemByNodeName(urisByNodeName map[string]string) (m if !strings.Contains(err.Error(), "\"error\":\"nodedown\"") { return nil, err } - klog.Error(fmt.Errorf("continuing despite error: %v", err)) + slog.Error(fmt.Sprintf("continuing despite error: %v", err)) continue } @@ -373,7 +372,7 @@ func (c *CouchdbClient) updateSeqFromInt(message json.RawMessage) string { var updateSeq int64 err := json.Unmarshal(message, &updateSeq) if err != nil { - klog.Warningf("%v", err) + slog.Warn(fmt.Sprintf("%v", err)) } return strconv.FormatInt(updateSeq, 10) } @@ -382,7 +381,7 @@ func (c *CouchdbClient) updateSeqFromString(message json.RawMessage) string { var updateSeq string err := json.Unmarshal(message, &updateSeq) if err != nil { - klog.Warningf("%v", err) + slog.Warn(fmt.Sprintf("%v", err)) } return updateSeq } @@ -439,7 +438,7 @@ func (c *CouchdbClient) enhanceWithViewUpdateSeq(isCouchdbV1 bool, dbStatsByDbNa continue } go func() { - //klog.Infof("/%s/%s/_view/%s\n", dbName, row.Doc.Id, viewName) + //slog.Infof("/%s/%s/_view/%s\n", dbName, row.Doc.Id, viewName) err := semaphore.Acquire() if err != nil { // send something to parent coroutine so it doesn't block forever on receive @@ -454,11 +453,11 @@ func (c *CouchdbClient) enhanceWithViewUpdateSeq(isCouchdbV1 bool, dbStatsByDbNa res := <-v if res.warn != "" { // TODO consider adding a metric to make warnings more visible - //klog.Warning(res.warn) + //slog.Warning(res.warn) } if res.err != nil { // TODO consider adding a metric to make errors more visible - klog.Error(res.err) + slog.Error(fmt.Sprintf("%v", res.err)) continue //r <- dbStatsResult{err: res.err} //return @@ -594,7 +593,7 @@ type requestCountingRoundTripper struct { func (rt *requestCountingRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { atomic.AddInt64(&rt.RequestCount, 1) - //klog.Infof("req[%d] %s", atomic.LoadInt64(&rt.RequestCount), req.URL.String()) + //slog.Infof("req[%d] %s", atomic.LoadInt64(&rt.RequestCount), req.URL.String()) return rt.rt.RoundTrip(req) } diff --git a/lib/exporter.go b/lib/exporter.go index a3eb348b..bd72a1e6 100644 --- a/lib/exporter.go +++ b/lib/exporter.go @@ -1,7 +1,8 @@ package lib import ( - "k8s.io/klog/v2" + "fmt" + "log/slog" "sync" "time" @@ -106,7 +107,7 @@ type Exporter struct { func (e *Exporter) maybeStartScraping() { if e.collectorConfig.ScrapeInterval > 0 { - klog.Infof("Asynchronously scraping the CouchDB stats at an interval of %v", e.collectorConfig.ScrapeInterval) + slog.Info(fmt.Sprintf("Asynchronously scraping the CouchDB stats at an interval of %v", e.collectorConfig.ScrapeInterval)) ticker := time.NewTicker(e.collectorConfig.ScrapeInterval) quit := make(chan struct{}) go func() { @@ -115,7 +116,7 @@ func (e *Exporter) maybeStartScraping() { case <-ticker.C: err := e.scrape() if err != nil { - klog.Error(err) + slog.Error(fmt.Sprintf("%v", err)) } case <-quit: ticker.Stop()