From c8b92ec846c744e58fe9c66da1dd8a8245e981aa Mon Sep 17 00:00:00 2001 From: Alin Sinpalean Date: Mon, 14 May 2018 15:39:10 +0200 Subject: [PATCH] Add a config.data-source-name flag, to optionally override the config file value, as requested in issue #7. --- cmd/sql_exporter/main.go | 14 +++++++------- exporter.go | 12 ++++++++++++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/cmd/sql_exporter/main.go b/cmd/sql_exporter/main.go index 436224ad..096c3ea9 100644 --- a/cmd/sql_exporter/main.go +++ b/cmd/sql_exporter/main.go @@ -15,6 +15,13 @@ import ( _ "net/http/pprof" ) +var ( + showVersion = flag.Bool("version", false, "Print version information.") + listenAddress = flag.String("web.listen-address", ":9399", "Address to listen on for web interface and telemetry.") + metricsPath = flag.String("web.metrics-path", "/metrics", "Path under which to expose metrics.") + configFile = flag.String("config.file", "sql_exporter.yml", "SQL Exporter configuration file name.") +) + func init() { prometheus.MustRegister(version.NewCollector("sql_exporter")) } @@ -25,13 +32,6 @@ func main() { runtime.SetMutexProfileFraction(1) } - var ( - showVersion = flag.Bool("version", false, "Print version information.") - listenAddress = flag.String("web.listen-address", ":9399", "Address to listen on for web interface and telemetry.") - metricsPath = flag.String("web.metrics-path", "/metrics", "Path under which to expose metrics.") - configFile = flag.String("config.file", "sql_exporter.yml", "SQL Exporter configuration file name.") - ) - // Override --alsologtostderr default value. if alsoLogToStderr := flag.Lookup("alsologtostderr"); alsoLogToStderr != nil { alsoLogToStderr.DefValue = "true" diff --git a/exporter.go b/exporter.go index 6a43b2c5..6db1f484 100644 --- a/exporter.go +++ b/exporter.go @@ -2,6 +2,7 @@ package sql_exporter import ( "context" + "flag" "fmt" "sync" @@ -11,6 +12,8 @@ import ( dto "github.com/prometheus/client_model/go" ) +var dsnOverride = flag.String("config.data-source-name", "", "Data source name to override the value in the configuration file with.") + // Exporter is a prometheus.Gatherer that gathers SQL metrics from targets and merges them with the default registry. type Exporter interface { prometheus.Gatherer @@ -35,6 +38,15 @@ func NewExporter(configFile string) (Exporter, error) { return nil, err } + // Override the DSN if requested (and in single target mode). + if *dsnOverride != "" { + if len(c.Jobs) > 0 { + return nil, fmt.Errorf("The config.data-source-name flag (value %q) only applies in single target mode", *dsnOverride) + } else { + c.Target.DSN = config.Secret(*dsnOverride) + } + } + var targets []Target if c.Target != nil { target, err := NewTarget("", "", string(c.Target.DSN), c.Target.Collectors(), nil, c.Globals)