From 42471f1031930f750db5c38dd130e71ae71f8cce Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Thu, 24 Jun 2021 09:40:48 +0200 Subject: [PATCH] Alerting: Change `__value__` label to `__value_string__` annotation and add `ValueString` variable in notifications (#36032) (#36105) * Alerting: Allow __value__ label in notifications was being removed by removePrivateItems discoverd in #36020, but issue is not about that specifically * __value__ label to __value_string__ annotation and .ValueString extended property for notifications (cherry picked from commit 19f764739b2c02f9dc015ef7710ec2d597012417) Co-authored-by: Kyle Brandt --- pkg/services/ngalert/eval/eval.go | 2 +- pkg/services/ngalert/notifier/channels/template_data.go | 5 +++++ pkg/services/ngalert/schedule/compat.go | 7 +++++-- pkg/tests/api/alerting/api_notification_channel_test.go | 8 +++++--- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/pkg/services/ngalert/eval/eval.go b/pkg/services/ngalert/eval/eval.go index 37a2c2d22b7..475cd442153 100644 --- a/pkg/services/ngalert/eval/eval.go +++ b/pkg/services/ngalert/eval/eval.go @@ -166,7 +166,7 @@ func executeCondition(ctx AlertExecCtx, c *models.Condition, now time.Time, data return ExecutionResults{Error: err} } - // eval captures for the '__value__' label. + // eval captures for the '__value_string__' annotation and the Value property of the API response. captures := make([]NumberValueCapture, 0, len(execResp.Responses)) captureVal := func(refID string, labels data.Labels, value *float64) { diff --git a/pkg/services/ngalert/notifier/channels/template_data.go b/pkg/services/ngalert/notifier/channels/template_data.go index d4ef379a882..fca4ed65e05 100644 --- a/pkg/services/ngalert/notifier/channels/template_data.go +++ b/pkg/services/ngalert/notifier/channels/template_data.go @@ -29,6 +29,7 @@ type ExtendedAlert struct { SilenceURL string `json:"silenceURL"` DashboardURL string `json:"dashboardURL"` PanelURL string `json:"panelURL"` + ValueString string `json:"valueString"` } type ExtendedAlerts []ExtendedAlert @@ -87,6 +88,10 @@ func extendAlert(alert template.Alert, externalURL string, logger log.Logger) *E } } + if alert.Annotations != nil { + extended.ValueString = alert.Annotations[`__value_string__`] + } + matchers := make([]string, 0) for key, value := range alert.Labels { if !(strings.HasPrefix(key, "__") && strings.HasSuffix(key, "__")) { diff --git a/pkg/services/ngalert/schedule/compat.go b/pkg/services/ngalert/schedule/compat.go index a885c13b3c1..e78877ea036 100644 --- a/pkg/services/ngalert/schedule/compat.go +++ b/pkg/services/ngalert/schedule/compat.go @@ -7,6 +7,7 @@ import ( "time" "github.com/go-openapi/strfmt" + "github.com/grafana/grafana-plugin-sdk-go/data" apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions" "github.com/prometheus/alertmanager/api/v2/models" @@ -29,8 +30,10 @@ func FromAlertStateToPostableAlerts(logger log.Logger, firingStates []*state.Sta for _, alertState := range firingStates { if alertState.NeedsSending(stateManager.ResendDelay) { nL := alertState.Labels.Copy() + nA := data.Labels(alertState.Annotations).Copy() + if len(alertState.Results) > 0 { - nL["__value__"] = alertState.Results[0].EvaluationString + nA["__value_string__"] = alertState.Results[0].EvaluationString } genURL := appURL @@ -42,7 +45,7 @@ func FromAlertStateToPostableAlerts(logger log.Logger, firingStates []*state.Sta } alerts.PostableAlerts = append(alerts.PostableAlerts, models.PostableAlert{ - Annotations: alertState.Annotations, + Annotations: models.LabelSet(nA), StartsAt: strfmt.DateTime(alertState.StartsAt), EndsAt: strfmt.DateTime(alertState.EndsAt), Alert: models.Alert{ diff --git a/pkg/tests/api/alerting/api_notification_channel_test.go b/pkg/tests/api/alerting/api_notification_channel_test.go index 788aa672f6f..f3797970efa 100644 --- a/pkg/tests/api/alerting/api_notification_channel_test.go +++ b/pkg/tests/api/alerting/api_notification_channel_test.go @@ -1455,9 +1455,10 @@ var expNotifications = map[string][]string{ }, "annotations": {}, "startsAt": "%s", + "valueString": "[ var='A' labels={} value=1 ]", "endsAt": "0001-01-01T00:00:00Z", "generatorURL": "http://localhost:3000/alerting/UID_WebhookAlert/edit", - "fingerprint": "7611eef9e67f6e50", + "fingerprint": "929467973978d053", "silenceURL": "http://localhost:3000/alerting/silence/new?alertmanager=grafana&matchers=alertname%%3DWebhookAlert", "dashboardURL": "", "panelURL": "" @@ -1621,10 +1622,11 @@ var expNotifications = map[string][]string{ { "labels": { "__alert_rule_uid__": "UID_AlertmanagerAlert", - "__value__": "[ var='A' labels={} value=1 ]", "alertname": "AlertmanagerAlert" }, - "annotations": {}, + "annotations": { + "__value_string__": "[ var='A' labels={} value=1 ]" + }, "startsAt": "%s", "endsAt": "0001-01-01T00:00:00Z", "generatorURL": "http://localhost:3000/alerting/UID_AlertmanagerAlert/edit",