Alerting: use hash of labels instead of labels string as the alert state cache key (#88956)

* Alerting: use hash instead of labels as the cache key
* Use data.Labels.Fingerprint to calculate the cache key
This commit is contained in:
Alexander Akhmetov
2024-06-11 18:34:58 +02:00
committed by GitHub
parent d004f8a98d
commit 667fea6623
11 changed files with 72 additions and 81 deletions
+1 -1
View File
@@ -90,7 +90,7 @@ func (e *Engine) Test(ctx context.Context, user identity.Requester, rule *models
start := time.Now()
tsField := data.NewField("Time", nil, make([]time.Time, length))
valueFields := make(map[string]*data.Field)
valueFields := make(map[data.Fingerprint]*data.Field)
err = evaluator.Eval(ruleCtx, from, time.Duration(rule.IntervalSeconds)*time.Second, length, func(idx int, currentTime time.Time, results eval.Results) error {
if idx >= length {
@@ -160,9 +160,10 @@ func TestNewBacktestingEvaluator(t *testing.T) {
func TestEvaluatorTest(t *testing.T) {
states := []eval.State{eval.Normal, eval.Alerting, eval.Pending}
generateState := func(prefix string) *state.State {
labels := models.GenerateAlertLabels(rand.Intn(5)+1, prefix+"-")
return &state.State{
CacheID: "state-" + prefix,
Labels: models.GenerateAlertLabels(rand.Intn(5)+1, prefix+"-"),
CacheID: labels.Fingerprint(),
Labels: labels,
State: states[rand.Intn(len(states))],
}
}
@@ -201,10 +202,11 @@ func TestEvaluatorTest(t *testing.T) {
var states []state.StateTransition
for _, s := range allStates {
labels := models.GenerateAlertLabels(rand.Intn(5)+1, s.String()+"-")
states = append(states, state.StateTransition{
State: &state.State{
CacheID: "state-" + s.String(),
Labels: models.GenerateAlertLabels(rand.Intn(5)+1, s.String()+"-"),
CacheID: labels.Fingerprint(),
Labels: labels,
State: s,
StateReason: util.GenerateShortUID(),
},
@@ -226,7 +228,7 @@ func TestEvaluatorTest(t *testing.T) {
require.Equal(t, data.FieldTypeTime, timestampField.Type())
})
fieldByState := make(map[string]*data.Field, len(states))
fieldByState := make(map[data.Fingerprint]*data.Field, len(states))
t.Run("should contain a field per state", func(t *testing.T) {
for _, s := range states {
@@ -269,11 +271,12 @@ func TestEvaluatorTest(t *testing.T) {
from := time.Unix(0, 0)
to := from.Add(5 * ruleInterval)
labels := models.GenerateAlertLabels(rand.Intn(5)+1, "test-")
states := []state.StateTransition{
{
State: &state.State{
CacheID: "state-1",
Labels: models.GenerateAlertLabels(rand.Intn(5)+1, "test-"),
CacheID: labels.Fingerprint(),
Labels: labels,
State: eval.Normal,
StateReason: util.GenerateShortUID(),
},