Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move session params to DSN #259

Merged
merged 3 commits into from
Jan 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 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