Add support for 'DATA_SOURCE_URI_FILE' envvar.

Closes #326 as is provides a viable solution to use a K8S init container
to fully contruct the PostgreSQL URI and 'hand it over' to the postgres_exporter
process.
This commit is contained in:
Benjamin P. Jung 2019-11-05 13:31:15 +01:00 committed by Will Rouesnel
parent 852ec5d9a1
commit 9b13f5ec57
2 changed files with 15 additions and 1 deletions

View File

@ -85,6 +85,9 @@ The following environment variables configure the exporter:
an alternative to `DATA_SOURCE_NAME` which exclusively accepts the raw URI
without a username and password component.
* `DATA_SOURCE_URI_FILE`
The same as above but reads the URI from a file.
* `DATA_SOURCE_USER`
When using `DATA_SOURCE_URI`, this environment variable is used to specify
the username.

View File

@ -1488,6 +1488,7 @@ func getDataSources() []string {
if len(dsn) == 0 {
var user string
var pass string
var uri string
if len(os.Getenv("DATA_SOURCE_USER_FILE")) != 0 {
fileContents, err := ioutil.ReadFile(os.Getenv("DATA_SOURCE_USER_FILE"))
@ -1510,7 +1511,17 @@ func getDataSources() []string {
}
ui := url.UserPassword(user, pass).String()
uri := os.Getenv("DATA_SOURCE_URI")
if len(os.Getenv("DATA_SOURCE_URI_FILE")) != 0 {
fileContents, err := ioutil.ReadFile(os.Getenv("DATA_SOURCE_URI_FILE"))
if err != nil {
panic(err)
}
uri = strings.TrimSpace(string(fileContents))
} else {
uri = os.Getenv("DATA_SOURCE_URI")
}
dsn = "postgresql://" + ui + "@" + uri
return []string{dsn}