vmalert add quotes escape function (#510)

* vmalert add quotes escape function

Co-authored-by: kreedom
This commit is contained in:
kreedom 2020-05-20 22:20:31 +03:00 committed by Aliaksandr Valialkin
parent b26245c48b
commit 2752d6cb26
2 changed files with 8 additions and 5 deletions

View File

@ -52,15 +52,15 @@ func TestAlert_ExecTemplate(t *testing.T) {
{
name: "expression-template",
alert: &Alert{
Expr: "vm_rows>0",
Expr: `vm_rows{"label"="bar"}>0`,
},
annotations: map[string]string{
"exprEscapedQuery": "{{ $expr|queryEscape }}",
"exprEscapedPath": "{{ $expr|pathEscape }}",
"exprEscapedQuery": "{{ $expr|quotesEscape|queryEscape }}",
"exprEscapedPath": "{{ $expr|quotesEscape|pathEscape }}",
},
expTpl: map[string]string{
"exprEscapedQuery": "vm_rows%3E0",
"exprEscapedPath": "vm_rows%3E0",
"exprEscapedQuery": "vm_rows%7B%5C%22label%5C%22%3D%5C%22bar%5C%22%7D%3E0",
"exprEscapedPath": "vm_rows%7B%5C%22label%5C%22=%5C%22bar%5C%22%7D%3E0",
},
},
}

View File

@ -148,6 +148,9 @@ func InitTemplateFunc(externalURL *url.URL) {
"queryEscape": func(q string) string {
return url.QueryEscape(q)
},
"quotesEscape": func(q string) string {
return strings.Replace(q, `"`, `\"`, -1)
},
}
}