From b2a51cb5011ed86a93b03cb7254cb172668889ba Mon Sep 17 00:00:00 2001 From: Andrew Chubatiuk Date: Wed, 27 Sep 2023 17:13:08 +0300 Subject: [PATCH 1/3] allow override placeholders --- config.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/config.go b/config.go index a3da8c51..497b178d 100644 --- a/config.go +++ b/config.go @@ -17,6 +17,14 @@ import ( "gopkg.in/yaml.v2" ) +func getenv(key, defaultVal string) string { + if val, found := os.LookupEnv(key); found { + return val + } else { + return defaultVal + } +} + var ( failedScrapes = prometheus.NewGaugeVec( prometheus.GaugeOpts{ @@ -25,7 +33,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,7 +59,7 @@ 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)) From 5c15918417d181681a51d76b445fab4e6c8a209b Mon Sep 17 00:00:00 2001 From: Andrew Chubatiuk Date: Wed, 27 Sep 2023 20:26:31 +0300 Subject: [PATCH 2/3] pr comments --- config.go | 3 +-- examples/kubernetes/configmap.yml | 1 + examples/kubernetes/deployment.yml | 6 ++++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/config.go b/config.go index 497b178d..9d6aac15 100644 --- a/config.go +++ b/config.go @@ -20,9 +20,8 @@ import ( func getenv(key, defaultVal string) string { if val, found := os.LookupEnv(key); found { return val - } else { - return defaultVal } + return defaultVal } var ( 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: From 8e4c5d398c3aede2f408f459dc09cddee32ab934 Mon Sep 17 00:00:00 2001 From: Andrew Chubatiuk Date: Thu, 28 Sep 2023 11:55:21 +0300 Subject: [PATCH 3/3] trim env variables spaces --- config.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/config.go b/config.go index 9d6aac15..30b9fdd6 100644 --- a/config.go +++ b/config.go @@ -61,7 +61,9 @@ func Read(path string) (File, error) { 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