Skip to content

Commit

Permalink
Migrate from klog to log/slog
Browse files Browse the repository at this point in the history
  • Loading branch information
gesellix committed Sep 10, 2024
1 parent 5eeac61 commit cff9a1a
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 151 deletions.
64 changes: 32 additions & 32 deletions couchdb-exporter.go
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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"
Expand Down Expand Up @@ -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 != "" {
Expand All @@ -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) {
Expand All @@ -256,7 +256,7 @@ func main() {
</body>
</html>`))
if err != nil {
klog.Error(err)
slog.Error(fmt.Sprintf("%v", err))
}
}
landingPageHandler, err := web.NewLandingPage(web.LandingConfig{
Expand Down Expand Up @@ -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
Expand All @@ -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))
}
}

Expand All @@ -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
//}
4 changes: 0 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ 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
github.com/prometheus/exporter-toolkit v0.13.0
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 (
Expand All @@ -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
Expand Down
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
22 changes: 0 additions & 22 deletions kitlog/klog_logger.go

This file was deleted.

60 changes: 0 additions & 60 deletions kitlog/klog_logger_test.go

This file was deleted.

7 changes: 3 additions & 4 deletions lib/collector-v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ package lib
import (
"encoding/json"
"fmt"
"log/slog"
"strconv"

"k8s.io/klog/v2"
)

func (e *Exporter) collectV1(stats Stats, exposedHttpStatusCodes []string, collectorConfig CollectorConfig) error {
e.databasesTotal.Set(float64(stats.DatabasesTotal))

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)

Expand Down Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions lib/collector-v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ package lib
import (
"encoding/json"
"fmt"
"log/slog"
"strconv"

"k8s.io/klog/v2"
)

func (e *Exporter) collectV2(stats Stats, exposedHttpStatusCodes []string, collectorConfig CollectorConfig) error {
e.databasesTotal.Set(float64(stats.DatabasesTotal))

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)

Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions lib/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Loading

0 comments on commit cff9a1a

Please sign in to comment.