Alerting: Change __value__ label to __value_string__ annotation and add ValueString variable in notifications (#36032)

* 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
This commit is contained in:
Kyle Brandt
2021-06-24 12:45:49 +05:30
committed by GitHub
parent fa46c877a9
commit 19f764739b
4 changed files with 16 additions and 6 deletions
+1 -1
View File
@@ -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) {
@@ -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, "__")) {
+5 -2
View File
@@ -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{
@@ -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",