From 4374966987d4a2c31514db56a67e9a4458f83909 Mon Sep 17 00:00:00 2001 From: Yuri Tseretyan Date: Mon, 12 Dec 2022 10:12:30 -0500 Subject: [PATCH] Alerting: Replace hardcoded to [no value] in label expansion (#60129) * replace hardcoded to [no value] in label expansion --- pkg/services/ngalert/state/template.go | 11 ++++++++--- pkg/services/ngalert/state/template_test.go | 11 ++++++----- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/pkg/services/ngalert/state/template.go b/pkg/services/ngalert/state/template.go index 3169e1b8d62..b6e8bb11a00 100644 --- a/pkg/services/ngalert/state/template.go +++ b/pkg/services/ngalert/state/template.go @@ -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]") + return result, resultErr } func newTemplateCaptureValues(values map[string]eval.NumberValueCapture) map[string]templateCaptureValue { diff --git a/pkg/services/ngalert/state/template_test.go b/pkg/services/ngalert/state/template_test.go index 74ba9d64f79..398349b61c3 100644 --- a/pkg/services/ngalert/state/template_test.go +++ b/pkg/services/ngalert/state/template_test.go @@ -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 ", + name: "missing label in $labels returns [no value]", text: "{{ $labels.instance }} is down", labels: data.Labels{}, - expected: " 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 ", + 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: " 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 }}",