Repository: VictoriaMetrics/VictoriaMetrics
Pull request #4009 (2023-03-24 22:21:19)

allowed using dashes and dots in environment variables names
allowed using dashes and dots in environment variables names for templating config files with envtemplate (#3999)


codecov[bot]: ## [Codecov](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/4009?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VictoriaMetrics) Report
Patch coverage has no change and project coverage change: **`-0.02`** :warning:
> Comparison is base [(`365d2ff`)](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/commit/365d2ff0bfa2cffdfcf0be9d224715023f847d4e?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VictoriaMetrics) 58.55% compared to head [(`3a97d15`)](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/4009?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VictoriaMetrics) 58.53%.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master    #4009      +/-   ##
==========================================
- Coverage   58.55%   58.53%   -0.02%     
==========================================
  Files         315      315              
  Lines       52606    52606              
==========================================
- Hits        30801    30794       -7     
- Misses      19892    19899       +7     
  Partials     1913     1913              
```


| [Impacted Files](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/4009?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VictoriaMetrics) | Coverage Δ | |
|---|---|---|
| [lib/envtemplate/envtemplate.go](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/4009?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VictoriaMetrics#diff-bGliL2VudnRlbXBsYXRlL2VudnRlbXBsYXRlLmdv) | `94.20% <ø> (ø)` | |

... and [6 files with indirect coverage changes](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/4009/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VictoriaMetrics)

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VictoriaMetrics). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VictoriaMetrics)

</details>

[:umbrella: View full report in Codecov by Sentry](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/4009?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VictoriaMetrics).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VictoriaMetrics).


Patch:
diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md
index 462d6aedb..41841c989 100644
--- a/docs/CHANGELOG.md
+++ b/docs/CHANGELOG.md
@@ -35,6 +35,7 @@ created by v1.90.0 or newer versions. The solution is to upgrade to v1.90.0 or n
 * BUGFIX: prevent from slow [snapshot creating](https://docs.victoriametrics.com/#how-to-work-with-snapshots) under high data ingestion rate. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3551).
 * BUGFIX: [vmauth](https://docs.victoriametrics.com/vmauth.html):  suppress [proxy protocol](https://www.haproxy.org/download/2.3/doc/proxy-protocol.txt) parsing errors in case of `EOF`. Usually, the error is caused by health checks and is not a sign of an actual error.
 * BUGFIX: [vmui](https://docs.victoriametrics.com/#vmui): fix displaying errors for each query. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3987).
+* BUGFIX: allow using dashes and dots in environment variables names referred in config files via `%{ENV-VAR.SYNTAX}`. See [these docs](https://docs.victoriametrics.com/#environment-variables) and [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3999).
 
 ## [v1.89.1](https://github.com/VictoriaMetrics/VictoriaMetrics/releases/tag/v1.89.1)
 
diff --git a/lib/envtemplate/envtemplate.go b/lib/envtemplate/envtemplate.go
index 6ad779d63..393cc4426 100644
--- a/lib/envtemplate/envtemplate.go
+++ b/lib/envtemplate/envtemplate.go
@@ -113,4 +113,7 @@ func isValidEnvVarName(s string) bool {
 	return envVarNameRegex.MatchString(s)
 }
 
-var envVarNameRegex = regexp.MustCompile("^[a-zA-Z0-9_]+$")
+// envVarNameRegex is used for validating environment variable names.
+//
+// Allow dashes and dots in env var names - see https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3999
+var envVarNameRegex = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_\-.]*$`)


Test patch:
diff --git a/lib/envtemplate/envtemplate_test.go b/lib/envtemplate/envtemplate_test.go
index cdd7b9ab3..420d67960 100644
--- a/lib/envtemplate/envtemplate_test.go
+++ b/lib/envtemplate/envtemplate_test.go
@@ -24,7 +24,9 @@ func TestExpandTemplates(t *testing.T) {
 	f([]string{"foo=%{bar}", "bar=x"}, []string{"bar=x", "foo=x"})
 	f([]string{"a=x%{b}", "b=y%{c}z%{d}", "c=123", "d=qwe"}, []string{"a=xy123zqwe", "b=y123zqwe", "c=123", "d=qwe"})
 	f([]string{"a=x%{b}y", "b=z%{a}q", "c"}, []string{"a=xzxzxzxz%{a}qyqyqyqy", "b=zxzxzxzx%{b}yqyqyqyq", "c="})
-	f([]string{"a=%{x.y}"}, []string{"a=%{x.y}"})
+	f([]string{"a=%{x.y}", "x.y=test"}, []string{"a=test", "x.y=test"})
+	f([]string{"a=%{x y}"}, []string{"a=%{x y}"})
+	f([]string{"a=%{123}"}, []string{"a=%{123}"})
 }
 
 func TestLookupEnv(t *testing.T) {
@@ -49,7 +51,9 @@ func TestLookupEnv(t *testing.T) {
 
 func TestReplaceSuccess(t *testing.T) {
 	envVars = map[string]string{
-		"foo": "bar",
+		"foo":       "bar",
+		"foo.bar_1": "baz",
+		"foo-bar_2": "test",
 	}
 	f := func(s, resultExpected string) {
 		t.Helper()
@@ -71,7 +75,8 @@ func TestReplaceSuccess(t *testing.T) {
 	f("", "")
 	f("foo", "foo")
 	f("a %{foo}-x", "a bar-x")
-	f("%{foo.bar}", "%{foo.bar}")
+	f("%{foo.bar_1}", "baz")
+	f("qq.%{foo-bar_2}.ww", "qq.test.ww")
 }
 
 func TestReplaceFailure(t *testing.T) {
@@ -85,4 +90,7 @@ func TestReplaceFailure(t *testing.T) {
 		}
 	}
 	f("foo %{bar} %{baz}")
+	f("%{Foo_Foo_1}")
+	f("%{Foo-Bar-2}")
+	f("%{Foo.Baz.3}")
 }