diff --git a/README.md b/README.md index 69bdf4a6..bd6ac824 100644 --- a/README.md +++ b/README.md @@ -82,9 +82,7 @@ dns names or ip addresses when configuring the CouchDB URI. ## Logging -The couchdb-exporter uses the [glog](https://godoc.org/github.com/golang/glog) library for logging. -With the default parameters everything will be logged to `/tmp/`. -Use `--logtostderr` to enable logging to stderr and `--help` to see all options. +The couchdb-exporter uses the [slog](https://pkg.go.dev/golang.org/x/exp/slog) module for logging. ## CouchDB 2+ clusters diff --git a/couchdb-exporter.go b/couchdb-exporter.go index 61fcafea..e0c07dc1 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" @@ -45,23 +42,12 @@ type exporterConfigType struct { schedulerJobs bool } -type loggingConfigType struct { - toStderr bool // The -logtostderr flag. - alsoToStderr bool // The -alsologtostderr flag. - verbosity int // V logging level, the value of the -v flag/ - stderrThreshold int // The -stderrthreshold flag. - logDir string // The -log_dir flag. -} - var exporterConfig exporterConfigType var webConfig webConfigType var configFileFlagname = "config" var webConfigFile = "" -// custom exposed (but hidden) logging config flags -var loggingConfig loggingConfigType - var appFlags []cli.Flag // TODO graceful migration to new parameter names @@ -171,41 +157,6 @@ func init() { Hidden: false, Destination: &exporterConfig.schedulerJobs, }), - - altsrc.NewBoolFlag(&cli.BoolFlag{ - Name: "logtostderr", - Usage: "log to standard error instead of files", - Hidden: true, - Value: true, - Destination: &loggingConfig.toStderr, - }), - altsrc.NewBoolFlag(&cli.BoolFlag{ - Name: "alsologtostderr", - Usage: "log to standard error as well as files", - Hidden: true, - Destination: &loggingConfig.alsoToStderr, - }), - // TODO `v` clashed with urfave/cli's "version" shortcut `-v`. - altsrc.NewIntFlag(&cli.IntFlag{ - Name: "verbosity", - Usage: "log level for V logs", - Value: 0, - Hidden: true, - Destination: &loggingConfig.verbosity, - }), - altsrc.NewIntFlag(&cli.IntFlag{ - Name: "stderrthreshold", - Usage: "logs at or above this threshold go to stderr", - Value: 2, - Hidden: true, - Destination: &loggingConfig.stderrThreshold, - }), - altsrc.NewStringFlag(&cli.StringFlag{ - Name: "log_dir", - Usage: "If non-empty, write log files in this directory", - Hidden: true, - Destination: &loggingConfig.logDir, - }), } } @@ -217,6 +168,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 +196,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 +210,7 @@ func main() { `)) if err != nil { - klog.Error(err) + slog.Error(fmt.Sprintf("%v", err)) } } landingPageHandler, err := web.NewLandingPage(web.LandingConfig{ @@ -293,15 +247,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 +270,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 +287,6 @@ func beforeApp(appFlags []cli.Flag) cli.BeforeFunc { if err := altsrc.InitInputSourceWithContext(appFlags, inputSource)(context); err != nil { return err } - return initKlogFlags(context, loggingConfig) - } -} - -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 - } + return nil } - - klog.Infof("adopted logging config: %+v\n", loggingConfig) - return nil } diff --git a/go.mod b/go.mod index a4ed4e58..44969979 100644 --- a/go.mod +++ b/go.mod @@ -6,15 +6,13 @@ toolchain go1.22.3 require ( github.com/gesellix/couchdb-cluster-config/v17 v17.5.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.5 github.com/prometheus/client_model v0.6.1 - github.com/prometheus/exporter-toolkit v0.11.0 + github.com/prometheus/exporter-toolkit v0.13.1 github.com/stretchr/testify v1.9.0 github.com/urfave/cli/v2 v2.27.5 google.golang.org/protobuf v1.35.1 - k8s.io/klog/v2 v2.130.1 ) require ( @@ -24,23 +22,23 @@ require ( github.com/coreos/go-systemd/v22 v22.5.0 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.5 // 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 + github.com/mdlayher/vsock v1.2.1 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/prometheus/common v0.55.0 // indirect + github.com/prometheus/common v0.60.1 // indirect github.com/prometheus/procfs v0.15.1 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect - golang.org/x/crypto v0.24.0 // indirect - golang.org/x/net v0.26.0 // indirect - golang.org/x/oauth2 v0.21.0 // indirect - golang.org/x/sync v0.7.0 // indirect - golang.org/x/sys v0.22.0 // indirect - golang.org/x/text v0.16.0 // indirect + golang.org/x/crypto v0.28.0 // indirect + golang.org/x/net v0.29.0 // indirect + golang.org/x/oauth2 v0.23.0 // indirect + golang.org/x/sync v0.8.0 // indirect + golang.org/x/sys v0.26.0 // indirect + golang.org/x/text v0.19.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 9da128ae..9b284eab 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.5.0 h1:ukaf1x2dnOb9Io04y9m/jcZdRSMAUujzylHi9IhNWVs= github.com/gesellix/couchdb-cluster-config/v17 v17.5.0/go.mod h1:d7cV9fN1NZkZxLOdNA1dyrDviHJ4oWVQ/W8O8nA/EAQ= -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= @@ -31,6 +25,10 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= +github.com/mdlayher/socket v0.4.1 h1:eM9y2/jlbs1M615oshPQOHZzj6R6wMT7bX5NPiQvn2U= +github.com/mdlayher/socket v0.4.1/go.mod h1:cAqeGjoufqdxWkD7DkpyS+wcefOtmu5OQ8KuoJGIReA= +github.com/mdlayher/vsock v1.2.1 h1:pC1mTJTvjo1r9n9fbm7S1j04rCgCzhCOS5DY0zqHlnQ= +github.com/mdlayher/vsock v1.2.1/go.mod h1:NRfCibel++DgeMD8z/hP+PPTjlNJsdPOmxcnENvE+SE= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f h1:KUppIJq7/+SVif2QVs3tOP0zanoHgBEVAwHxUSIzRqU= @@ -43,10 +41,10 @@ github.com/prometheus/client_golang v1.20.5 h1:cxppBPuYhUnsO6yo/aoRol4L7q7UFfdm+ github.com/prometheus/client_golang v1.20.5/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE= github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E= github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY= -github.com/prometheus/common v0.55.0 h1:KEi6DK7lXW/m7Ig5i47x0vRzuBsHuvJdi5ee6Y3G1dc= -github.com/prometheus/common v0.55.0/go.mod h1:2SECS4xJG1kd8XF9IcM1gMX6510RAEL65zxzNImwdc8= -github.com/prometheus/exporter-toolkit v0.11.0 h1:yNTsuZ0aNCNFQ3aFTD2uhPOvr4iD7fdBvKPAEGkNf+g= -github.com/prometheus/exporter-toolkit v0.11.0/go.mod h1:BVnENhnNecpwoTLiABx7mrPB/OLRIgN74qlQbV+FK1Q= +github.com/prometheus/common v0.60.1 h1:FUas6GcOw66yB/73KC+BOZoFJmbo/1pojoILArPAaSc= +github.com/prometheus/common v0.60.1/go.mod h1:h0LYf1R1deLSKtD4Vdg8gy4RuOvENW2J/h19V5NADQw= +github.com/prometheus/exporter-toolkit v0.13.1 h1:Evsh0gWQo2bdOHlnz9+0Nm7/OFfIwhE2Ws4A2jIlR04= +github.com/prometheus/exporter-toolkit v0.13.1/go.mod h1:ujdv2YIOxtdFxxqtloLpbqmxd5J0Le6IITUvIRSWjj0= 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/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= @@ -59,18 +57,18 @@ github.com/urfave/cli/v2 v2.27.5 h1:WoHEJLdsXr6dDWoJgMq/CboDmyY/8HMMH1fTECbih+w= github.com/urfave/cli/v2 v2.27.5/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ= github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4= github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM= -golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI= -golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM= -golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ= -golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE= -golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs= -golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= -golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= -golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= -golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= -golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= +golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw= +golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U= +golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo= +golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0= +golang.org/x/oauth2 v0.23.0 h1:PbgcYx2W7i4LvjJWEbf0ngHV6qJYr86PkAV3bXdLEbs= +golang.org/x/oauth2 v0.23.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= +golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= +golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= +golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM= +golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA= google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= @@ -80,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()