From 21a623f1ba7f230897f0ebad85bea643d6602a76 Mon Sep 17 00:00:00 2001 From: Joe Adams Date: Tue, 28 Jun 2022 22:22:14 -0400 Subject: [PATCH] cleanup and README Signed-off-by: Joe Adams Co-authored-by: Ben Kochie --- README.md | 30 ++++++++++++++++++++++++++++++ cmd/postgres_exporter/main.go | 6 ------ cmd/postgres_exporter/probe.go | 20 ++++++-------------- config/config.go | 10 +++------- 4 files changed, 39 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index bb7c90d46..8c0b6dcba 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,36 @@ docker run \ quay.io/prometheuscommunity/postgres-exporter ``` +## Multi-Target Support (BETA) +**This Feature is in beta and may require changes in future releases. Feedback is welcome.** + +This exporter supports the [multi-target pattern](https://prometheus.io/docs/guides/multi-target-exporter/). This allows running a single instance of this exporter for multiple postgres targets. Using the milti-target funcationality of this exporter is **optional** and meant for users where it is impossible to install the exporter as a sidecar. For example SaaS-managed services. + +To use the multi-target functionality, send an http request to the endpoint `/probe?target=foo:5432` where target is set to the DSN of the postgres instance to scrape metrics from. + +To avoid putting sensitive information like username and password in the URL, preconfigured auth modules are supported via the [auth_modules](#auth_modules) section of the config file. auth_modules for DSNs can be used with the `/probe` endpoint by specifying the `?auth_module=foo` http parameter. + +## Configuration File + +The configuration file controls the behavior of the exporter. It can be set using the `--config.file` command line flag and defaults to `postres_exporter.yml`. + +### auth_modules +This section defines preset authentication and connection parameters for use in the [multi-target endpoint](#multi-target-support-beta). `auth_modules` is a map of modules with the key being the identifier which can be used in the `/probe` endpoint. +Currently only the `userpass` type is supported. + +Example: +```yaml +auth_modules: + foo1: # Set this to any name you want + type: userpass + userpass: + username: first + password: firstpass + options: + # options become key=value parameters of the DSN + sslmode: disable +``` + ## Building and running git clone https://github.com/prometheus-community/postgres_exporter.git diff --git a/cmd/postgres_exporter/main.go b/cmd/postgres_exporter/main.go index aee325039..d8f612956 100644 --- a/cmd/postgres_exporter/main.go +++ b/cmd/postgres_exporter/main.go @@ -102,12 +102,6 @@ func main() { os.Exit(1) } - // TODO(@sysadmind): Remove this with multi-target support - // if len(dsn) == 0 { - // level.Error(logger).Log("msg", "Couldn't find environment variables describing the datasource to use") - // os.Exit(1) - // } - opts := []ExporterOpt{ DisableDefaultMetrics(*disableDefaultMetrics), DisableSettingsMetrics(*disableSettingsMetrics), diff --git a/cmd/postgres_exporter/probe.go b/cmd/postgres_exporter/probe.go index 813f4ea81..7b2154319 100644 --- a/cmd/postgres_exporter/probe.go +++ b/cmd/postgres_exporter/probe.go @@ -60,7 +60,7 @@ func handleProbe(logger log.Logger) http.HandlerFunc { return } - // TODO: Timeout + // TODO(@sysadmind): Timeout probeSuccessGauge := prometheus.NewGauge(prometheus.GaugeOpts{ Name: "probe_success", @@ -86,23 +86,15 @@ func handleProbe(logger log.Logger) http.HandlerFunc { http.Error(w, err.Error(), http.StatusInternalServerError) return } - _ = ctx - // TODO: Which way should this be? Register or handle the collection manually? - // Also, what about the context? + // TODO(@sysadmind): Remove the registry.MustRegister() call below and instead handle the collection here. That will allow + // for the passing of context, handling of timeouts, and more control over the collection. + // The current NewProbeCollector() implementation relies on the MustNewConstMetric() call to create the metrics which is not + // ideal to use without the registry.MustRegister() call. + _ = ctx - // Option 1: Register the collector registry.MustRegister(pc) - // Option 2: Handle the collection manually. This allows us to collect duration metrics. - // The collectors themselves already support their own duration metrics. - // err = pc.Update(ctx) - // if err != nil { - // probeSuccessGauge.Set(0) - // } else { - // probeSuccessGauge.Set(1) - // } - duration := time.Since(start).Seconds() probeDurationGauge.Set(duration) diff --git a/config/config.go b/config/config.go index 49a2dbd63..10e7b7337 100644 --- a/config/config.go +++ b/config/config.go @@ -22,28 +22,24 @@ import ( "github.com/go-kit/log" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" "gopkg.in/yaml.v3" ) var ( - configReloadSuccess = prometheus.NewGauge(prometheus.GaugeOpts{ + configReloadSuccess = promauto.NewGauge(prometheus.GaugeOpts{ Namespace: "postgres_exporter", Name: "config_last_reload_successful", Help: "Postgres exporter config loaded successfully.", }) - configReloadSeconds = prometheus.NewGauge(prometheus.GaugeOpts{ + configReloadSeconds = promauto.NewGauge(prometheus.GaugeOpts{ Namespace: "postgres_exporter", Name: "config_last_reload_success_timestamp_seconds", Help: "Timestamp of the last successful configuration reload.", }) ) -func init() { - prometheus.MustRegister(configReloadSuccess) - prometheus.MustRegister(configReloadSeconds) -} - type Config struct { AuthModules map[string]AuthModule `yaml:"auth_modules"` }