Skip to content

Commit

Permalink
Move session params to DSN
Browse files Browse the repository at this point in the history
In order to avoid lost session params if the `db` object reconnects in
the background, move session params to the DSN string configuration.
  • Loading branch information
SuperQ committed Jan 10, 2018
1 parent da3c16f commit 121694d
Showing 1 changed file with 19 additions and 22 deletions.
41 changes: 19 additions & 22 deletions collector/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ const (

// SQL Queries.
const (
sessionSettingsQuery = `SET SESSION log_slow_filter = 'tmp_table_on_disk,filesort_on_disk'`
upQuery = `SELECT 1`
timeoutQuery = `SET SESSION lock_wait_timeout = %d`
// 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`

upQuery = `SELECT 1`
)

// Metric descriptors.
Expand Down Expand Up @@ -80,6 +83,19 @@ type Exporter struct {

// New returns a new MySQL exporter for the provided DSN.
func New(dsn string, collect Collect) *Exporter {
// Setup extra params for the DSN, default to having a lock timeout.
dsnParms = []string{fmt.Sprintf(timeoutParam, exporterLockTimeout)}

if e.collect.SlowLogFilter {
dsnParms = append(dsnParms, sessionSettingsParam)
}

if strings.Contains(dsn, "?") {
dsn = dsn + "&" + strings.Join(dsnParams, "&")
} else {
dsn = dsn + "?" + strings.Join(dsnParams, "&")
}

return &Exporter{
dsn: dsn,
collect: collect,
Expand Down Expand Up @@ -175,27 +191,8 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
}
isUpRows.Close()

timeoutRows, err := db.Query(fmt.Sprintf(timeoutQuery, exporterLockTimeout))
if err != nil {
log.Errorln("Error setting timeout", err)
e.mysqldUp.Set(0)
e.error.Set(1)
return
}
timeoutRows.Close()

e.mysqldUp.Set(1)

if e.collect.SlowLogFilter {
sessionSettingsRows, err := db.Query(sessionSettingsQuery)
if err != nil {
log.Errorln("Error setting log_slow_filter:", err)
e.error.Set(1)
return
}
sessionSettingsRows.Close()
}

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

if e.collect.GlobalStatus {
Expand Down

0 comments on commit 121694d

Please sign in to comment.