Skip to content

Add ability to set allow DBs list #499

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

Merged
merged 1 commit into from
Mar 11, 2021
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ This will build the docker image as `prometheuscommunity/postgres_exporter:${bra
* `exclude-databases`
A list of databases to remove when autoDiscoverDatabases is enabled.

* `include-databases`
A list of databases to only include when autoDiscoverDatabases is enabled.

* `log.level`
Set logging level: one of `debug`, `info`, `warn`, `error`.

Expand Down Expand Up @@ -138,6 +141,10 @@ The following environment variables configure the exporter:
* `PG_EXPORTER_EXCLUDE_DATABASES`
A comma-separated list of databases to remove when autoDiscoverDatabases is enabled. Default is empty string.

* `PG_EXPORTER_INCLUDE_DATABASES`
A comma-separated list of databases to only include when autoDiscoverDatabases is enabled. Default is empty string,
means allow all.

* `PG_EXPORTER_METRIC_PREFIX`
A prefix to use for each of the default metrics exported by postgres-exporter. Default is `pg`

Expand Down Expand Up @@ -191,6 +198,9 @@ result a new set of DSN's is created for which the metrics are scraped.

In addition, the option `--exclude-databases` adds the possibily to filter the result from the auto discovery to discard databases you do not need.

If you want to include only subset of databases, you can use option `--include-databases`. Exporter still makes request to
`pg_database` table, but do scrape from only if database is in include list.

### Running as non-superuser

To be able to collect metrics from `pg_stat_activity` and `pg_stat_replication`
Expand Down
14 changes: 14 additions & 0 deletions cmd/postgres_exporter/postgres_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ var (
onlyDumpMaps = kingpin.Flag("dumpmaps", "Do not run, simply dump the maps.").Bool()
constantLabelsList = kingpin.Flag("constantLabels", "A list of label=value separated by comma(,).").Default("").Envar("PG_EXPORTER_CONSTANT_LABELS").String()
excludeDatabases = kingpin.Flag("exclude-databases", "A list of databases to remove when autoDiscoverDatabases is enabled").Default("").Envar("PG_EXPORTER_EXCLUDE_DATABASES").String()
includeDatabases = kingpin.Flag("include-databases", "A list of databases to include when autoDiscoverDatabases is enabled").Default("").Envar("PG_EXPORTER_INCLUDE_DATABASES").String()
metricPrefix = kingpin.Flag("metric-prefix", "A metric prefix can be used to have non-default (not \"pg\") prefixes for each of the metrics").Default("pg").Envar("PG_EXPORTER_METRIC_PREFIX").String()
logger = log.NewNopLogger()
)
Expand Down Expand Up @@ -1099,6 +1100,7 @@ type Exporter struct {
disableDefaultMetrics, disableSettingsMetrics, autoDiscoverDatabases bool

excludeDatabases []string
includeDatabases []string
dsn []string
userQueriesPath string
constantLabels prometheus.Labels
Expand Down Expand Up @@ -1144,6 +1146,13 @@ func ExcludeDatabases(s string) ExporterOpt {
}
}

// IncludeDatabases allows to filter result from AutoDiscoverDatabases
func IncludeDatabases(s string) ExporterOpt {
return func(e *Exporter) {
e.includeDatabases = strings.Split(s, ",")
}
}

// WithUserQueriesPath configures user's queries path.
func WithUserQueriesPath(p string) ExporterOpt {
return func(e *Exporter) {
Expand Down Expand Up @@ -1678,6 +1687,10 @@ func (e *Exporter) discoverDatabaseDSNs() []string {
continue
}

if len(e.includeDatabases) != 0 && !contains(e.includeDatabases, databaseName) {
continue
}

if dsnURI != nil {
dsnURI.Path = databaseName
dsn = dsnURI.String()
Expand Down Expand Up @@ -1822,6 +1835,7 @@ func main() {
WithUserQueriesPath(*queriesPath),
WithConstantLabels(*constantLabelsList),
ExcludeDatabases(*excludeDatabases),
IncludeDatabases(*includeDatabases),
}

exporter := NewExporter(dsn, opts...)
Expand Down