diff --git a/config.go b/config.go index a3da8c51..30b9fdd6 100644 --- a/config.go +++ b/config.go @@ -17,6 +17,13 @@ import ( "gopkg.in/yaml.v2" ) +func getenv(key, defaultVal string) string { + if val, found := os.LookupEnv(key); found { + return val + } + return defaultVal +} + var ( failedScrapes = prometheus.NewGaugeVec( prometheus.GaugeOpts{ @@ -25,7 +32,9 @@ var ( }, []string{"driver", "host", "database", "user", "sql_job", "query"}, ) - reEnvironmentPlaceholders = regexp.MustCompile(`{{.+?}}`) + tmplStart = getenv("TEMPLATE_START", "{{") + tmplEnd = getenv("TEMPLATE_END", "}}") + reEnvironmentPlaceholders = regexp.MustCompile(fmt.Sprintf("%s.+?%s", tmplStart, tmplEnd)) ) func init() { @@ -49,10 +58,12 @@ func Read(path string) (File, error) { } placeholders := reEnvironmentPlaceholders.FindAllString(string(buf), -1) - replacer := strings.NewReplacer("{{", "", "}}", "") + replacer := strings.NewReplacer(tmplStart, "", tmplEnd, "") var replacements []string for _, placeholder := range placeholders { - environmentVariableName := strings.ToUpper(replacer.Replace(placeholder)) + environmentVariableName := strings.TrimSpace( + strings.ToUpper(replacer.Replace(placeholder)), + ) environmentVariableValue := os.Getenv(environmentVariableName) // We extracted a placeholder and found the value in the env variables to replace it with diff --git a/examples/kubernetes/configmap.yml b/examples/kubernetes/configmap.yml index e9e5c442..04232d8b 100644 --- a/examples/kubernetes/configmap.yml +++ b/examples/kubernetes/configmap.yml @@ -227,6 +227,7 @@ data: - 'postgres://postgres_exporter@postgres/db1?sslmode=disable' - 'postgres://postgres_exporter@postgres/db2?sslmode=disable' - 'postgres://postgres_exporter@postgres/db3?sslmode=disable' + - '[[ POSTGRES_CONNECTION_DB4 ]]' queries: - name: "pg_stat_user_tables" help: "Table stats" diff --git a/examples/kubernetes/deployment.yml b/examples/kubernetes/deployment.yml index b6762c14..bb3719f2 100644 --- a/examples/kubernetes/deployment.yml +++ b/examples/kubernetes/deployment.yml @@ -26,6 +26,12 @@ spec: value: /config/config.yml - name: PGPASSFILE value: /pgpass/pgpass + - name: TEMPLATE_START + value: '[[' + - name: TEMPLATE_END + value: ']]' + - name: POSTGRES_CONNECTION_DB4 + value: postgres://postgres_exporter@postgres/db4?sslmode=disable image: ghcr.io/justwatchcom/sql_exporter:latest livenessProbe: httpGet: