Skip to content

Commit

Permalink
Merge pull request #10 from free/issue_7_override_dsn_from_flag
Browse files Browse the repository at this point in the history
Add a config.data-source-name flag, as requested in issue #7
  • Loading branch information
free authored May 14, 2018
2 parents 107be18 + c8b92ec commit 2b52910
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
14 changes: 7 additions & 7 deletions cmd/sql_exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
Expand All @@ -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"
Expand Down
12 changes: 12 additions & 0 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sql_exporter

import (
"context"
"flag"
"fmt"
"sync"

Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 2b52910

Please sign in to comment.