fix: add support for {{.Labels.<key>}} with dots in key for template (#6282)

This commit is contained in:
Srikanth Chekuri 2024-10-30 14:12:45 +05:30 committed by GitHub
parent cc90321ac0
commit 68d25a8989
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 5 deletions

View File

@ -234,15 +234,27 @@ func AlertTemplateData(labels map[string]string, value string, threshold string)
// If there is a go template block, it won't be replaced.
// The example for existing go template block is: {{$threshold}} or {{$value}} or any other valid go template syntax.
func (te *TemplateExpander) preprocessTemplate() {
re := regexp.MustCompile(`({{.*?}})|(\$(\w+(?:\.\w+)*))`)
te.text = re.ReplaceAllStringFunc(te.text, func(match string) string {
// Handle the $variable syntax
reDollar := regexp.MustCompile(`({{.*?}})|(\$(\w+(?:\.\w+)*))`)
te.text = reDollar.ReplaceAllStringFunc(te.text, func(match string) string {
if strings.HasPrefix(match, "{{") {
// If it's a Go template block, leave it unchanged
return match
}
// Otherwise, it's our custom $variable syntax
path := strings.Split(match[1:], ".")
return "{{index $labels \"" + strings.Join(path, ".") + "\"}}"
path := match[1:] // Remove the '$'
return fmt.Sprintf(`{{index $labels "%s"}}`, path)
})
// Handle the {{.Labels.service.name}} syntax
reLabels := regexp.MustCompile(`{{\s*\.Labels\.([a-zA-Z0-9_.]+)(.*?)}}`)
te.text = reLabels.ReplaceAllStringFunc(te.text, func(match string) string {
submatches := reLabels.FindStringSubmatch(match)
if len(submatches) < 3 {
return match // Should not happen
}
path := submatches[1]
rest := submatches[2]
return fmt.Sprintf(`{{index .Labels "%s"%s}}`, path, rest)
})
}

View File

@ -63,3 +63,14 @@ func TestTemplateExpander_WithMissingKey(t *testing.T) {
}
require.Equal(t, "test exceeds 100 and observed at 200", result)
}
func TestTemplateExpander_WithLablesDotSyntax(t *testing.T) {
defs := "{{$labels := .Labels}}{{$value := .Value}}{{$threshold := .Threshold}}"
data := AlertTemplateData(map[string]string{"service.name": "my-service"}, "200", "100")
expander := NewTemplateExpander(context.Background(), defs+"test {{.Labels.service.name}} exceeds {{$threshold}} and observed at {{$value}}", "test", data, times.Time(time.Now().Unix()), nil)
result, err := expander.Expand()
if err != nil {
t.Fatal(err)
}
require.Equal(t, "test my-service exceeds 100 and observed at 200", result)
}