From f7b639ce32588d33aee8a15dcca6f088465cd0d0 Mon Sep 17 00:00:00 2001 From: Ben Kochie Date: Wed, 10 Jan 2018 16:53:10 +0100 Subject: [PATCH 1/3] 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. --- collector/exporter.go | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/collector/exporter.go b/collector/exporter.go index e34aa356..305d48b7 100644 --- a/collector/exporter.go +++ b/collector/exporter.go @@ -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. @@ -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. + dsnParams := []string{fmt.Sprintf(timeoutParam, exporterLockTimeout)} + + if e.collect.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, @@ -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 { From 2caad5bb518a45a3d6fca3ee5b4725bbb90561a3 Mon Sep 17 00:00:00 2001 From: Ben Kochie Date: Sat, 13 Jan 2018 15:29:48 +0100 Subject: [PATCH 2/3] Move log_slow_filter flag to exporter * Update flag name to be in `exporter` namespace. * Update README. --- README.md | 3 ++- collector/exporter.go | 8 ++++++-- mysqld_exporter.go | 5 ----- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 6ecd3438..fd385bfc 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,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 exessive MySQL slow logging. 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. diff --git a/collector/exporter.go b/collector/exporter.go index 305d48b7..f56d799b 100644 --- a/collector/exporter.go +++ b/collector/exporter.go @@ -3,6 +3,7 @@ package collector import ( "database/sql" "fmt" + "strings" "time" _ "github.com/go-sql-driver/mysql" @@ -33,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"), @@ -43,7 +48,6 @@ var ( // Collect defines which metrics we should collect type Collect struct { - SlowLogFilter bool Processlist bool TableSchema bool InnodbTablespaces bool @@ -86,7 +90,7 @@ 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 e.collect.SlowLogFilter { + if *slowLogFilter { dsnParams = append(dsnParams, sessionSettingsParam) } diff --git a/mysqld_exporter.go b/mysqld_exporter.go index dc965bae..c03355c0 100644 --- a/mysqld_exporter.go +++ b/mysqld_exporter.go @@ -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", @@ -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), From 6361361f797746374932ee365e270cc890d41a8d Mon Sep 17 00:00:00 2001 From: Ben Kochie Date: Mon, 22 Jan 2018 11:08:32 +0100 Subject: [PATCH 3/3] Tweak exporter.log_slow_filter flag documentation. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fd385bfc..35815a4c 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ Name | Description config.my-cnf | Path to .my.cnf file to read MySQL credentials from. (default: `~/.my.cnf`) log.level | Logging verbosity (default: info) 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 exessive MySQL slow logging. NOTE: Not supported by Oracle MySQL. +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.