Alerting: Return errors when expanding templates (#63662)

This commit changes the state package so that errors encountered while
expanding templates for custom labels and annotations are returned
from the function. This is not used at present, but will be used in the
future as we look at how to offer better feedback to users who don't
have access to logs, for example our customers who use Hosted Grafana.
This commit is contained in:
George Robinson
2023-03-08 12:25:02 +00:00
committed by GitHub
parent 986a1c2a1b
commit 0c8876c3a2
4 changed files with 145 additions and 25 deletions

View File

@@ -2,6 +2,7 @@ package template
import (
"context"
"fmt"
"math"
"net/url"
"sort"
@@ -83,6 +84,17 @@ func NewData(labels map[string]string, res eval.Result) Data {
}
}
// ExpandError is an error containing the template and the error that occurred
// while expanding it.
type ExpandError struct {
Tmpl string
Err error
}
func (e ExpandError) Error() string {
return fmt.Sprintf("failed to expand template '%s': %s", e.Tmpl, e.Err)
}
func Expand(ctx context.Context, name, tmpl string, data Data, externalURL *url.URL, evaluatedAt time.Time) (string, error) {
// add __alert_ to avoid possible conflicts with other templates
name = "__alert_" + name
@@ -101,7 +113,7 @@ func Expand(ctx context.Context, name, tmpl string, data Data, externalURL *url.
result, err := expander.Expand()
if err != nil {
return "", err
return "", ExpandError{Tmpl: tmpl, Err: err}
}
// We need to replace <no value> with [no value] as some integrations think <no value> is invalid HTML. For example,

View File

@@ -66,6 +66,11 @@ func TestValueString(t *testing.T) {
}
}
func TestExpandError(t *testing.T) {
err := ExpandError{Tmpl: "{{", Err: errors.New("unexpected {{")}
assert.Equal(t, "failed to expand template '{{': unexpected {{", err.Error())
}
func TestExpandTemplate(t *testing.T) {
pathPrefix := "/path/prefix"
externalURL, err := url.Parse("http://localhost" + pathPrefix)
@@ -167,7 +172,7 @@ func TestExpandTemplate(t *testing.T) {
alertInstance: eval.Result{
EvaluationString: "invalid",
},
expectedError: errors.New(`error executing template __alert_test: template: __alert_test:1:79: executing "__alert_test" at <humanize $value>: error calling humanize: strconv.ParseFloat: parsing "invalid": invalid syntax`),
expectedError: errors.New(`failed to expand template '{{- $labels := .Labels -}}{{- $values := .Values -}}{{- $value := .Value -}}{{ humanize $value }}': error executing template __alert_test: template: __alert_test:1:79: executing "__alert_test" at <humanize $value>: error calling humanize: strconv.ParseFloat: parsing "invalid": invalid syntax`),
}, {
name: "humanize1024 float64",
text: "{{ range $key, $val := $values }}{{ humanize1024 .Value }}:{{ end }}",
@@ -202,7 +207,7 @@ func TestExpandTemplate(t *testing.T) {
alertInstance: eval.Result{
EvaluationString: "invalid",
},
expectedError: errors.New(`error executing template __alert_test: template: __alert_test:1:79: executing "__alert_test" at <humanize1024 $value>: error calling humanize1024: strconv.ParseFloat: parsing "invalid": invalid syntax`),
expectedError: errors.New(`failed to expand template '{{- $labels := .Labels -}}{{- $values := .Values -}}{{- $value := .Value -}}{{ humanize1024 $value }}': error executing template __alert_test: template: __alert_test:1:79: executing "__alert_test" at <humanize1024 $value>: error calling humanize1024: strconv.ParseFloat: parsing "invalid": invalid syntax`),
}, {
name: "humanizeDuration - seconds - float64",
text: "{{ range $key, $val := $values }}{{ humanizeDuration .Value }}:{{ end }}",
@@ -321,7 +326,7 @@ func TestExpandTemplate(t *testing.T) {
alertInstance: eval.Result{
EvaluationString: "invalid",
},
expectedError: errors.New(`error executing template __alert_test: template: __alert_test:1:79: executing "__alert_test" at <humanizeDuration $value>: error calling humanizeDuration: strconv.ParseFloat: parsing "invalid": invalid syntax`),
expectedError: errors.New(`failed to expand template '{{- $labels := .Labels -}}{{- $values := .Values -}}{{- $value := .Value -}}{{ humanizeDuration $value }}': error executing template __alert_test: template: __alert_test:1:79: executing "__alert_test" at <humanizeDuration $value>: error calling humanizeDuration: strconv.ParseFloat: parsing "invalid": invalid syntax`),
}, {
name: "humanizePercentage - float64",
text: "{{ -0.22222 | humanizePercentage }}:{{ 0.0 | humanizePercentage }}:{{ 0.1234567 | humanizePercentage }}:{{ 1.23456 | humanizePercentage }}",
@@ -333,7 +338,7 @@ func TestExpandTemplate(t *testing.T) {
}, {
name: "humanizePercentage - string with error",
text: `{{ "invalid" | humanizePercentage }}`,
expectedError: errors.New(`error executing template __alert_test: template: __alert_test:1:91: executing "__alert_test" at <humanizePercentage>: error calling humanizePercentage: strconv.ParseFloat: parsing "invalid": invalid syntax`),
expectedError: errors.New(`failed to expand template '{{- $labels := .Labels -}}{{- $values := .Values -}}{{- $value := .Value -}}{{ "invalid" | humanizePercentage }}': error executing template __alert_test: template: __alert_test:1:91: executing "__alert_test" at <humanizePercentage>: error calling humanizePercentage: strconv.ParseFloat: parsing "invalid": invalid syntax`),
}, {
name: "humanizeTimestamp - float64",
text: "{{ 1435065584.128 | humanizeTimestamp }}",