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 Nov 11, 2024
1 parent c3998ef commit fd258bf
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 167 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.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.13.0
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 (
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.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
Expand Down
44 changes: 20 additions & 24 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.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=
Expand All @@ -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=
Expand All @@ -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.58.0 h1:N+N8vY4/23r6iYfD3UQZUoJPnUYAo7v6LG5XZxjZTXo=
github.com/prometheus/common v0.58.0/go.mod h1:GpWM7dewqmVYcd7SmRaiWVe9SSqjf0UrwnYnpEZNuT0=
github.com/prometheus/exporter-toolkit v0.13.0 h1:lmA0Q+8IaXgmFRKw09RldZmZdnvu9wwcDLIXGmTPw1c=
github.com/prometheus/exporter-toolkit v0.13.0/go.mod h1:2uop99EZl80KdXhv/MxVI2181fMcwlsumFOqBecGkG0=
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=
Expand All @@ -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.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
golang.org/x/oauth2 v0.22.0 h1:BzDx2FehcG7jJwgWLELCdmLuxk2i+x9UDpSiss2u0ZA=
golang.org/x/oauth2 v0.22.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.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.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=
Expand All @@ -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=
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
Loading

0 comments on commit fd258bf

Please sign in to comment.