diff --git a/template/template.go b/template/template.go index 3c66bbd2b8..e06e26d82b 100644 --- a/template/template.go +++ b/template/template.go @@ -273,6 +273,7 @@ func (te Expander) Expand() (result string, resultErr error) { }() tmpl, err := text_template.New(te.name).Funcs(te.funcMap).Parse(te.text) + tmpl.Option("missingkey=zero") if err != nil { return "", fmt.Errorf("error parsing template %v: %v", te.name, err) } @@ -297,6 +298,7 @@ func (te Expander) ExpandHTML(templateFiles []string) (result string, resultErr }() tmpl := html_template.New(te.name).Funcs(html_template.FuncMap(te.funcMap)) + tmpl.Option("missingkey=zero") tmpl.Funcs(html_template.FuncMap{ "tmpl": func(name string, data interface{}) (html_template.HTML, error) { var buffer bytes.Buffer diff --git a/template/template_test.go b/template/template_test.go index 16f634f5ee..0dd46a4fdf 100644 --- a/template/template_test.go +++ b/template/template_test.go @@ -84,6 +84,21 @@ func TestTemplateExpansion(t *testing.T) { text: "{{ query \"metric{instance='a'}\" | first | label \"instance\" }}", output: "a", }, + { + // Missing label is empty when using label function. + text: "{{ query \"metric{instance='a'}\" | first | label \"foo\" }}", + output: "", + }, + { + // Missing label is empty when not using label function. + text: "{{ $x := query \"metric\" | first }}{{ $x.Labels.foo }}", + output: "", + }, + { + text: "{{ $x := query \"metric\" | first }}{{ $x.Labels.foo }}", + output: "", + html: true, + }, { // Range over query and sort by label. text: "{{ range query \"metric\" | sortByLabel \"instance\" }}{{.Labels.instance}}:{{.Value}}: {{end}}",