mirror of
https://git.mirrors.martin98.com/https://github.com/SigNoz/signoz
synced 2025-09-13 17:33:16 +08:00
fix: add support for {{.Labels.<key>}} with dots in key
for template (#6282)
This commit is contained in:
parent
cc90321ac0
commit
68d25a8989
@ -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.
|
// 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.
|
// The example for existing go template block is: {{$threshold}} or {{$value}} or any other valid go template syntax.
|
||||||
func (te *TemplateExpander) preprocessTemplate() {
|
func (te *TemplateExpander) preprocessTemplate() {
|
||||||
re := regexp.MustCompile(`({{.*?}})|(\$(\w+(?:\.\w+)*))`)
|
// Handle the $variable syntax
|
||||||
te.text = re.ReplaceAllStringFunc(te.text, func(match string) string {
|
reDollar := regexp.MustCompile(`({{.*?}})|(\$(\w+(?:\.\w+)*))`)
|
||||||
|
te.text = reDollar.ReplaceAllStringFunc(te.text, func(match string) string {
|
||||||
if strings.HasPrefix(match, "{{") {
|
if strings.HasPrefix(match, "{{") {
|
||||||
// If it's a Go template block, leave it unchanged
|
// If it's a Go template block, leave it unchanged
|
||||||
return match
|
return match
|
||||||
}
|
}
|
||||||
// Otherwise, it's our custom $variable syntax
|
path := match[1:] // Remove the '$'
|
||||||
path := strings.Split(match[1:], ".")
|
return fmt.Sprintf(`{{index $labels "%s"}}`, path)
|
||||||
return "{{index $labels \"" + strings.Join(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)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,3 +63,14 @@ func TestTemplateExpander_WithMissingKey(t *testing.T) {
|
|||||||
}
|
}
|
||||||
require.Equal(t, "test exceeds 100 and observed at 200", result)
|
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)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user