Alerting: Update state manager to take image only once per rule evaluation (#98289)

* add test

* update state manager to take image only once per rule evaluation process execution

* update test
This commit is contained in:
Yuri Tseretyan
2025-01-09 12:57:58 -05:00
committed by GitHub
parent ee7ffb7a04
commit 4f62c8a160
3 changed files with 228 additions and 37 deletions
+32
View File
@@ -2,6 +2,8 @@ package state
import (
"context"
"fmt"
"math/rand"
"slices"
"sync"
@@ -107,3 +109,33 @@ func (s *NoopImageService) NewImage(_ context.Context, _ *models.AlertRule) (*mo
// NoopSender is a no-op sender. Used when you want state manager to update LastSentAt without sending any alerts.
var NoopSender = func(_ context.Context, _ StateTransitions) {}
type CountingImageService struct {
mtx sync.Mutex
Called int
Image *models.Image
Err error
}
func (c *CountingImageService) NewImage(_ context.Context, _ *models.AlertRule) (*models.Image, error) {
c.mtx.Lock()
defer c.mtx.Unlock()
c.Called += 1
return c.Image, c.Err
}
func newSuccessfulCountingImageService() *CountingImageService {
return &CountingImageService{
Called: 0,
Image: &models.Image{
Token: fmt.Sprint(rand.Int()),
},
}
}
func NewFailingCountingImageService(err error) *CountingImageService {
return &CountingImageService{
Called: 0,
Err: err,
}
}