Skip to content

Commit

Permalink
Update exporter-toolkit
Browse files Browse the repository at this point in the history
* Update Go to 1.23.
* Replace go-kit/log with slog.

Signed-off-by: SuperQ <[email protected]>
  • Loading branch information
SuperQ committed Oct 10, 2024
1 parent abc1816 commit 7a62116
Show file tree
Hide file tree
Showing 72 changed files with 186 additions and 233 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ executors:
# should also be updated.
golang:
docker:
- image: cimg/go:1.22
- image: cimg/go:1.23
jobs:
test:
executor: golang
Expand All @@ -18,7 +18,7 @@ jobs:
file: mysqld_exporter
integration:
docker:
- image: cimg/go:1.22
- image: cimg/go:1.23
- image: << parameters.mysql_image >>
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
Expand Down
2 changes: 1 addition & 1 deletion .promu.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
go:
# Whenever the Go version is updated here, .circle/config.yml should also
# be updated.
version: 1.22
version: 1.23
repository:
path: github.com/prometheus/mysqld_exporter
build:
Expand Down
4 changes: 2 additions & 2 deletions collector/binlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ package collector
import (
"context"
"fmt"
"log/slog"
"strconv"
"strings"

"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)

Expand Down Expand Up @@ -71,7 +71,7 @@ func (ScrapeBinlogSize) Version() float64 {
}

// Scrape collects data from database connection and sends it over channel as prometheus metric.
func (ScrapeBinlogSize) Scrape(ctx context.Context, instance *instance, ch chan<- prometheus.Metric, logger log.Logger) error {
func (ScrapeBinlogSize) Scrape(ctx context.Context, instance *instance, ch chan<- prometheus.Metric, logger *slog.Logger) error {
var logBin uint8
db := instance.getDB()
err := db.QueryRowContext(ctx, logbinQuery).Scan(&logBin)
Expand Down
3 changes: 1 addition & 2 deletions collector/binlog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"testing"

"github.com/DATA-DOG/go-sqlmock"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/smartystreets/goconvey/convey"
Expand All @@ -44,7 +43,7 @@ func TestScrapeBinlogSize(t *testing.T) {

ch := make(chan prometheus.Metric)
go func() {
if err = (ScrapeBinlogSize{}).Scrape(context.Background(), inst, ch, log.NewNopLogger()); err != nil {
if err = (ScrapeBinlogSize{}).Scrape(context.Background(), inst, ch, promslog.NewNopLogger()); err != nil {

Check failure on line 46 in collector/binlog_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: promslog

Check failure on line 46 in collector/binlog_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: promslog
t.Errorf("error calling function on test: %s", err)
}
close(ch)
Expand Down
4 changes: 2 additions & 2 deletions collector/engine_innodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ package collector

import (
"context"
"log/slog"
"regexp"
"strconv"
"strings"

"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)

Expand Down Expand Up @@ -51,7 +51,7 @@ func (ScrapeEngineInnodbStatus) Version() float64 {
}

// Scrape collects data from database connection and sends it over channel as prometheus metric.
func (ScrapeEngineInnodbStatus) Scrape(ctx context.Context, instance *instance, ch chan<- prometheus.Metric, logger log.Logger) error {
func (ScrapeEngineInnodbStatus) Scrape(ctx context.Context, instance *instance, ch chan<- prometheus.Metric, logger *slog.Logger) error {
db := instance.getDB()
rows, err := db.QueryContext(ctx, engineInnodbStatusQuery)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions collector/engine_innodb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"testing"

"github.com/DATA-DOG/go-sqlmock"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/smartystreets/goconvey/convey"
Expand Down Expand Up @@ -155,7 +154,7 @@ END OF INNODB MONITOR OUTPUT
inst := &instance{db: db}
ch := make(chan prometheus.Metric)
go func() {
if err = (ScrapeEngineInnodbStatus{}).Scrape(context.Background(), inst, ch, log.NewNopLogger()); err != nil {
if err = (ScrapeEngineInnodbStatus{}).Scrape(context.Background(), inst, ch, promslog.NewNopLogger()); err != nil {

Check failure on line 157 in collector/engine_innodb_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: promslog

Check failure on line 157 in collector/engine_innodb_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: promslog
t.Errorf("error calling function on test: %s", err)
}
close(ch)
Expand Down
4 changes: 2 additions & 2 deletions collector/engine_tokudb.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ package collector
import (
"context"
"database/sql"
"log/slog"
"strings"

"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)

Expand Down Expand Up @@ -50,7 +50,7 @@ func (ScrapeEngineTokudbStatus) Version() float64 {
}

// Scrape collects data from database connection and sends it over channel as prometheus metric.
func (ScrapeEngineTokudbStatus) Scrape(ctx context.Context, instance *instance, ch chan<- prometheus.Metric, logger log.Logger) error {
func (ScrapeEngineTokudbStatus) Scrape(ctx context.Context, instance *instance, ch chan<- prometheus.Metric, logger *slog.Logger) error {
db := instance.getDB()
tokudbRows, err := db.QueryContext(ctx, engineTokudbStatusQuery)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions collector/engine_tokudb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"testing"

"github.com/DATA-DOG/go-sqlmock"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/smartystreets/goconvey/convey"
Expand Down Expand Up @@ -60,7 +59,7 @@ func TestScrapeEngineTokudbStatus(t *testing.T) {

ch := make(chan prometheus.Metric)
go func() {
if err = (ScrapeEngineTokudbStatus{}).Scrape(context.Background(), inst, ch, log.NewNopLogger()); err != nil {
if err = (ScrapeEngineTokudbStatus{}).Scrape(context.Background(), inst, ch, promslog.NewNopLogger()); err != nil {

Check failure on line 62 in collector/engine_tokudb_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: promslog

Check failure on line 62 in collector/engine_tokudb_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: promslog
t.Errorf("error calling function on test: %s", err)
}
close(ch)
Expand Down
17 changes: 8 additions & 9 deletions collector/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ package collector
import (
"context"
"fmt"
"log/slog"
"strings"
"sync"
"time"

"github.com/alecthomas/kingpin/v2"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/go-sql-driver/mysql"
"github.com/prometheus/client_golang/prometheus"
)
Expand Down Expand Up @@ -80,14 +79,14 @@ var _ prometheus.Collector = (*Exporter)(nil)
// Exporter collects MySQL metrics. It implements prometheus.Collector.
type Exporter struct {
ctx context.Context
logger log.Logger
logger *slog.Logger
dsn string
scrapers []Scraper
instance *instance
}

// New returns a new MySQL exporter for the provided DSN.
func New(ctx context.Context, dsn string, scrapers []Scraper, logger log.Logger) *Exporter {
func New(ctx context.Context, dsn string, scrapers []Scraper, logger *slog.Logger) *Exporter {
// Setup extra params for the DSN, default to having a lock timeout.
dsnParams := []string{fmt.Sprintf(timeoutParam, *exporterLockTimeout)}

Expand Down Expand Up @@ -129,14 +128,14 @@ func (e *Exporter) scrape(ctx context.Context, ch chan<- prometheus.Metric) floa
scrapeTime := time.Now()
instance, err := newInstance(e.dsn)
if err != nil {
level.Error(e.logger).Log("msg", "Error opening connection to database", "err", err)
e.logger.Error("Error opening connection to database", "err", err)
return 0.0
}
defer instance.Close()
e.instance = instance

if err := instance.Ping(); err != nil {
level.Error(e.logger).Log("msg", "Error pinging mysqld", "err", err)
e.logger.Error("Error pinging mysqld", "err", err)
return 0.0
}

Expand All @@ -157,8 +156,8 @@ func (e *Exporter) scrape(ctx context.Context, ch chan<- prometheus.Metric) floa
label := "collect." + scraper.Name()
scrapeTime := time.Now()
collectorSuccess := 1.0
if err := scraper.Scrape(ctx, instance, ch, log.With(e.logger, "scraper", scraper.Name())); err != nil {
level.Error(e.logger).Log("msg", "Error from scraper", "scraper", scraper.Name(), "target", e.getTargetFromDsn(), "err", err)
if err := scraper.Scrape(ctx, instance, ch, e.logger.With("scraper", scraper.Name())); err != nil {
e.logger.Error("Error from scraper", "scraper", scraper.Name(), "target", e.getTargetFromDsn(), "err", err)
collectorSuccess = 0.0
}
ch <- prometheus.MustNewConstMetric(mysqlScrapeCollectorSuccess, prometheus.GaugeValue, collectorSuccess, label)
Expand All @@ -172,7 +171,7 @@ func (e *Exporter) getTargetFromDsn() string {
// Get target from DSN.
dsnConfig, err := mysql.ParseDSN(e.dsn)
if err != nil {
level.Error(e.logger).Log("msg", "Error parsing DSN", "err", err)
e.logger.Error("Error parsing DSN", "err", err)
return ""
}
return dsnConfig.Addr
Expand Down
3 changes: 1 addition & 2 deletions collector/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"context"
"testing"

"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"github.com/smartystreets/goconvey/convey"
Expand All @@ -36,7 +35,7 @@ func TestExporter(t *testing.T) {
[]Scraper{
ScrapeGlobalStatus{},
},
log.NewNopLogger(),
promslog.NewNopLogger(),

Check failure on line 38 in collector/exporter_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: promslog

Check failure on line 38 in collector/exporter_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: promslog
)

convey.Convey("Metrics describing", t, func() {
Expand Down
4 changes: 2 additions & 2 deletions collector/global_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ package collector
import (
"context"
"database/sql"
"log/slog"
"regexp"
"strconv"
"strings"

"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)

Expand Down Expand Up @@ -99,7 +99,7 @@ func (ScrapeGlobalStatus) Version() float64 {
}

// Scrape collects data from database connection and sends it over channel as prometheus metric.
func (ScrapeGlobalStatus) Scrape(ctx context.Context, instance *instance, ch chan<- prometheus.Metric, logger log.Logger) error {
func (ScrapeGlobalStatus) Scrape(ctx context.Context, instance *instance, ch chan<- prometheus.Metric, logger *slog.Logger) error {
db := instance.getDB()
globalStatusRows, err := db.QueryContext(ctx, globalStatusQuery)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions collector/global_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"testing"

"github.com/DATA-DOG/go-sqlmock"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/smartystreets/goconvey/convey"
Expand Down Expand Up @@ -64,7 +63,7 @@ func TestScrapeGlobalStatus(t *testing.T) {

ch := make(chan prometheus.Metric)
go func() {
if err = (ScrapeGlobalStatus{}).Scrape(context.Background(), inst, ch, log.NewNopLogger()); err != nil {
if err = (ScrapeGlobalStatus{}).Scrape(context.Background(), inst, ch, promslog.NewNopLogger()); err != nil {

Check failure on line 66 in collector/global_status_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: promslog

Check failure on line 66 in collector/global_status_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: promslog
t.Errorf("error calling function on test: %s", err)
}
close(ch)
Expand Down
4 changes: 2 additions & 2 deletions collector/global_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ package collector
import (
"context"
"database/sql"
"log/slog"
"regexp"
"strconv"
"strings"

"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)

Expand Down Expand Up @@ -138,7 +138,7 @@ func (ScrapeGlobalVariables) Version() float64 {
}

// Scrape collects data from database connection and sends it over channel as prometheus metric.
func (ScrapeGlobalVariables) Scrape(ctx context.Context, instance *instance, ch chan<- prometheus.Metric, logger log.Logger) error {
func (ScrapeGlobalVariables) Scrape(ctx context.Context, instance *instance, ch chan<- prometheus.Metric, logger *slog.Logger) error {
db := instance.getDB()
globalVariablesRows, err := db.QueryContext(ctx, globalVariablesQuery)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions collector/global_variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"testing"

"github.com/DATA-DOG/go-sqlmock"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/smartystreets/goconvey/convey"
Expand Down Expand Up @@ -53,7 +52,7 @@ func TestScrapeGlobalVariables(t *testing.T) {

ch := make(chan prometheus.Metric)
go func() {
if err = (ScrapeGlobalVariables{}).Scrape(context.Background(), inst, ch, log.NewNopLogger()); err != nil {
if err = (ScrapeGlobalVariables{}).Scrape(context.Background(), inst, ch, promslog.NewNopLogger()); err != nil {

Check failure on line 55 in collector/global_variables_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: promslog

Check failure on line 55 in collector/global_variables_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: promslog
t.Errorf("error calling function on test: %s", err)
}
close(ch)
Expand Down
4 changes: 2 additions & 2 deletions collector/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import (
"context"
"database/sql"
"fmt"
"log/slog"
"strconv"

"github.com/alecthomas/kingpin/v2"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)

Expand Down Expand Up @@ -100,7 +100,7 @@ func nowExpr() string {
}

// Scrape collects data from database connection and sends it over channel as prometheus metric.
func (ScrapeHeartbeat) Scrape(ctx context.Context, instance *instance, ch chan<- prometheus.Metric, logger log.Logger) error {
func (ScrapeHeartbeat) Scrape(ctx context.Context, instance *instance, ch chan<- prometheus.Metric, logger *slog.Logger) error {
db := instance.getDB()
query := fmt.Sprintf(heartbeatQuery, nowExpr(), *collectHeartbeatDatabase, *collectHeartbeatTable)
heartbeatRows, err := db.QueryContext(ctx, query)
Expand Down
3 changes: 1 addition & 2 deletions collector/heartbeat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

"github.com/DATA-DOG/go-sqlmock"
"github.com/alecthomas/kingpin/v2"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/smartystreets/goconvey/convey"
Expand Down Expand Up @@ -73,7 +72,7 @@ func TestScrapeHeartbeat(t *testing.T) {

ch := make(chan prometheus.Metric)
go func() {
if err = (ScrapeHeartbeat{}).Scrape(context.Background(), inst, ch, log.NewNopLogger()); err != nil {
if err = (ScrapeHeartbeat{}).Scrape(context.Background(), inst, ch, promslog.NewNopLogger()); err != nil {

Check failure on line 75 in collector/heartbeat_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: promslog

Check failure on line 75 in collector/heartbeat_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: promslog
t.Errorf("error calling function on test: %s", err)
}
close(ch)
Expand Down
4 changes: 2 additions & 2 deletions collector/info_schema_auto_increment.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ package collector

import (
"context"
"log/slog"

"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)

Expand Down Expand Up @@ -69,7 +69,7 @@ func (ScrapeAutoIncrementColumns) Version() float64 {
}

// Scrape collects data from database connection and sends it over channel as prometheus metric.
func (ScrapeAutoIncrementColumns) Scrape(ctx context.Context, instance *instance, ch chan<- prometheus.Metric, logger log.Logger) error {
func (ScrapeAutoIncrementColumns) Scrape(ctx context.Context, instance *instance, ch chan<- prometheus.Metric, logger *slog.Logger) error {
db := instance.getDB()
autoIncrementRows, err := db.QueryContext(ctx, infoSchemaAutoIncrementQuery)
if err != nil {
Expand Down
9 changes: 4 additions & 5 deletions collector/info_schema_clientstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ package collector
import (
"context"
"fmt"
"log/slog"
"strings"

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

Expand Down Expand Up @@ -160,16 +159,16 @@ func (ScrapeClientStat) Version() float64 {
}

// Scrape collects data from database connection and sends it over channel as prometheus metric.
func (ScrapeClientStat) Scrape(ctx context.Context, instance *instance, ch chan<- prometheus.Metric, logger log.Logger) error {
func (ScrapeClientStat) Scrape(ctx context.Context, instance *instance, ch chan<- prometheus.Metric, logger *slog.Logger) error {
var varName, varVal string
db := instance.getDB()
err := db.QueryRowContext(ctx, userstatCheckQuery).Scan(&varName, &varVal)
if err != nil {
level.Debug(logger).Log("msg", "Detailed client stats are not available.")
logger.Debug("Detailed client stats are not available.")
return nil
}
if varVal == "OFF" {
level.Debug(logger).Log("msg", "MySQL variable is OFF.", "var", varName)
logger.Debug("MySQL variable is OFF.", "var", varName)
return nil
}

Expand Down
Loading

0 comments on commit 7a62116

Please sign in to comment.