Skip to content

Commit 677d38b

Browse files
authored
Merge pull request #521 from pvanderlinden/fix-auto-discovery
fix parsing include databases
2 parents cbc6ae3 + bc97291 commit 677d38b

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

Diff for: README.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ flag. This removes all built-in metrics, and uses only metrics defined by querie
192192
(so you must supply one, otherwise the exporter will return nothing but internal statuses and not your database).
193193

194194
### Automatically discover databases
195-
To scrape metrics from all databases on a database server, the database DSN's can be dynamically discovered via the
196-
`--auto-discover-databases` flag. When true, `SELECT datname FROM pg_database WHERE datallowconn = true AND datistemplate = false and datname != current_database()` is run for all configured DSN's. From the
195+
To scrape metrics from all databases on a database server, the database DSN's can be dynamically discovered via the
196+
`--auto-discover-databases` flag. When true, `SELECT datname FROM pg_database WHERE datallowconn = true AND datistemplate = false and datname != current_database()` is run for all configured DSN's. From the
197197
result a new set of DSN's is created for which the metrics are scraped.
198198

199199
In addition, the option `--exclude-databases` adds the possibily to filter the result from the auto discovery to discard databases you do not need.
@@ -285,3 +285,14 @@ GRANT SELECT ON postgres_exporter.pg_stat_statements TO postgres_exporter;
285285
> ```
286286
> DATA_SOURCE_NAME=postgresql://postgres_exporter:password@localhost:5432/postgres?sslmode=disable
287287
> ```
288+
289+
290+
## Running the tests
291+
```
292+
# Run the unit tests
293+
make test
294+
# Start the test database with docker
295+
docker run -p 5432:5432 -e POSTGRES_DB=circle_test -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=test -d postgres
296+
# Run the integration tests
297+
DATA_SOURCE_NAME='postgresql://postgres:test@localhost:5432/circle_test?sslmode=disable' GOOPTS='-v -tags integration' make test
298+
```

Diff for: cmd/postgres_exporter/postgres_exporter.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,9 @@ func ExcludeDatabases(s string) ExporterOpt {
11491149
// IncludeDatabases allows to filter result from AutoDiscoverDatabases
11501150
func IncludeDatabases(s string) ExporterOpt {
11511151
return func(e *Exporter) {
1152-
e.includeDatabases = strings.Split(s, ",")
1152+
if len(s) > 0 {
1153+
e.includeDatabases = strings.Split(s, ",")
1154+
}
11531155
}
11541156
}
11551157

Diff for: cmd/postgres_exporter/postgres_exporter_integration_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,16 @@ func (s *IntegrationSuite) TestExtendQueriesDoesntCrash(c *C) {
162162
// scrape the exporter and make sure it works
163163
exporter.scrape(ch)
164164
}
165+
166+
func (s *IntegrationSuite) TestAutoDiscoverDatabases(c *C) {
167+
dsn := os.Getenv("DATA_SOURCE_NAME")
168+
169+
exporter := NewExporter(
170+
strings.Split(dsn, ","),
171+
)
172+
c.Assert(exporter, NotNil)
173+
174+
dsns := exporter.discoverDatabaseDSNs()
175+
176+
c.Assert(len(dsns), Equals, 2)
177+
}

0 commit comments

Comments
 (0)