Skip to content

Commit

Permalink
Merge pull request #103 from AndrewChubatiuk/allow-override-placeholders
Browse files Browse the repository at this point in the history
allow override placeholders
  • Loading branch information
dewey committed Sep 28, 2023
2 parents 027fd64 + 8e4c5d3 commit b51b1ff
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
17 changes: 14 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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() {
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions examples/kubernetes/configmap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions examples/kubernetes/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit b51b1ff

Please sign in to comment.