Skip to content

Commit

Permalink
Adapt to slog change in upstream
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Kosegi <[email protected]>
  • Loading branch information
rkosegi committed Sep 10, 2024
1 parent da2c644 commit 7fd42c4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 24 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ toolchain go1.22.5

require (
github.com/alecthomas/kingpin/v2 v2.4.0
github.com/go-kit/log v0.2.1
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.20.3
github.com/prometheus/common v0.59.1
Expand All @@ -35,7 +34,6 @@ require (
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logfmt/logfmt v0.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
Expand Down
4 changes: 0 additions & 4 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/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
Expand Down
25 changes: 12 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

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

Expand All @@ -26,13 +27,11 @@ import (
"gopkg.in/yaml.v3"

"github.com/alecthomas/kingpin/v2"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors/version"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
"github.com/prometheus/common/promslog"
"github.com/prometheus/common/promslog/flag"
pv "github.com/prometheus/common/version"
"github.com/prometheus/exporter-toolkit/web"
webflag "github.com/prometheus/exporter-toolkit/web/kingpinflag"
Expand All @@ -52,7 +51,7 @@ func init() {
prometheus.MustRegister(version.NewCollector(progName))
}

func newHandler(devices *[]types.Device, logger log.Logger, m types.Metrics) http.HandlerFunc {
func newHandler(devices *[]types.Device, logger *slog.Logger, m types.Metrics) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
registry := prometheus.NewRegistry()
registry.MustRegister(exporter.NewExporter(devices, logger, m))
Expand All @@ -67,27 +66,27 @@ func newHandler(devices *[]types.Device, logger log.Logger, m types.Metrics) htt
}

func main() {
promlogConfig := &promlog.Config{}
promlogConfig := &promslog.Config{}
flag.AddFlags(kingpin.CommandLine, promlogConfig)
kingpin.Version(pv.Print(progName))
kingpin.HelpFlag.Short('h')
kingpin.Parse()

logger := promlog.New(promlogConfig)
level.Info(logger).Log("msg", fmt.Sprintf("Starting %s", progName), "version", pv.Info(), "config", *configFile)
logger := promslog.New(promlogConfig)
logger.Info(fmt.Sprintf("Starting %s", progName), "version", pv.Info(), "config", *configFile)

devs, err := loadConfig(*configFile)

if err != nil {
level.Error(logger).Log("msg", "Error reading configuration", "err", err)
logger.Error("Error reading configuration", "err", err)
os.Exit(1)
}

if len(*devs) == 0 {
level.Error(logger).Log("msg", "no devices configured")
logger.Error("no devices configured")
os.Exit(1)
} else {
level.Info(logger).Log("msg", fmt.Sprintf("Configured %d devices", len(*devs)))
logger.Info(fmt.Sprintf("Configured %d devices", len(*devs)))
}

var landingPage = []byte(`<html>
Expand All @@ -103,12 +102,12 @@ func main() {
http.Handle(*metricPath, promhttp.InstrumentMetricHandler(prometheus.DefaultRegisterer, handlerFunc))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if _, err = w.Write(landingPage); err != nil {
level.Error(logger).Log("msg", "Unable to write page content", "err", err)
logger.Error("Unable to write page content", "err", err)
}
})
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("Error starting HTTP server", "err", err)
os.Exit(1)
}
}
Expand Down
9 changes: 4 additions & 5 deletions pkg/exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ limitations under the License.
package exporter

import (
"log/slog"
"time"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/rkosegi/tuya-smartplug-exporter/pkg/proto"
"github.com/rkosegi/tuya-smartplug-exporter/pkg/types"
Expand All @@ -29,7 +28,7 @@ import (
type exporter struct {
m types.Metrics
devs *[]types.Device
l log.Logger
l *slog.Logger
}

func (e *exporter) Describe(descs chan<- *prometheus.Desc) {
Expand All @@ -53,7 +52,7 @@ func (e *exporter) Collect(c chan<- prometheus.Metric) {
client := proto.NewClient(dev.Ip, dev.Id, []byte(dev.Key))
status, err := client.Status()
if err != nil {
level.Warn(e.l).Log("msg", "error during scrape", "device", dev.Name, "error", err)
e.l.Warn("error during scrape", "device", dev.Name, "error", err)
e.m.ScrapeErrors.With(labels).Inc()
e.m.Error.Set(1)
} else {
Expand All @@ -78,7 +77,7 @@ func (e *exporter) Collect(c chan<- prometheus.Metric) {
e.m.SwitchOn.Collect(c)
}

func NewExporter(devices *[]types.Device, logger log.Logger, m types.Metrics) prometheus.Collector {
func NewExporter(devices *[]types.Device, logger *slog.Logger, m types.Metrics) prometheus.Collector {
return &exporter{
m: m,
devs: devices,
Expand Down

0 comments on commit 7fd42c4

Please sign in to comment.