Skip to content

Commit c51a28f

Browse files
authored
Merge pull request #499 from weastur/allow_databases_param
Add ability to set allow DBs list
2 parents 5c660ba + 2850ac8 commit c51a28f

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ This will build the docker image as `prometheuscommunity/postgres_exporter:${bra
7373
* `exclude-databases`
7474
A list of databases to remove when autoDiscoverDatabases is enabled.
7575

76+
* `include-databases`
77+
A list of databases to only include when autoDiscoverDatabases is enabled.
78+
7679
* `log.level`
7780
Set logging level: one of `debug`, `info`, `warn`, `error`.
7881

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

144+
* `PG_EXPORTER_INCLUDE_DATABASES`
145+
A comma-separated list of databases to only include when autoDiscoverDatabases is enabled. Default is empty string,
146+
means allow all.
147+
141148
* `PG_EXPORTER_METRIC_PREFIX`
142149
A prefix to use for each of the default metrics exported by postgres-exporter. Default is `pg`
143150

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

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

201+
If you want to include only subset of databases, you can use option `--include-databases`. Exporter still makes request to
202+
`pg_database` table, but do scrape from only if database is in include list.
203+
194204
### Running as non-superuser
195205

196206
To be able to collect metrics from `pg_stat_activity` and `pg_stat_replication`

cmd/postgres_exporter/postgres_exporter.go

+14
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ var (
5555
onlyDumpMaps = kingpin.Flag("dumpmaps", "Do not run, simply dump the maps.").Bool()
5656
constantLabelsList = kingpin.Flag("constantLabels", "A list of label=value separated by comma(,).").Default("").Envar("PG_EXPORTER_CONSTANT_LABELS").String()
5757
excludeDatabases = kingpin.Flag("exclude-databases", "A list of databases to remove when autoDiscoverDatabases is enabled").Default("").Envar("PG_EXPORTER_EXCLUDE_DATABASES").String()
58+
includeDatabases = kingpin.Flag("include-databases", "A list of databases to include when autoDiscoverDatabases is enabled").Default("").Envar("PG_EXPORTER_INCLUDE_DATABASES").String()
5859
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()
5960
logger = log.NewNopLogger()
6061
)
@@ -1099,6 +1100,7 @@ type Exporter struct {
10991100
disableDefaultMetrics, disableSettingsMetrics, autoDiscoverDatabases bool
11001101

11011102
excludeDatabases []string
1103+
includeDatabases []string
11021104
dsn []string
11031105
userQueriesPath string
11041106
constantLabels prometheus.Labels
@@ -1144,6 +1146,13 @@ func ExcludeDatabases(s string) ExporterOpt {
11441146
}
11451147
}
11461148

1149+
// IncludeDatabases allows to filter result from AutoDiscoverDatabases
1150+
func IncludeDatabases(s string) ExporterOpt {
1151+
return func(e *Exporter) {
1152+
e.includeDatabases = strings.Split(s, ",")
1153+
}
1154+
}
1155+
11471156
// WithUserQueriesPath configures user's queries path.
11481157
func WithUserQueriesPath(p string) ExporterOpt {
11491158
return func(e *Exporter) {
@@ -1678,6 +1687,10 @@ func (e *Exporter) discoverDatabaseDSNs() []string {
16781687
continue
16791688
}
16801689

1690+
if len(e.includeDatabases) != 0 && !contains(e.includeDatabases, databaseName) {
1691+
continue
1692+
}
1693+
16811694
if dsnURI != nil {
16821695
dsnURI.Path = databaseName
16831696
dsn = dsnURI.String()
@@ -1822,6 +1835,7 @@ func main() {
18221835
WithUserQueriesPath(*queriesPath),
18231836
WithConstantLabels(*constantLabelsList),
18241837
ExcludeDatabases(*excludeDatabases),
1838+
IncludeDatabases(*includeDatabases),
18251839
}
18261840

18271841
exporter := NewExporter(dsn, opts...)

0 commit comments

Comments
 (0)