Alerting: Support values in notification templates (#56457)

We have received a lot of feedback regarding the ValueString in alert notifications. Perhaps one of the most frequent complaints about ValueString is that it is difficult to read because it contains a lot of information, and the information is shown as a JSON-like string. Users have often asked how it can be templated and the answer is that it can't.

Until now users have been able to add custom annotations to their alert rules which contains values via the $values variable added in previous versions of Grafana. However, these custom annotations must be added for each of the user's alert rule, instead of once in a template that all of their alerts can be notified via.

This commit adds then the much requested feature to support values in notification templates. Users can then create a single template that prints the annotations, labels and values of their alerts in a format of their choice!
This commit is contained in:
George Robinson
2022-10-10 13:40:21 +01:00
committed by GitHub
parent 62674604b4
commit 802d67eeca
14 changed files with 165 additions and 76 deletions

View File

@@ -3,6 +3,7 @@ package state
import (
"context"
"fmt"
"math"
"net/url"
"strings"
"sync"
@@ -50,6 +51,15 @@ func (c *cache) getOrCreate(ctx context.Context, log log.Logger, alertRule *ngMo
func (rs *ruleStates) getOrCreate(ctx context.Context, log log.Logger, alertRule *ngModels.AlertRule, result eval.Result, extraLabels data.Labels, externalURL *url.URL) *State {
ruleLabels, annotations := rs.expandRuleLabelsAndAnnotations(ctx, log, alertRule, result, extraLabels, externalURL)
values := make(map[string]float64)
for _, v := range result.Values {
if v.Value != nil {
values[v.Var] = *v.Value
} else {
values[v.Var] = math.NaN()
}
}
lbs := make(data.Labels, len(extraLabels)+len(ruleLabels)+len(result.Instance))
dupes := make(data.Labels)
for key, val := range extraLabels {
@@ -102,6 +112,7 @@ func (rs *ruleStates) getOrCreate(ctx context.Context, log log.Logger, alertRule
}
}
state.Annotations = annotations
state.Values = values
rs.states[id] = state
return state
}
@@ -115,6 +126,7 @@ func (rs *ruleStates) getOrCreate(ctx context.Context, log log.Logger, alertRule
Labels: lbs,
Annotations: annotations,
EvaluationDuration: result.EvaluationDuration,
Values: values,
}
if result.State == eval.Alerting {
newState.StartsAt = result.EvaluatedAt