mirror of
https://github.com/prometheus-community/postgres_exporter
synced 2025-04-08 02:01:23 +00:00
add support for secrets files
This commit is contained in:
parent
e701b80fd5
commit
528e347b48
@ -15,6 +15,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
|
|
||||||
@ -999,6 +1000,45 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// try to get the DataSource
|
||||||
|
// DATA_SOURCE_NAME always wins so we do not break older versions
|
||||||
|
// reading secrets from files wins over secrets in environment variables
|
||||||
|
// DATA_SOURCE_NAME > DATA_SOURCE_{USER|FILE}_FILE > DATA_SOURCE_{USER|FILE}
|
||||||
|
func getDataSource() string {
|
||||||
|
var dsn string
|
||||||
|
|
||||||
|
dsn = os.Getenv("DATA_SOURCE_NAME")
|
||||||
|
if len(dsn) == 0 {
|
||||||
|
var user string
|
||||||
|
var pass string
|
||||||
|
|
||||||
|
if(len(os.Getenv("DATA_SOURCE_USER_FILE")) != 0) {
|
||||||
|
fileContents, err := ioutil.ReadFile(os.Getenv("DATA_SOURCE_USER_FILE"))
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
user = strings.TrimSpace(string(fileContents))
|
||||||
|
} else {
|
||||||
|
user = os.Getenv("DATA_SOURCE_USER")
|
||||||
|
}
|
||||||
|
|
||||||
|
if(len(os.Getenv("DATA_SOURCE_PASS_FILE")) != 0) {
|
||||||
|
fileContents, err := ioutil.ReadFile(os.Getenv("DATA_SOURCE_PASS_FILE"))
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
pass = strings.TrimSpace(string(fileContents))
|
||||||
|
} else {
|
||||||
|
pass = os.Getenv("DATA_SOURCE_PASS")
|
||||||
|
}
|
||||||
|
|
||||||
|
uri := os.Getenv("DATA_SOURCE_URI")
|
||||||
|
dsn = "postgresql://" + user + ":" + pass + "@" + uri
|
||||||
|
}
|
||||||
|
|
||||||
|
return dsn
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
@ -1015,9 +1055,9 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
dsn := os.Getenv("DATA_SOURCE_NAME")
|
dsn := getDataSource()
|
||||||
if len(dsn) == 0 {
|
if len(dsn) == 0 {
|
||||||
log.Fatal("couldn't find environment variable DATA_SOURCE_NAME")
|
log.Fatal("couldn't find environment variables describing the datasource to use")
|
||||||
}
|
}
|
||||||
|
|
||||||
exporter := NewExporter(dsn, *queriesPath)
|
exporter := NewExporter(dsn, *queriesPath)
|
||||||
|
Loading…
Reference in New Issue
Block a user