Skip to content

Commit

Permalink
feat(logging): Switch from go-kit/log to log/slog (#181)
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Henderson <[email protected]>
  • Loading branch information
hairyhenderson authored May 31, 2024
1 parent baef335 commit 319537f
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 93 deletions.
30 changes: 8 additions & 22 deletions collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@ package main

import (
"context"
"fmt"
"log/slog"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
hitron "github.com/hairyhenderson/hitron_coda"
"github.com/prometheus/client_golang/prometheus"
)

type collector struct {
ctx context.Context
logger log.Logger
client *hitron.CableModem
rc routerCollector
cc cmCollector
Expand All @@ -23,22 +20,11 @@ type collector struct {
config config
}

type debugLogAdapter struct {
log.Logger
}

func (l debugLogAdapter) Logf(format string, args ...interface{}) {
l.Log("msg", fmt.Sprintf(format, args...))
}

func newCollector(ctx context.Context, conf config, logger log.Logger) *collector {
debugLogger := debugLogAdapter{level.Debug(logger)}
ctx = hitron.ContextWithDebugLogger(ctx, debugLogger)

c := &collector{ctx: ctx, config: conf, logger: logger}
c.rc = newRouterCollector(ctx, logger, c.getClient)
c.cc = newCMCollector(ctx, logger, c.getClient)
c.wc = newWiFiCollector(ctx, logger, c.getClient)
func newCollector(ctx context.Context, conf config) *collector {
c := &collector{ctx: ctx, config: conf}
c.rc = newRouterCollector(ctx, c.getClient)
c.cc = newCMCollector(ctx, c.getClient)
c.wc = newWiFiCollector(ctx, c.getClient)

c.up = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: metricsNS,
Expand Down Expand Up @@ -72,15 +58,15 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {

c.client, err = hitron.New(c.config.Host, c.config.Username, c.config.Password)
if err != nil {
level.Error(c.logger).Log("msg", "Error scraping target", "err", err)
slog.ErrorContext(c.ctx, "Error creating client", "err", err)
exporterClientErrors.Inc()

return
}

err = c.client.Login(c.ctx)
if err != nil {
level.Error(c.logger).Log("msg", "Error logging in", "err", err)
slog.ErrorContext(c.ctx, "Error logging in", "err", err)
exporterClientErrors.Inc()

return
Expand Down
22 changes: 10 additions & 12 deletions collector_cm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package main
import (
"context"
"fmt"
"log/slog"
"strconv"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
hitron "github.com/hairyhenderson/hitron_coda"
"github.com/prometheus/client_golang/prometheus"
)
Expand All @@ -15,7 +14,6 @@ import (
type cmCollector struct {
ctx context.Context
client func() *hitron.CableModem
logger log.Logger
sysInfo struct {
usDataRate prometheus.Gauge
dsDataRate prometheus.Gauge
Expand Down Expand Up @@ -49,8 +47,8 @@ type cmCollector struct {
}

//nolint:funlen
func newCMCollector(ctx context.Context, logger log.Logger, clientProvider func() *hitron.CableModem) cmCollector {
c := cmCollector{ctx: ctx, logger: logger, client: clientProvider}
func newCMCollector(ctx context.Context, clientProvider func() *hitron.CableModem) cmCollector {
c := cmCollector{ctx: ctx, client: clientProvider}

sub := "cm"

Expand Down Expand Up @@ -220,7 +218,7 @@ func (c cmCollector) Collect(ch chan<- prometheus.Metric) {
client := c.client()
if client == nil {
err := fmt.Errorf("client not initialized: %v", client)
level.Error(c.logger).Log("msg", "Error scraping target", "err", err)
slog.ErrorContext(c.ctx, "Error scraping target", slog.Any("err", err))
exporterClientErrors.Inc()

return
Expand All @@ -236,7 +234,7 @@ func (c cmCollector) Collect(ch chan<- prometheus.Metric) {
func (c cmCollector) collectVersionInfo(ch chan<- prometheus.Metric, client *hitron.CableModem) {
vi, err := client.CMVersion(c.ctx)
if err != nil {
level.Error(c.logger).Log("msg", "Error scraping CMVersion", "err", err)
slog.ErrorContext(c.ctx, "Error scraping CMVersion", slog.Any("err", err))
exporterRequestErrors.Inc()

return
Expand All @@ -258,7 +256,7 @@ func (c cmCollector) collectVersionInfo(ch chan<- prometheus.Metric, client *hit
func (c cmCollector) collectSysInfo(ch chan<- prometheus.Metric, client *hitron.CableModem) {
si, err := client.CMSysInfo(c.ctx)
if err != nil {
level.Error(c.logger).Log("msg", "Error scraping CMSysInfo", "err", err)
slog.ErrorContext(c.ctx, "Error scraping CMSysInfo", slog.Any("err", err))
exporterRequestErrors.Inc()

return
Expand All @@ -283,7 +281,7 @@ func (c cmCollector) collectSysInfo(ch chan<- prometheus.Metric, client *hitron.
func (c cmCollector) collectDsInfo(ch chan<- prometheus.Metric, client *hitron.CableModem) {
dsinfo, err := client.CMDsInfo(c.ctx)
if err != nil {
level.Error(c.logger).Log("msg", "Error scraping CMDsInfo", "err", err)
slog.ErrorContext(c.ctx, "Error scraping CMDsInfo", slog.Any("err", err))
exporterRequestErrors.Inc()

return
Expand Down Expand Up @@ -315,7 +313,7 @@ func (c cmCollector) collectDsInfo(ch chan<- prometheus.Metric, client *hitron.C
func (c cmCollector) collectUsInfo(ch chan<- prometheus.Metric, client *hitron.CableModem) {
usinfo, err := client.CMUsInfo(c.ctx)
if err != nil {
level.Error(c.logger).Log("msg", "Error scraping CMUsInfo", "err", err)
slog.ErrorContext(c.ctx, "Error scraping CMUsInfo", slog.Any("err", err))
exporterRequestErrors.Inc()

return
Expand Down Expand Up @@ -343,7 +341,7 @@ func (c cmCollector) collectUsInfo(ch chan<- prometheus.Metric, client *hitron.C
func (c cmCollector) collectOfdm(ch chan<- prometheus.Metric, client *hitron.CableModem) {
usofdm, err := client.CMUsOfdm(c.ctx)
if err != nil {
level.Error(c.logger).Log("msg", "Error scraping CMUsOfdm", "err", err)
slog.ErrorContext(c.ctx, "Error scraping CMUsOfdm", slog.Any("err", err))
exporterRequestErrors.Inc()
} else {
for _, channel := range usofdm.Channels {
Expand All @@ -369,7 +367,7 @@ func (c cmCollector) collectOfdm(ch chan<- prometheus.Metric, client *hitron.Cab

dsofdm, err := client.CMDsOfdm(c.ctx)
if err != nil {
level.Error(c.logger).Log("msg", "Error scraping CMDsOfdm", "err", err)
slog.ErrorContext(c.ctx, "Error scraping CMDsOfdm", slog.Any("err", err))
exporterRequestErrors.Inc()
} else {
for _, receiver := range dsofdm.Receivers {
Expand Down
14 changes: 6 additions & 8 deletions collector_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package main
import (
"context"
"fmt"
"log/slog"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
hitron "github.com/hairyhenderson/hitron_coda"
"github.com/prometheus/client_golang/prometheus"
)
Expand All @@ -14,7 +13,6 @@ import (
type routerCollector struct {
ctx context.Context
client func() *hitron.CableModem
logger log.Logger
sysInfo struct {
systemTimeSeconds prometheus.Gauge
lanReceiveBytesTotal *prometheus.CounterVec
Expand All @@ -30,8 +28,8 @@ type routerCollector struct {
}

//nolint:funlen
func newRouterCollector(ctx context.Context, logger log.Logger, clientProvider func() *hitron.CableModem) routerCollector {
c := routerCollector{ctx: ctx, logger: logger, client: clientProvider}
func newRouterCollector(ctx context.Context, clientProvider func() *hitron.CableModem) routerCollector {
c := routerCollector{ctx: ctx, client: clientProvider}

sub := "router"

Expand Down Expand Up @@ -112,15 +110,15 @@ func (c routerCollector) Collect(ch chan<- prometheus.Metric) {
client := c.client()
if client == nil {
err := fmt.Errorf("client not initialized: %v", client)
level.Error(c.logger).Log("msg", "Error scraping target", "err", err)
slog.ErrorContext(c.ctx, "Error scraping target", "err", err)
exporterClientErrors.Inc()

return
}

si, err := client.RouterSysInfo(c.ctx)
if err != nil {
level.Error(c.logger).Log("msg", "Error scraping RouterSysInfo", "err", err)
slog.ErrorContext(c.ctx, "Error scraping RouterSysInfo", "err", err)
exporterRequestErrors.Inc()
} else {
c.sysInfo.systemTimeSeconds.Set(float64(si.SystemTime.Unix()))
Expand Down Expand Up @@ -153,7 +151,7 @@ func (c routerCollector) Collect(ch chan<- prometheus.Metric) {

loc, err := client.RouterLocation(c.ctx)
if err != nil {
level.Error(c.logger).Log("msg", "Error scraping RouterLocation", "err", err)
slog.ErrorContext(c.ctx, "Error scraping RouterLocation", "err", err)
exporterRequestErrors.Inc()
} else {
c.sysInfo.info.With(routerSysInfoLabels(si, loc)).Set(1)
Expand Down
12 changes: 5 additions & 7 deletions collector_wifi.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package main
import (
"context"
"fmt"
"log/slog"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
hitron "github.com/hairyhenderson/hitron_coda"
"github.com/prometheus/client_golang/prometheus"
)
Expand All @@ -14,7 +13,6 @@ import (
type wifiCollector struct {
ctx context.Context
client func() *hitron.CableModem
logger log.Logger

clientStats struct {
rssi *prometheus.GaugeVec
Expand All @@ -23,8 +21,8 @@ type wifiCollector struct {
}
}

func newWiFiCollector(ctx context.Context, logger log.Logger, clientProvider func() *hitron.CableModem) wifiCollector {
c := wifiCollector{ctx: ctx, logger: logger, client: clientProvider}
func newWiFiCollector(ctx context.Context, clientProvider func() *hitron.CableModem) wifiCollector {
c := wifiCollector{ctx: ctx, client: clientProvider}

sub := "wifi"

Expand Down Expand Up @@ -63,15 +61,15 @@ func (c wifiCollector) Collect(ch chan<- prometheus.Metric) {
client := c.client()
if client == nil {
err := fmt.Errorf("client not initialized: %v", client)
level.Error(c.logger).Log("msg", "Error scraping target", "err", err)
slog.ErrorContext(c.ctx, "Error scraping target", "err", err)
exporterClientErrors.Inc()

return
}

wc, err := client.WiFiClient(c.ctx)
if err != nil {
level.Error(c.logger).Log("msg", "Error scraping WiFiClient", "err", err)
slog.ErrorContext(c.ctx, "Error scraping WiFiClient", "err", err)
exporterRequestErrors.Inc()

return
Expand Down
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ go 1.22.3

require (
github.com/alecthomas/kingpin/v2 v2.4.0
github.com/go-kit/log v0.2.1
github.com/hairyhenderson/hitron_coda v0.1.1
github.com/hairyhenderson/hitron_coda v0.2.0
github.com/prometheus/client_golang v1.19.1
github.com/prometheus/common v0.53.0
github.com/stretchr/testify v1.9.0
gopkg.in/yaml.v3 v3.0.1
)
Expand All @@ -17,10 +15,10 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.6.0 // indirect
github.com/prometheus/common v0.53.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/xhit/go-str2duration/v2 v2.1.0 // indirect
golang.org/x/sys v0.18.0 // indirect
Expand Down
13 changes: 6 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/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/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/hairyhenderson/hitron_coda v0.1.1 h1:8AKT/t9iTNGYFMxXkJt1NHnVmhl/42YFCulHHfrpiJ0=
github.com/hairyhenderson/hitron_coda v0.1.1/go.mod h1:D9tCPDr43YjCLLSFlq4fLstxJ96TXDf7gKARmH04WKk=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hairyhenderson/hitron_coda v0.2.0 h1:FSF1T+3kNNsXmWMnfRL00gkR9/eOXvXIejS060CQl1A=
github.com/hairyhenderson/hitron_coda v0.2.0/go.mod h1:EBtf1/EpqRdabuFKLl5G8HQW+L3NmibWPgeetpGHGUo=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -31,6 +29,7 @@ github.com/prometheus/common v0.53.0/go.mod h1:BrxBKv3FWBIGXw89Mg1AeBq7FSyRzXWI3
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
Expand All @@ -45,7 +44,7 @@ google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGm
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading

0 comments on commit 319537f

Please sign in to comment.