|
39 | 39 | queriesPath = kingpin.Flag("extend.query-path", "Path to custom queries to run.").Default("").Envar("PG_EXPORTER_EXTEND_QUERY_PATH").String() |
40 | 40 | onlyDumpMaps = kingpin.Flag("dumpmaps", "Do not run, simply dump the maps.").Bool() |
41 | 41 | constantLabelsList = kingpin.Flag("constantLabels", "A list of label=value separated by comma(,).").Default("").Envar("PG_EXPORTER_CONSTANT_LABELS").String() |
| 42 | + excludeDatabases = kingpin.Flag("exclude-databases", "A list of databases to remove when autoDiscoverDatabases is enabled").Default("").Envar("PG_EXPORTER_EXCLUDE_DATABASES").String() |
42 | 43 | ) |
43 | 44 |
|
44 | 45 | // Metric name parts. |
@@ -896,6 +897,7 @@ type Exporter struct { |
896 | 897 |
|
897 | 898 | disableDefaultMetrics, disableSettingsMetrics, autoDiscoverDatabases bool |
898 | 899 |
|
| 900 | + excludeDatabases []string |
899 | 901 | dsn []string |
900 | 902 | userQueriesPath string |
901 | 903 | constantLabels prometheus.Labels |
@@ -934,6 +936,13 @@ func AutoDiscoverDatabases(b bool) ExporterOpt { |
934 | 936 | } |
935 | 937 | } |
936 | 938 |
|
| 939 | +// ExcludeDatabases allows to filter out result from AutoDiscoverDatabases |
| 940 | +func ExcludeDatabases(s string) ExporterOpt { |
| 941 | + return func(e *Exporter) { |
| 942 | + e.excludeDatabases = strings.Split(s, ",") |
| 943 | + } |
| 944 | +} |
| 945 | + |
937 | 946 | // WithUserQueriesPath configures user's queries path. |
938 | 947 | func WithUserQueriesPath(p string) ExporterOpt { |
939 | 948 | return func(e *Exporter) { |
@@ -1352,6 +1361,9 @@ func (e *Exporter) discoverDatabaseDSNs() []string { |
1352 | 1361 | continue |
1353 | 1362 | } |
1354 | 1363 | for _, databaseName := range databaseNames { |
| 1364 | + if contains(e.excludeDatabases, databaseName) { |
| 1365 | + continue |
| 1366 | + } |
1355 | 1367 | parsedDSN.Path = databaseName |
1356 | 1368 | dsns[parsedDSN.String()] = struct{}{} |
1357 | 1369 | } |
@@ -1420,6 +1432,15 @@ func getDataSources() []string { |
1420 | 1432 | return strings.Split(dsn, ",") |
1421 | 1433 | } |
1422 | 1434 |
|
| 1435 | +func contains(a []string, x string) bool { |
| 1436 | + for _, n := range a { |
| 1437 | + if x == n { |
| 1438 | + return true |
| 1439 | + } |
| 1440 | + } |
| 1441 | + return false |
| 1442 | +} |
| 1443 | + |
1423 | 1444 | func main() { |
1424 | 1445 | kingpin.Version(fmt.Sprintf("postgres_exporter %s (built with %s)\n", Version, runtime.Version())) |
1425 | 1446 | log.AddFlags(kingpin.CommandLine) |
@@ -1452,6 +1473,7 @@ func main() { |
1452 | 1473 | AutoDiscoverDatabases(*autoDiscoverDatabases), |
1453 | 1474 | WithUserQueriesPath(*queriesPath), |
1454 | 1475 | WithConstantLabels(*constantLabelsList), |
| 1476 | + ExcludeDatabases(*excludeDatabases), |
1455 | 1477 | ) |
1456 | 1478 | defer func() { |
1457 | 1479 | exporter.servers.Close() |
|
0 commit comments