Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore!: adopt log/slog, drop go-kit/log #875

Merged
merged 2 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ linters:
enable:
- misspell
- staticcheck
- sloglint
disable-all: true
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
4 changes: 2 additions & 2 deletions collector/binlog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ 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/prometheus/common/promslog"
"github.com/smartystreets/goconvey/convey"
)

Expand All @@ -44,7 +44,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 {
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
4 changes: 2 additions & 2 deletions collector/engine_innodb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ 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/prometheus/common/promslog"
"github.com/smartystreets/goconvey/convey"
)

Expand Down Expand Up @@ -155,7 +155,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 {
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
4 changes: 2 additions & 2 deletions collector/engine_tokudb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ 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/prometheus/common/promslog"
"github.com/smartystreets/goconvey/convey"
)

Expand Down Expand Up @@ -60,7 +60,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 {
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
4 changes: 2 additions & 2 deletions collector/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import (
"context"
"testing"

"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"github.com/prometheus/common/promslog"
"github.com/smartystreets/goconvey/convey"
)

Expand All @@ -36,7 +36,7 @@ func TestExporter(t *testing.T) {
[]Scraper{
ScrapeGlobalStatus{},
},
log.NewNopLogger(),
promslog.NewNopLogger(),
)

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
4 changes: 2 additions & 2 deletions collector/global_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ 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/prometheus/common/promslog"
"github.com/smartystreets/goconvey/convey"
)

Expand Down Expand Up @@ -64,7 +64,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 {
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
4 changes: 2 additions & 2 deletions collector/global_variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ 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/prometheus/common/promslog"
"github.com/smartystreets/goconvey/convey"
)

Expand Down Expand Up @@ -53,7 +53,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 {
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
4 changes: 2 additions & 2 deletions collector/heartbeat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ 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/prometheus/common/promslog"
"github.com/smartystreets/goconvey/convey"
)

Expand Down Expand Up @@ -73,7 +73,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 {
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