mirror of
https://github.com/grafana/grafana.git
synced 2026-07-30 08:18:10 -05:00
avoid template execution errors on missing values (#41617)
This commit is contained in:
@@ -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 }}",
|
||||
|
||||
Reference in New Issue
Block a user