Skip to content

Commit

Permalink
Switch to go-kit for logs.
Browse files Browse the repository at this point in the history
Some log messages were made more consistent.

Logging of DSN was removed, as it may contain a password.

Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
  • Loading branch information
brian-brazil committed Sep 12, 2019
1 parent 8e4e7de commit 73160d9
Show file tree
Hide file tree
Showing 417 changed files with 3,634 additions and 232,853 deletions.
3 changes: 2 additions & 1 deletion collector/binlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strconv"
"strings"

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

Expand Down Expand Up @@ -71,7 +72,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, db *sql.DB, ch chan<- prometheus.Metric) error {
func (ScrapeBinlogSize) Scrape(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric, logger log.Logger) error {
var logBin uint8
err := db.QueryRowContext(ctx, logbinQuery).Scan(&logBin)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion collector/binlog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"testing"

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

ch := make(chan prometheus.Metric)
go func() {
if err = (ScrapeBinlogSize{}).Scrape(context.Background(), db, ch); err != nil {
if err = (ScrapeBinlogSize{}).Scrape(context.Background(), db, ch, log.NewNopLogger()); err != nil {
t.Errorf("error calling function on test: %s", err)
}
close(ch)
Expand Down
3 changes: 2 additions & 1 deletion collector/engine_innodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strconv"
"strings"

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

Expand Down Expand Up @@ -51,7 +52,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, db *sql.DB, ch chan<- prometheus.Metric) error {
func (ScrapeEngineInnodbStatus) Scrape(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric, logger log.Logger) error {
rows, err := db.QueryContext(ctx, engineInnodbStatusQuery)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion collector/engine_innodb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"testing"

"github.com/DATA-DOG/go-sqlmock"
"github.com/go-kit/kit/log"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/smartystreets/goconvey/convey"
Expand Down Expand Up @@ -154,7 +155,7 @@ END OF INNODB MONITOR OUTPUT

ch := make(chan prometheus.Metric)
go func() {
if err = (ScrapeEngineInnodbStatus{}).Scrape(context.Background(), db, ch); err != nil {
if err = (ScrapeEngineInnodbStatus{}).Scrape(context.Background(), db, ch, log.NewNopLogger()); err != nil {
t.Errorf("error calling function on test: %s", err)
}
close(ch)
Expand Down
3 changes: 2 additions & 1 deletion collector/engine_tokudb.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"database/sql"
"strings"

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

Expand Down Expand Up @@ -49,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, db *sql.DB, ch chan<- prometheus.Metric) error {
func (ScrapeEngineTokudbStatus) Scrape(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric, logger log.Logger) error {
tokudbRows, err := db.QueryContext(ctx, engineTokudbStatusQuery)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion collector/engine_tokudb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"testing"

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

ch := make(chan prometheus.Metric)
go func() {
if err = (ScrapeEngineTokudbStatus{}).Scrape(context.Background(), db, ch); err != nil {
if err = (ScrapeEngineTokudbStatus{}).Scrape(context.Background(), db, ch, log.NewNopLogger()); err != nil {
t.Errorf("error calling function on test: %s", err)
}
close(ch)
Expand Down
15 changes: 9 additions & 6 deletions collector/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ import (
"sync"
"time"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
_ "github.com/go-sql-driver/mysql"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"gopkg.in/alecthomas/kingpin.v2"
)

Expand Down Expand Up @@ -76,13 +77,14 @@ var _ prometheus.Collector = (*Exporter)(nil)
// Exporter collects MySQL metrics. It implements prometheus.Collector.
type Exporter struct {
ctx context.Context
logger log.Logger
dsn string
scrapers []Scraper
metrics Metrics
}

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

Expand All @@ -99,6 +101,7 @@ func New(ctx context.Context, dsn string, metrics Metrics, scrapers []Scraper) *

return &Exporter{
ctx: ctx,
logger: logger,
dsn: dsn,
scrapers: scrapers,
metrics: metrics,
Expand Down Expand Up @@ -130,7 +133,7 @@ func (e *Exporter) scrape(ctx context.Context, ch chan<- prometheus.Metric) {
scrapeTime := time.Now()
db, err := sql.Open("mysql", e.dsn)
if err != nil {
log.Errorln("Error opening connection to database:", err)
level.Error(e.logger).Log("msg", "Error opening connection to database", "err", err)
e.metrics.Error.Set(1)
return
}
Expand All @@ -143,7 +146,7 @@ func (e *Exporter) scrape(ctx context.Context, ch chan<- prometheus.Metric) {
db.SetConnMaxLifetime(1 * time.Minute)

if err := db.PingContext(ctx); err != nil {
log.Errorln("Error pinging mysqld:", err)
level.Error(e.logger).Log("msg", "Error pinging mysqld", "err", err)
e.metrics.MySQLUp.Set(0)
e.metrics.Error.Set(1)
return
Expand All @@ -167,8 +170,8 @@ func (e *Exporter) scrape(ctx context.Context, ch chan<- prometheus.Metric) {
defer wg.Done()
label := "collect." + scraper.Name()
scrapeTime := time.Now()
if err := scraper.Scrape(ctx, db, ch); err != nil {
log.Errorln("Error scraping for "+label+":", err)
if err := scraper.Scrape(ctx, db, ch, log.With(e.logger, "scraper", scraper.Name())); err != nil {
level.Error(e.logger).Log("msg", "Error from scraper", "scraper", scraper.Name(), "err", err)
e.metrics.ScrapeErrors.WithLabelValues(label).Inc()
e.metrics.Error.Set(1)
}
Expand Down
5 changes: 4 additions & 1 deletion collector/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"database/sql"
"testing"

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

convey.Convey("Metrics describing", t, func() {
ch := make(chan *prometheus.Desc)
Expand Down
3 changes: 2 additions & 1 deletion collector/global_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strconv"
"strings"

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

Expand Down Expand Up @@ -98,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, db *sql.DB, ch chan<- prometheus.Metric) error {
func (ScrapeGlobalStatus) Scrape(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric, logger log.Logger) error {
globalStatusRows, err := db.QueryContext(ctx, globalStatusQuery)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion collector/global_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"testing"

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

ch := make(chan prometheus.Metric)
go func() {
if err = (ScrapeGlobalStatus{}).Scrape(context.Background(), db, ch); err != nil {
if err = (ScrapeGlobalStatus{}).Scrape(context.Background(), db, ch, log.NewNopLogger()); err != nil {
t.Errorf("error calling function on test: %s", err)
}
close(ch)
Expand Down
3 changes: 2 additions & 1 deletion collector/global_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strconv"
"strings"

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

Expand Down Expand Up @@ -137,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, db *sql.DB, ch chan<- prometheus.Metric) error {
func (ScrapeGlobalVariables) Scrape(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric, logger log.Logger) error {
globalVariablesRows, err := db.QueryContext(ctx, globalVariablesQuery)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion collector/global_variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"testing"

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

ch := make(chan prometheus.Metric)
go func() {
if err = (ScrapeGlobalVariables{}).Scrape(context.Background(), db, ch); err != nil {
if err = (ScrapeGlobalVariables{}).Scrape(context.Background(), db, ch, log.NewNopLogger()); err != nil {
t.Errorf("error calling function on test: %s", err)
}
close(ch)
Expand Down
3 changes: 2 additions & 1 deletion collector/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"strconv"

"github.com/go-kit/kit/log"
"github.com/prometheus/client_golang/prometheus"
"gopkg.in/alecthomas/kingpin.v2"
)
Expand Down Expand Up @@ -85,7 +86,7 @@ func (ScrapeHeartbeat) Version() float64 {
}

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

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

ch := make(chan prometheus.Metric)
go func() {
if err = (ScrapeHeartbeat{}).Scrape(context.Background(), db, ch); err != nil {
if err = (ScrapeHeartbeat{}).Scrape(context.Background(), db, ch, log.NewNopLogger()); err != nil {
t.Errorf("error calling function on test: %s", err)
}
close(ch)
Expand Down
3 changes: 2 additions & 1 deletion collector/info_schema_auto_increment.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"database/sql"

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

Expand Down Expand Up @@ -69,7 +70,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, db *sql.DB, ch chan<- prometheus.Metric) error {
func (ScrapeAutoIncrementColumns) Scrape(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric, logger log.Logger) error {
autoIncrementRows, err := db.QueryContext(ctx, infoSchemaAutoIncrementQuery)
if err != nil {
return err
Expand Down
9 changes: 5 additions & 4 deletions collector/info_schema_clientstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import (
"fmt"
"strings"

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

const clientStatQuery = `SELECT * FROM information_schema.client_statistics`
Expand Down Expand Up @@ -160,15 +161,15 @@ func (ScrapeClientStat) Version() float64 {
}

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

Expand Down
3 changes: 2 additions & 1 deletion collector/info_schema_clientstats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"testing"

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

ch := make(chan prometheus.Metric)
go func() {
if err = (ScrapeClientStat{}).Scrape(context.Background(), db, ch); err != nil {
if err = (ScrapeClientStat{}).Scrape(context.Background(), db, ch, log.NewNopLogger()); err != nil {
t.Errorf("error calling function on test: %s", err)
}
close(ch)
Expand Down
3 changes: 2 additions & 1 deletion collector/info_schema_innodb_cmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"database/sql"

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

Expand Down Expand Up @@ -76,7 +77,7 @@ func (ScrapeInnodbCmp) Version() float64 {
}

// Scrape collects data from database connection and sends it over channel as prometheus metric.
func (ScrapeInnodbCmp) Scrape(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric) error {
func (ScrapeInnodbCmp) Scrape(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric, logger log.Logger) error {
informationSchemaInnodbCmpRows, err := db.QueryContext(ctx, innodbCmpQuery)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion collector/info_schema_innodb_cmp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"testing"

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

ch := make(chan prometheus.Metric)
go func() {
if err = (ScrapeInnodbCmp{}).Scrape(context.Background(), db, ch); err != nil {
if err = (ScrapeInnodbCmp{}).Scrape(context.Background(), db, ch, log.NewNopLogger()); err != nil {
t.Errorf("error calling function on test: %s", err)
}
close(ch)
Expand Down
Loading

0 comments on commit 73160d9

Please sign in to comment.