Alerting: Replace hardcoded <no value> to [no value] in label expansion (#60129)

* replace hardcoded <no value> to [no value] in label expansion
This commit is contained in:
Yuri Tseretyan 2022-12-12 10:12:30 -05:00 committed by GitHub
parent 3172d8dd9b
commit 4374966987
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 8 deletions

View File

@ -7,15 +7,17 @@ import (
"math"
"net/url"
"strconv"
"strings"
"time"
text_template "text/template"
"github.com/grafana/grafana/pkg/services/ngalert/eval"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/pkg/timestamp"
"github.com/prometheus/prometheus/promql"
"github.com/prometheus/prometheus/template"
"github.com/grafana/grafana/pkg/services/ngalert/eval"
)
// templateCaptureValue represents each value in .Values in the annotations
@ -66,8 +68,11 @@ func expandTemplate(ctx context.Context, name, text string, labels map[string]st
return ""
},
})
return expander.Expand()
result, resultErr = expander.Expand()
// Replace missing key value to one that does not look like an HTML tag. This can cause problems downstream in some notifiers.
// For example, Telegram in HTML mode rejects requests with unsupported tags.
result = strings.ReplaceAll(result, "<no value>", "[no value]")
return result, resultErr
}
func newTemplateCaptureValues(values map[string]eval.NumberValueCapture) map[string]templateCaptureValue {

View File

@ -7,10 +7,11 @@ import (
"testing"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana/pkg/services/ngalert/eval"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
ptr "github.com/xorcare/pointer"
"github.com/grafana/grafana/pkg/services/ngalert/eval"
)
func TestTemplateCaptureValueStringer(t *testing.T) {
@ -57,10 +58,10 @@ func TestExpandTemplate(t *testing.T) {
labels: data.Labels{"instance": "foo"},
expected: "foo is down",
}, {
name: "missing label in $labels returns <no value>",
name: "missing label in $labels returns [no value]",
text: "{{ $labels.instance }} is down",
labels: data.Labels{},
expected: "<no value> is down",
expected: "[no value] is down",
}, {
name: "values are expanded into $values",
text: "{{ $values.A.Labels.instance }} has value {{ $values.A }}",
@ -88,7 +89,7 @@ func TestExpandTemplate(t *testing.T) {
},
expected: "foo has value 1.1",
}, {
name: "missing label in $values returns <no value>",
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{
@ -99,7 +100,7 @@ func TestExpandTemplate(t *testing.T) {
},
},
},
expected: "<no value> has value 1",
expected: "[no value] has value 1",
}, {
name: "missing value in $values is returned as NaN",
text: "{{ $values.A.Labels.instance }} has value {{ $values.A }}",