From c44e907a00404f28ee37034ead6b19e9d647fc60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Wenzel?= Date: Tue, 5 Oct 2021 17:08:28 +0200 Subject: [PATCH] Add support for DATA_SOURCE_NAME_FILE MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Björn Wenzel --- README.md | 3 +++ cmd/postgres_exporter/datasource.go | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/README.md b/README.md index fa247631..aea733b7 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,9 @@ The following environment variables configure the exporter: * `DATA_SOURCE_NAME` the default legacy format. Accepts URI form and key=value form arguments. The URI may contain the username and password to connect with. + +* `DATA_SOURCE_NAME_FILE` + The same as above but reads the URI from a file. * `DATA_SOURCE_URI` an alternative to `DATA_SOURCE_NAME` which exclusively accepts the hostname diff --git a/cmd/postgres_exporter/datasource.go b/cmd/postgres_exporter/datasource.go index 3bbe2f0a..8a143d3e 100644 --- a/cmd/postgres_exporter/datasource.go +++ b/cmd/postgres_exporter/datasource.go @@ -126,6 +126,16 @@ func getDataSources() ([]string, error) { return strings.Split(dsn, ","), nil } + dataSourceNameFile := os.Getenv("DATA_SOURCE_NAME_FILE") + if len(dataSourceNameFile) != 0 { + fileContents, err := ioutil.ReadFile(dataSourceNameFile) + if err != nil { + return nil, fmt.Errorf("failed loading data source name file %s: %s", dataSourceNameFile, err.Error()) + } + dsnContent := strings.TrimSpace(string(fileContents)) + return strings.Split(dsnContent, ","), nil + } + var user, pass, uri string dataSourceUserFile := os.Getenv("DATA_SOURCE_USER_FILE")