Skip to content

Commit

Permalink
chore: adopt log/slog and drop go-kit/log
Browse files Browse the repository at this point in the history
  • Loading branch information
damoun committed Feb 6, 2025
1 parent 03037fc commit 6c4f6e3
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 55 deletions.
15 changes: 7 additions & 8 deletions collector/channel_followers_total.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package collector

import (
"errors"
"log/slog"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/nicklaw5/helix/v2"
"github.com/prometheus/client_golang/prometheus"
)

type channelFollowersTotalCollector struct {
logger log.Logger
logger *slog.Logger
client *helix.Client
channelNames ChannelNames

Expand All @@ -21,7 +20,7 @@ func init() {
registerCollector("channel_followers_total", defaultEnabled, NewChannelFollowersTotalCollector)
}

func NewChannelFollowersTotalCollector(logger log.Logger, client *helix.Client, channelNames ChannelNames) (Collector, error) {
func NewChannelFollowersTotalCollector(logger *slog.Logger, client *helix.Client, channelNames ChannelNames) (Collector, error) {
c := channelFollowersTotalCollector{
logger: logger,
client: client,
Expand All @@ -47,12 +46,12 @@ func (c channelFollowersTotalCollector) Update(ch chan<- prometheus.Metric) erro
})

if err != nil {
level.Error(c.logger).Log("msg", "Failed to collect users stats from Twitch helix API", "err", err)
c.logger.Error("msg", "Failed to collect users stats from Twitch helix API", "err", err)
return err
}

if usersResp.StatusCode != 200 {
level.Error(c.logger).Log("msg", "Failed to collect users stats from Twitch helix API", "err", usersResp.ErrorMessage)
c.logger.Error("msg", "Failed to collect users stats from Twitch helix API", "err", usersResp.ErrorMessage)
return errors.New(usersResp.ErrorMessage)
}

Expand All @@ -63,12 +62,12 @@ func (c channelFollowersTotalCollector) Update(ch chan<- prometheus.Metric) erro
})

if err != nil {
level.Error(c.logger).Log("msg", "Failed to collect follower stats from Twitch helix API", "err", err)
c.logger.Error("msg", "Failed to collect follower stats from Twitch helix API", "err", err)
return err
}

if usersFollowsResp.StatusCode != 200 {
level.Error(c.logger).Log("msg", "Failed to collect follower stats from Twitch helix API", "err", usersFollowsResp.ErrorMessage)
c.logger.Error("msg", "Failed to collect follower stats from Twitch helix API", "err", usersFollowsResp.ErrorMessage)
return errors.New(usersFollowsResp.ErrorMessage)
}

Expand Down
15 changes: 7 additions & 8 deletions collector/channel_subscribers_total.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package collector

import (
"errors"
"log/slog"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/nicklaw5/helix/v2"
"github.com/prometheus/client_golang/prometheus"
)
Expand All @@ -15,7 +14,7 @@ const (
)

type ChannelSubscriberTotalCollector struct {
logger log.Logger
logger *slog.Logger
client *helix.Client
channelNames ChannelNames

Expand All @@ -26,7 +25,7 @@ func init() {
registerCollector("channel_subscribers_total", defaultDisabled, NewChannelSubscriberTotalCollector)
}

func NewChannelSubscriberTotalCollector(logger log.Logger, client *helix.Client, channelNames ChannelNames) (Collector, error) {
func NewChannelSubscriberTotalCollector(logger *slog.Logger, client *helix.Client, channelNames ChannelNames) (Collector, error) {
c := ChannelSubscriberTotalCollector{
logger: logger,
client: client,
Expand All @@ -52,12 +51,12 @@ func (c ChannelSubscriberTotalCollector) Update(ch chan<- prometheus.Metric) err
})

if err != nil {
level.Error(c.logger).Log("msg", "Failed to collect users stats from Twitch helix API", "err", err)
c.logger.Error("msg", "Failed to collect users stats from Twitch helix API", "err", err)
return err
}

if usersResp.StatusCode != 200 {
level.Error(c.logger).Log("msg", "Failed to collect users stats from Twitch helix API", "err", usersResp.ErrorMessage)
c.logger.Error("msg", "Failed to collect users stats from Twitch helix API", "err", usersResp.ErrorMessage)
return errors.New(usersResp.ErrorMessage)
}

Expand All @@ -68,12 +67,12 @@ func (c ChannelSubscriberTotalCollector) Update(ch chan<- prometheus.Metric) err
})

if err != nil {
level.Error(c.logger).Log("msg", "Failed to collect subscribers stats from Twitch helix API", "err", err)
c.logger.Error("msg", "Failed to collect subscribers stats from Twitch helix API", "err", err)
return err
}

if subscribtionsResp.StatusCode != 200 {
level.Error(c.logger).Log("msg", "Failed to collect subscirbers stats from Twitch helix API", "err", subscribtionsResp.ErrorMessage)
c.logger.Error("msg", "Failed to collect subscirbers stats from Twitch helix API", "err", subscribtionsResp.ErrorMessage)
return errors.New(subscribtionsResp.ErrorMessage)
}

Expand Down
10 changes: 5 additions & 5 deletions collector/channel_up.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package collector

import (
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"log/slog"

"github.com/nicklaw5/helix/v2"
"github.com/prometheus/client_golang/prometheus"
)

type channelUpCollector struct {
logger log.Logger
logger *slog.Logger
client *helix.Client
channelNames ChannelNames

Expand All @@ -19,7 +19,7 @@ func init() {
registerCollector("channel_up", defaultEnabled, NewChannelUpCollector)
}

func NewChannelUpCollector(logger log.Logger, client *helix.Client, channelNames ChannelNames) (Collector, error) {
func NewChannelUpCollector(logger *slog.Logger, client *helix.Client, channelNames ChannelNames) (Collector, error) {
c := channelUpCollector{
logger: logger,
client: client,
Expand All @@ -46,7 +46,7 @@ func (c channelUpCollector) Update(ch chan<- prometheus.Metric) error {
})

if err != nil {
level.Error(c.logger).Log("msg", "could not get streams", "err", err)
c.logger.Error("msg", "could not get streams", "err", err)
return err
}

Expand Down
10 changes: 5 additions & 5 deletions collector/channel_viewers_total.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package collector

import (
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"log/slog"

"github.com/nicklaw5/helix/v2"
"github.com/prometheus/client_golang/prometheus"
)

type ChannelViewersTotalCollector struct {
logger log.Logger
logger *slog.Logger
client *helix.Client
channelNames ChannelNames

Expand All @@ -19,7 +19,7 @@ func init() {
registerCollector("channel_viewers_total", defaultEnabled, NewChannelViewersTotalCollector)
}

func NewChannelViewersTotalCollector(logger log.Logger, client *helix.Client, channelNames ChannelNames) (Collector, error) {
func NewChannelViewersTotalCollector(logger *slog.Logger, client *helix.Client, channelNames ChannelNames) (Collector, error) {
c := ChannelViewersTotalCollector{
logger: logger,
client: client,
Expand All @@ -46,7 +46,7 @@ func (c ChannelViewersTotalCollector) Update(ch chan<- prometheus.Metric) error
})

if err != nil {
level.Error(c.logger).Log("msg", "could not get streams", "err", err)
c.logger.Error("msg", "could not get streams", "err", err)
return err
}

Expand Down
21 changes: 10 additions & 11 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ package collector
import (
"errors"
"fmt"
"log/slog"
"sync"
"time"

"github.com/alecthomas/kingpin/v2"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/nicklaw5/helix/v2"
"github.com/prometheus/client_golang/prometheus"
)
Expand Down Expand Up @@ -39,14 +38,14 @@ const (
)

var (
factories = make(map[string]func(logger log.Logger, client *helix.Client, channelNames ChannelNames) (Collector, error))
factories = make(map[string]func(logger *slog.Logger, client *helix.Client, channelNames ChannelNames) (Collector, error))
initiatedCollectorsMtx = sync.Mutex{}
initiatedCollectors = make(map[string]Collector)
collectorState = make(map[string]*bool)
forcedCollectors = map[string]bool{} // collectors which have been explicitly enabled or disabled
)

func registerCollector(collector string, isDefaultEnabled bool, factory func(logger log.Logger, client *helix.Client, channelNames ChannelNames) (Collector, error)) {
func registerCollector(collector string, isDefaultEnabled bool, factory func(logger *slog.Logger, client *helix.Client, channelNames ChannelNames) (Collector, error)) {
var helpDefaultState string
if isDefaultEnabled {
helpDefaultState = "enabled"
Expand All @@ -67,7 +66,7 @@ func registerCollector(collector string, isDefaultEnabled bool, factory func(log
type Exporter struct {
Collectors map[string]Collector
client *helix.Client
logger log.Logger
logger *slog.Logger
}

// Describe describes all the metrics ever exported by the Twitch exporter. It
Expand Down Expand Up @@ -97,7 +96,7 @@ func collectorFlagAction(collector string) func(ctx *kingpin.ParseContext) error
}
}

func NewExporter(logger log.Logger, client *helix.Client, channelNames ChannelNames, filters ...string) (*Exporter, error) {
func NewExporter(logger *slog.Logger, client *helix.Client, channelNames ChannelNames, filters ...string) (*Exporter, error) {
f := make(map[string]bool)
for _, filter := range filters {
enabled, exist := collectorState[filter]
Expand Down Expand Up @@ -131,7 +130,7 @@ func NewExporter(logger log.Logger, client *helix.Client, channelNames ChannelNa
}

for k, _ := range collectors {
level.Info(logger).Log("msg", "enabled collector", "collector", k)
logger.Info("msg", "enabled collector", "collector", k)
}

return &Exporter{
Expand All @@ -154,21 +153,21 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
wg.Wait()
}

func execute(name string, c Collector, ch chan<- prometheus.Metric, logger log.Logger) {
func execute(name string, c Collector, ch chan<- prometheus.Metric, logger *slog.Logger) {
begin := time.Now()
err := c.Update(ch)
duration := time.Since(begin)
var success float64

if err != nil {
if IsNoDataError(err) {
level.Error(logger).Log("collector returned no data", "name", name, "duration_seconds", duration.Seconds(), "err", err)
logger.Error("collector returned no data", "name", name, "duration_seconds", duration.Seconds(), "err", err)
} else {
level.Error(logger).Log("collector failed", "name", name, "duration_seconds", duration.Seconds(), "err", err)
logger.Error("collector failed", "name", name, "duration_seconds", duration.Seconds(), "err", err)
}
success = 0
} else {
level.Info(logger).Log("collector succeeded", "name", name, "duration_seconds", duration.Seconds())
logger.Info("collector succeeded", "name", name, "duration_seconds", duration.Seconds())
success = 1
}

Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ module github.com/damoun/twitch_exporter

require (
github.com/alecthomas/kingpin/v2 v2.4.0
github.com/go-kit/log v0.2.1
github.com/nicklaw5/helix/v2 v2.31.0
github.com/prometheus/client_golang v1.20.5
github.com/prometheus/common v0.62.0
github.com/prometheus/exporter-toolkit v0.11.0
github.com/prometheus/exporter-toolkit v0.13.2
)

require (
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/golang-jwt/jwt/v4 v4.5.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/prometheus/client_model v0.6.1 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV
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.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA=
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/golang-jwt/jwt/v4 v4.5.1 h1:JdqV9zKUdtaa9gdPlywC3aeoEsR681PlKC+4F5gQgeo=
github.com/golang-jwt/jwt/v4 v4.5.1/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
Expand All @@ -30,6 +26,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 @@ -44,8 +44,8 @@ github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
github.com/prometheus/common v0.62.0 h1:xasJaQlnWAeyHdUBeGjXmutelfJHWMRr+Fg4QszZ2Io=
github.com/prometheus/common v0.62.0/go.mod h1:vyBcEuLSvWos9B1+CyL7JZ2up+uFzXhkqml0W5zIY1I=
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/exporter-toolkit v0.13.2 h1:Z02fYtbqTMy2i/f+xZ+UK5jy/bl1Ex3ndzh06T/Q9DQ=
github.com/prometheus/exporter-toolkit v0.13.2/go.mod h1:tCqnfx21q6qN1KA4U3Bfb8uWzXfijIrJz3/kTIqMV7g=
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 Down
17 changes: 8 additions & 9 deletions twitch_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ package main

import (
"fmt"
"log/slog"
"net/http"
"os"

kingpin "github.com/alecthomas/kingpin/v2"
"github.com/damoun/twitch_exporter/collector"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/nicklaw5/helix/v2"
"github.com/prometheus/client_golang/prometheus"
versioncollector "github.com/prometheus/client_golang/prometheus/collectors/version"
Expand All @@ -36,11 +35,11 @@ var (
)

type promHTTPLogger struct {
logger log.Logger
logger *slog.Logger
}

func (l promHTTPLogger) Println(v ...interface{}) {
level.Error(l.logger).Log("msg", fmt.Sprint(v...))
l.logger.Error("msg", fmt.Sprint(v...))
}

// Channels creates a collection of Channels from a kingpin command line argument.
Expand All @@ -63,22 +62,22 @@ func main() {
kingpin.Parse()

logger := promslog.New(promslogConfig)
level.Info(logger).Log("msg", "Starting twitch_exporter", "version", version.Info())
level.Info(logger).Log("build_context", version.BuildContext())
logger.Info("msg", "Starting twitch_exporter", "version", version.Info())
logger.Info("build_context", version.BuildContext())

client, err := helix.NewClient(&helix.Options{
ClientID: *twitchClientID,
UserAccessToken: *twitchAccessToken,
})

if err != nil {
level.Error(logger).Log("msg", "could not initialise twitch client", "err", err)
logger.Error("msg", "could not initialise twitch client", "err", err)
return
}

exporter, err := collector.NewExporter(logger, client, *twitchChannel)
if err != nil {
level.Error(logger).Log("msg", "Error creating the exporter", "err", err)
logger.Error("msg", "Error creating the exporter", "err", err)
os.Exit(1)
}

Expand Down Expand Up @@ -107,7 +106,7 @@ func main() {

srv := &http.Server{}
if err := web.ListenAndServe(srv, webConfig, logger); err != nil {
level.Error(logger).Log("msg", "Error starting HTTP server", "err", err)
logger.Error("msg", "Error starting HTTP server", "err", err)
os.Exit(1)
}
}

0 comments on commit 6c4f6e3

Please sign in to comment.