Skip to content

Commit

Permalink
cleanup and README
Browse files Browse the repository at this point in the history
Signed-off-by: Joe Adams <[email protected]>

Co-authored-by: Ben Kochie <[email protected]>
  • Loading branch information
sysadmind and SuperQ committed Jul 28, 2022
1 parent d8faacf commit 21a623f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 27 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions cmd/postgres_exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
20 changes: 6 additions & 14 deletions cmd/postgres_exporter/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)

Expand Down
10 changes: 3 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down

0 comments on commit 21a623f

Please sign in to comment.