Skip to content

Commit

Permalink
Move session params to DSN (#259)
Browse files Browse the repository at this point in the history
* Move session params to DSN

In order to avoid lost session params if the `db` object reconnects in
the background, move session params to the DSN string configuration.

* Move log_slow_filter flag to exporter

* Update flag name to be in `exporter` namespace.
* Update README.

* Tweak exporter.log_slow_filter flag documentation.
  • Loading branch information
SuperQ authored Jan 30, 2018
1 parent 446651d commit 0b7dda1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 29 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ Name | Description
-------------------------------------------|--------------------------------------------------------------------------------------------------
config.my-cnf | Path to .my.cnf file to read MySQL credentials from. (default: `~/.my.cnf`)
log.level | Logging verbosity (default: info)
log_slow_filter | Add a log_slow_filter to avoid exessive MySQL slow logging. NOTE: Not supported by Oracle MySQL.
exporter.lock_wait_timeout | Set a lock_wait_timeout on the connection to avoid long metadata locking. (default: 2 seconds)
exporter.log_slow_filter | Add a log_slow_filter to avoid slow query logging of scrapes. NOTE: Not supported by Oracle MySQL.
web.listen-address | Address to listen on for web interface and telemetry.
web.telemetry-path | Path under which to expose metrics.
version | Print the version information.
Expand Down
47 changes: 24 additions & 23 deletions collector/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package collector
import (
"database/sql"
"fmt"
"strings"
"time"

_ "github.com/go-sql-driver/mysql"
Expand All @@ -19,9 +20,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 All @@ -30,6 +34,10 @@ var (
"exporter.lock_wait_timeout",
"Set the MySQL session lock_wait_timeout to avoid stuck metadata locks",
).Default("2").Int()
slowLogFilter = kingpin.Flag(
"exporter.log_slow_filter",
"Add a log_slow_filter to avoid exessive MySQL slow logging. NOTE: Not supported by Oracle MySQL.",
).Default("false").Bool()

scrapeDurationDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, exporter, "collector_duration_seconds"),
Expand All @@ -40,7 +48,6 @@ var (

// Collect defines which metrics we should collect
type Collect struct {
SlowLogFilter bool
Processlist bool
TableSchema bool
InnodbTablespaces bool
Expand Down Expand Up @@ -80,6 +87,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.
dsnParams := []string{fmt.Sprintf(timeoutParam, exporterLockTimeout)}

if *slowLogFilter {
dsnParams = append(dsnParams, 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 +195,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
5 changes: 0 additions & 5 deletions mysqld_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ var (
"config.my-cnf",
"Path to .my.cnf file to read MySQL credentials from.",
).Default(path.Join(os.Getenv("HOME"), ".my.cnf")).String()
slowLogFilter = kingpin.Flag(
"log_slow_filter",
"Add a log_slow_filter to avoid exessive MySQL slow logging. NOTE: Not supported by Oracle MySQL.",
).Default("false").Bool()
collectProcesslist = kingpin.Flag(
"collect.info_schema.processlist",
"Collect current thread state counts from the information_schema.processlist",
Expand Down Expand Up @@ -194,7 +190,6 @@ func handler(w http.ResponseWriter, r *http.Request) {
}

collect := collector.Collect{
SlowLogFilter: *slowLogFilter,
Processlist: filter(filters, "info_schema.processlist", *collectProcesslist),
TableSchema: filter(filters, "info_schema.tables", *collectTableSchema),
InnodbTablespaces: filter(filters, "info_schema.innodb_tablespaces", *collectInnodbTablespaces),
Expand Down

0 comments on commit 0b7dda1

Please sign in to comment.