avoid template execution errors on missing values (#41617)

pull/42476/head^2
Santiago 4 years ago committed by GitHub
parent 54ea2ed766
commit a21d1e50f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      pkg/services/ngalert/state/template.go
  2. 12
      pkg/services/ngalert/state/template_test.go

@ -54,7 +54,7 @@ func expandTemplate(name, text string, labels map[string]string, alertInstance e
return nil, nil
},
externalURL,
[]string{"missingkey=error"},
[]string{"missingkey=invalid"},
)
expander.Funcs(text_template.FuncMap{

@ -56,10 +56,10 @@ func TestExpandTemplate(t *testing.T) {
labels: data.Labels{"instance": "foo"},
expected: "foo is down",
}, {
name: "missing label in $labels returns error",
text: "{{ $labels.instance }} is down",
labels: data.Labels{},
expectedError: errors.New("error executing template __alert_test: template: __alert_test:1:86: executing \"__alert_test\" at <$labels.instance>: map has no entry for key \"instance\""),
name: "missing label in $labels returns <no value>",
text: "{{ $labels.instance }} is down",
labels: data.Labels{},
expected: "<no value> is down",
}, {
name: "values are expanded into $values",
text: "{{ $values.A.Labels.instance }} has value {{ $values.A }}",
@ -87,7 +87,7 @@ func TestExpandTemplate(t *testing.T) {
},
expected: "foo has value 1.1",
}, {
name: "missing label in $values returns error",
name: "missing label in $values returns <no value>",
text: "{{ $values.A.Labels.instance }} has value {{ $values.A }}",
alertInstance: eval.Result{
Values: map[string]eval.NumberValueCapture{
@ -98,7 +98,7 @@ func TestExpandTemplate(t *testing.T) {
},
},
},
expectedError: errors.New("error executing template __alert_test: template: __alert_test:1:86: executing \"__alert_test\" at <$values.A.Labels.instance>: map has no entry for key \"instance\""),
expected: "<no value> has value 1",
}, {
name: "missing value in $values is returned as NaN",
text: "{{ $values.A.Labels.instance }} has value {{ $values.A }}",

Loading…
Cancel
Save