Skip to content

Commit

Permalink
Skip scrapers for unsupported MySQL versions.
Browse files Browse the repository at this point in the history
Signed-off-by: Alexey Palazhchenko <alexey.palazhchenko@percona.com>
  • Loading branch information
AlekSi committed Sep 4, 2018
1 parent ad23dd2 commit 779976e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
30 changes: 28 additions & 2 deletions collector/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package collector
import (
"database/sql"
"fmt"
"regexp"
"strconv"
"strings"
"sync"
"time"
Expand All @@ -19,14 +21,20 @@ const (
exporter = "exporter"
)

// SQL Queries.
// SQL queries and parameters.
const (
versionQuery = `SELECT @@version`

// System variable params formatting.
// See: https://github.com/go-sql-driver/mysql#system-variables
sessionSettingsParam = `log_slow_filter=%27tmp_table_on_disk,filesort_on_disk%27`
timeoutParam = `lock_wait_timeout=%d`
)

var (
versionRE = regexp.MustCompile(`^\d+\.\d+`)
)

// Tunable flags.
var (
exporterLockTimeout = kingpin.Flag(
Expand Down Expand Up @@ -126,9 +134,14 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {

ch <- prometheus.MustNewConstMetric(scrapeDurationDesc, prometheus.GaugeValue, time.Since(scrapeTime).Seconds(), "connection")

wg := &sync.WaitGroup{}
version := getMySQLVersion(db)
var wg sync.WaitGroup
defer wg.Wait()
for _, scraper := range e.scrapers {
if version < scraper.Version() {
continue
}

wg.Add(1)
go func(scraper Scraper) {
defer wg.Done()
Expand All @@ -144,6 +157,19 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
}
}

func getMySQLVersion(db *sql.DB) float64 {
var versionStr string
var versionNum float64
if err := db.QueryRow(versionQuery).Scan(&versionStr); err == nil {
versionNum, _ = strconv.ParseFloat(versionRE.FindString(versionStr), 64)
}
// If we can't match/parse the version, set it some big value that matches all versions.
if versionNum == 0 {
versionNum = 999
}
return versionNum
}

// Metrics represents exporter metrics which values can be carried between http requests.
type Metrics struct {
TotalScrapes prometheus.Counter
Expand Down
15 changes: 15 additions & 0 deletions collector/exporter_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package collector

import (
"database/sql"
"testing"

"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -48,3 +49,17 @@ func TestExporter(t *testing.T) {
}
})
}

func TestGetMySQLVersion(t *testing.T) {
if testing.Short() {
t.Skip("-short is passed, skipping test")
}

convey.Convey("Version parsing", t, func() {
db, err := sql.Open("mysql", dsn)
convey.So(err, convey.ShouldBeNil)
defer db.Close()

convey.So(getMySQLVersion(db), convey.ShouldBeBetweenOrEqual, 5.5, 10.3)
})
}

0 comments on commit 779976e

Please sign in to comment.