From 57467360721443f95875b4bd8942d8ac91f9e1f7 Mon Sep 17 00:00:00 2001 From: Andreas Gebhardt Date: Thu, 3 Oct 2024 10:27:00 +0200 Subject: [PATCH] Bump github.com/prometheus/exporter-toolkit from 0.12.0 to 0.13.0 Bumps [github.com/prometheus/exporter-toolkit](https://github.com/prometheus/exporter-toolkit) from 0.12.0 to 0.13.0. - [Release notes](https://github.com/prometheus/exporter-toolkit/releases) - [Changelog](https://github.com/prometheus/exporter-toolkit/blob/master/CHANGELOG.md) - [Commits](https://github.com/prometheus/exporter-toolkit/compare/v0.12.0...v0.13.0) --- updated-dependencies: - dependency-name: github.com/prometheus/exporter-toolkit dependency-type: direct:production update-type: version-update:semver-minor ... --- collector/collector.go | 13 ++++++------- collector/collector_test.go | 5 +++-- go.mod | 4 +--- go.sum | 8 ++------ mq/mq.go | 31 +++++++++++++------------------ mq_exporter.go | 30 +++++++++++++----------------- mq_exporter_test.go | 37 ++++++++++++++++++++++--------------- 7 files changed, 60 insertions(+), 68 deletions(-) diff --git a/collector/collector.go b/collector/collector.go index 2986ab3..62526f2 100644 --- a/collector/collector.go +++ b/collector/collector.go @@ -16,11 +16,10 @@ package collector import ( "context" + "log/slog" "sync" "time" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus/client_golang/prometheus" ) @@ -56,7 +55,7 @@ type QueueMetrics struct { type QueueCollector struct { sync.Mutex - logger log.Logger + logger *slog.Logger timeout time.Duration queues []Queue @@ -77,7 +76,7 @@ func (m *QueueMetadata) prometheusLabelValues() []string { } } -func NewQueueCollector(logger log.Logger, timeout time.Duration, queues []Queue) *QueueCollector { +func NewQueueCollector(logger *slog.Logger, timeout time.Duration, queues []Queue) *QueueCollector { newQueueMetric := func(name string, help string) *prometheus.GaugeVec { return prometheus.NewGaugeVec(prometheus.GaugeOpts{ @@ -150,7 +149,7 @@ func (c *QueueCollector) Collect(ch chan<- prometheus.Metric) { c.requestDuration.Collect(ch) } -func collect(logger log.Logger, timeout time.Duration, queues []Queue, ctx context.Context) *[]QueueMetrics { +func collect(logger *slog.Logger, timeout time.Duration, queues []Queue, ctx context.Context) *[]QueueMetrics { metrics := make([]QueueMetrics, 0) @@ -176,11 +175,11 @@ func collect(logger log.Logger, timeout time.Duration, queues []Queue, ctx conte for { select { case metric := <-ch: - level.Debug(logger).Log("msg", "Got queue metrics", "queue", metric.Metadata.QueueName, "connection", metric.Metadata.ConnectionName, "queue_manager", metric.Metadata.QMgrName, "channel", metric.Metadata.ChannelName) + logger.Debug("Got queue metrics", "queue", metric.Metadata.QueueName, "connection", metric.Metadata.ConnectionName, "queue_manager", metric.Metadata.QMgrName, "channel", metric.Metadata.ChannelName) metrics = append(metrics, metric) case <-ctx.Done(): if ctx.Err() == context.DeadlineExceeded { - level.Error(logger).Log("msg", "Deadline exceeded while waiting for queue metrics", "timeout", timeout) + logger.Error("Deadline exceeded while waiting for queue metrics", "timeout", timeout) } return &metrics } diff --git a/collector/collector_test.go b/collector/collector_test.go index 4831bdf..6ac9a1d 100644 --- a/collector/collector_test.go +++ b/collector/collector_test.go @@ -17,18 +17,19 @@ package collector import ( "context" "errors" + "io" + "log/slog" "runtime" "strings" "testing" "time" - "github.com/go-kit/log" "github.com/google/go-cmp/cmp" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/testutil" ) -var logger = log.NewNopLogger() +var logger = slog.New(slog.NewTextHandler(io.Discard, nil)) type succeedingQueueMetricReader struct { value QueueMetrics diff --git a/go.mod b/go.mod index 6e0e131..204c91a 100644 --- a/go.mod +++ b/go.mod @@ -2,12 +2,11 @@ module github.com/agebhar1/mq_exporter require ( github.com/alecthomas/kingpin/v2 v2.4.0 - github.com/go-kit/log v0.2.1 github.com/google/go-cmp v0.6.0 github.com/ibm-messaging/mq-golang/v5 v5.6.0 github.com/prometheus/client_golang v1.20.4 github.com/prometheus/common v0.60.0 - github.com/prometheus/exporter-toolkit v0.12.0 + github.com/prometheus/exporter-toolkit v0.13.0 gopkg.in/yaml.v2 v2.4.0 gotest.tools/v3 v3.5.1 ) @@ -17,7 +16,6 @@ require ( 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/jpillora/backoff v1.0.0 // indirect github.com/klauspost/compress v1.17.9 // indirect github.com/kylelemons/godebug v1.1.0 // indirect diff --git a/go.sum b/go.sum index 5cb91a7..7076a7f 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -46,8 +42,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.60.0 h1:+V9PAREWNvJMAuJ1x1BaWl9dewMW4YrHZQbx0sJNllA= github.com/prometheus/common v0.60.0/go.mod h1:h0LYf1R1deLSKtD4Vdg8gy4RuOvENW2J/h19V5NADQw= -github.com/prometheus/exporter-toolkit v0.12.0 h1:DkE5RcEZR3lQA2QD5JLVQIf41dFKNsVMXFhgqcif7fo= -github.com/prometheus/exporter-toolkit v0.12.0/go.mod h1:fQH0KtTn0yrrS0S82kqppRjDDiwMfIQUwT+RBRRhwUc= +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= diff --git a/mq/mq.go b/mq/mq.go index 84347b1..7feea3f 100644 --- a/mq/mq.go +++ b/mq/mq.go @@ -16,14 +16,13 @@ package mq import ( "fmt" + "log/slog" "os" "strings" "sync/atomic" "time" "github.com/agebhar1/mq_exporter/collector" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/ibm-messaging/mq-golang/v5/ibmmq" "gopkg.in/yaml.v2" ) @@ -113,12 +112,12 @@ func (cfg *MqConfiguration) validateReadFromYaml() error { type MqConnection struct { isConnecting *int64 cfg *MqConfiguration - logger log.Logger + logger *slog.Logger qMgr ibmmq.MQQueueManager queues map[string]ibmmq.MQObject } -func NewMqConnection(logger log.Logger, cfgFilename string) (*MqConnection, error) { +func NewMqConnection(logger *slog.Logger, cfgFilename string) (*MqConnection, error) { cfg, err := readConfigYaml(cfgFilename) if err != nil { @@ -131,7 +130,7 @@ func NewMqConnection(logger log.Logger, cfgFilename string) (*MqConnection, erro c := MqConnection{ isConnecting: new(int64), cfg: cfg, - logger: log.WithSuffix(logger, "connName", cfg.ConnName, "channel", cfg.Channel, "queueManager", cfg.QueueManager), + logger: logger.With("connName", cfg.ConnName, "channel", cfg.Channel, "queueManager", cfg.QueueManager), } *c.isConnecting = NO @@ -150,7 +149,7 @@ func (c *MqConnection) connect() error { } defer func() { atomic.StoreInt64(c.isConnecting, NO) - level.Info(c.logger).Log("msg", "connected to queue manager") + c.logger.Info("connected to queue manager") }() if len(c.cfg.Queues) > 0 { @@ -208,7 +207,7 @@ func (c *MqConnection) handleReturnValue(mqret *ibmmq.MQReturn) { go func() { err := c.connect() if err != nil { - level.Error(c.logger).Log("msg", "failed re-connect", "err", err) + c.logger.Error("failed re-connect", "err", err) } }() } @@ -240,7 +239,7 @@ func (c *MqConnection) Queues() []collector.Queue { Metadata: metadata, Reader: &MqQueue{ connection: c, - logger: log.WithSuffix(c.logger, "queue", queue), + logger: c.logger.With("queue", queue), metadata: metadata, }, }) @@ -249,23 +248,19 @@ func (c *MqConnection) Queues() []collector.Queue { } func (c *MqConnection) Close() { - - logInfo := level.Info(c.logger).Log - logError := level.Error(c.logger).Log - for _, queue := range c.queues { err := queue.Close(0) if err == nil { - logInfo("msg", "closed queue", "queue", queue.Name) + c.logger.Info("closed queue", "queue", queue.Name) } else { - logError("msg", "failed to close queue", "err", err, "queue", queue.Name) + c.logger.Error("failed to close queue", "err", err, "queue", queue.Name) } } err := c.qMgr.Disc() if err == nil { - logInfo("msg", "disconnected from queue manager") + c.logger.Info("disconnected from queue manager") } else { - logError("msg", "failed to disconnect from queue manager", "err", err) + c.logger.Error("failed to disconnect from queue manager", "err", err) } } @@ -275,7 +270,7 @@ func (c *MqConnection) Timeout() time.Duration { type MqQueue struct { connection *MqConnection - logger log.Logger + logger *slog.Logger metadata collector.QueueMetadata } @@ -284,7 +279,7 @@ func (q *MqQueue) Read() (collector.QueueMetrics, error) { values, err := q.connection.inqQueue(q, selectors) if err != nil { err := err.(*ibmmq.MQReturn) - level.Error(q.logger).Log("msg", "error inquire queue", "err", err, "mqcc", err.MQCC, "mqcr", err.MQRC) + q.logger.Error("error inquire queue", "err", err, "mqcc", err.MQCC, "mqcr", err.MQRC) return collector.QueueMetrics{}, err } return collector.QueueMetrics{ diff --git a/mq_exporter.go b/mq_exporter.go index c1ffde3..1f71aae 100644 --- a/mq_exporter.go +++ b/mq_exporter.go @@ -17,7 +17,10 @@ package main import ( "context" versionc "github.com/prometheus/client_golang/prometheus/collectors/version" + "github.com/prometheus/common/promslog" + "github.com/prometheus/common/promslog/flag" "io" + "log/slog" "net/http" "os" "os/signal" @@ -26,13 +29,9 @@ import ( "github.com/agebhar1/mq_exporter/collector" "github.com/agebhar1/mq_exporter/mq" "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" "github.com/prometheus/client_golang/prometheus/promhttp" - "github.com/prometheus/common/promlog" - "github.com/prometheus/common/promlog/flag" "github.com/prometheus/common/version" "github.com/prometheus/exporter-toolkit/web" webflag "github.com/prometheus/exporter-toolkit/web/kingpinflag" @@ -41,7 +40,7 @@ import ( var name = "mq_exporter" type appCtx struct { - logger log.Logger + logger *slog.Logger sigs chan os.Signal configFile *string @@ -49,7 +48,7 @@ type appCtx struct { webTelemetryPath *string } -func newAppCtx(args []string, usageWriter io.Writer, errorWriter io.Writer, logger log.Logger) *appCtx { +func newAppCtx(args []string, usageWriter io.Writer, errorWriter io.Writer, logger *slog.Logger) *appCtx { ctx := appCtx{} @@ -64,15 +63,15 @@ func newAppCtx(args []string, usageWriter io.Writer, errorWriter io.Writer, logg app.HelpFlag.Short('h') app.VersionFlag.Short('v') - promlogConfig := &promlog.Config{} - flag.AddFlags(app, promlogConfig) + promslogConfig := &promslog.Config{Style: promslog.GoKitStyle} + flag.AddFlags(app, promslogConfig) kingpin.MustParse(app.Parse(args)) if logger != nil { ctx.logger = logger } else { - ctx.logger = promlog.New(promlogConfig) + ctx.logger = promslog.New(promslogConfig) } ctx.sigs = make(chan os.Signal) @@ -83,11 +82,8 @@ func newAppCtx(args []string, usageWriter io.Writer, errorWriter io.Writer, logg func (app *appCtx) run() int { - logInfo := level.Info(app.logger).Log - logError := level.Error(app.logger).Log - - logInfo("msg", "Starting", "app_name", name, "version", version.Version, "branch", version.Branch, "revision", version.Revision) - logInfo("msg", "Build context", "go", version.GoVersion, "build_user", version.BuildUser, "build_date", version.BuildDate) + app.logger.Info("Starting", "app_name", name, "version", version.Version, "branch", version.Branch, "revision", version.Revision) + app.logger.Info("Build context", "go", version.GoVersion, "build_user", version.BuildUser, "build_date", version.BuildDate) reg := prometheus.NewRegistry() reg.MustRegister(versionc.NewCollector(name)) @@ -96,7 +92,7 @@ func (app *appCtx) run() int { mqConnection, err := mq.NewMqConnection(app.logger, *app.configFile) if err != nil { - logError("msg", err) + app.logger.Error(err.Error()) return 1 } @@ -128,12 +124,12 @@ func (app *appCtx) run() int { mqConnection.Close() - logInfo("msg", "Shutdown server.") + app.logger.Info("Shutdown server.") server.Shutdown(context.Background()) }() if err := web.ListenAndServe(server, app.toolkitFlags, app.logger); err != http.ErrServerClosed { - logError("msg", "Serve error", "err", err) + app.logger.Error("Serve error", "err", err) return 2 } return 0 diff --git a/mq_exporter_test.go b/mq_exporter_test.go index f6c9fad..3106985 100644 --- a/mq_exporter_test.go +++ b/mq_exporter_test.go @@ -16,6 +16,7 @@ package main import ( "io" + "log/slog" "net/http" "os" "regexp" @@ -26,15 +27,8 @@ import ( var configArg = "--config=fixtures/config-no-queues.yaml" type listenAddrListener struct { - c chan (string) -} - -func (l listenAddrListener) Log(keyvals ...interface{}) error { - if len(keyvals) == 6 && keyvals[3].(string) == "Listening on" { - addr := keyvals[5].(string) - l.c <- addr - } - return nil + logger *slog.Logger + c chan string } func (l listenAddrListener) addr() string { @@ -46,7 +40,20 @@ func (l listenAddrListener) close() { } func newListenAddrListener() listenAddrListener { - return listenAddrListener{c: make(chan string, 1)} + + c := make(chan string, 1) + + logger := slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{ + Level: slog.LevelInfo, + ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr { + if a.Key == "address" { + c <- a.Value.String() + } + return a + }, + })) + + return listenAddrListener{logger: logger, c: c} } func TestDefaultMetricsEndpoint(t *testing.T) { @@ -54,7 +61,7 @@ func TestDefaultMetricsEndpoint(t *testing.T) { l := newListenAddrListener() defer l.close() - app := newAppCtx([]string{"--web.listen-address=127.0.0.1:0", configArg}, os.Stdout, os.Stderr, l) + app := newAppCtx([]string{"--web.listen-address=127.0.0.1:0", configArg}, os.Stdout, os.Stderr, l.logger) go app.run() @@ -99,7 +106,7 @@ func TestCustomMetricsEndpoint(t *testing.T) { l := newListenAddrListener() defer l.close() - app := newAppCtx([]string{"--web.listen-address=:0", "--web.telemetry-path=/telemetry", configArg}, os.Stdout, os.Stderr, l) + app := newAppCtx([]string{"--web.listen-address=:0", "--web.telemetry-path=/telemetry", configArg}, os.Stdout, os.Stderr, l.logger) go app.run() @@ -134,7 +141,7 @@ func TestLandingPageDefaultMetricsEndpoint(t *testing.T) { l := newListenAddrListener() defer l.close() - app := newAppCtx([]string{"--web.listen-address=:0", configArg}, os.Stdout, os.Stderr, l) + app := newAppCtx([]string{"--web.listen-address=:0", configArg}, os.Stdout, os.Stderr, l.logger) go app.run() @@ -169,7 +176,7 @@ func TestLandingPageCustomMetricsEndpoint(t *testing.T) { l := newListenAddrListener() defer l.close() - app := newAppCtx([]string{"--web.listen-address=:0", "--web.telemetry-path=/telemetry", configArg}, os.Stdout, os.Stderr, l) + app := newAppCtx([]string{"--web.listen-address=:0", "--web.telemetry-path=/telemetry", configArg}, os.Stdout, os.Stderr, l.logger) go app.run() @@ -204,7 +211,7 @@ func TestBuildInfoMetric(t *testing.T) { l := newListenAddrListener() defer l.close() - app := newAppCtx([]string{"--web.listen-address=127.0.0.1:0", configArg}, os.Stdout, os.Stderr, l) + app := newAppCtx([]string{"--web.listen-address=127.0.0.1:0", configArg}, os.Stdout, os.Stderr, l.logger) go app.run()