2018-02-16 10:00:13 -06:00
|
|
|
package alerting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"math"
|
|
|
|
"testing"
|
2019-05-20 05:13:32 -05:00
|
|
|
"time"
|
|
|
|
|
2021-09-22 20:12:12 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2022-01-20 04:10:12 -06:00
|
|
|
"github.com/grafana/grafana/pkg/infra/tracing"
|
2021-09-22 20:12:12 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/usagestats"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2021-10-07 09:33:50 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/encryption/ossencryption"
|
2019-03-29 00:58:37 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2021-10-26 06:15:09 -05:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2018-02-16 10:00:13 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type FakeEvalHandler struct {
|
2018-04-13 11:40:14 -05:00
|
|
|
SuccessCallID int // 0 means never success
|
2018-02-16 10:00:13 -06:00
|
|
|
CallNb int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFakeEvalHandler(successCallID int) *FakeEvalHandler {
|
|
|
|
return &FakeEvalHandler{
|
|
|
|
SuccessCallID: successCallID,
|
|
|
|
CallNb: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (handler *FakeEvalHandler) Eval(evalContext *EvalContext) {
|
|
|
|
handler.CallNb++
|
|
|
|
if handler.CallNb != handler.SuccessCallID {
|
|
|
|
evalContext.Error = errors.New("Fake evaluation failure")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type FakeResultHandler struct{}
|
|
|
|
|
2019-05-20 05:13:32 -05:00
|
|
|
func (handler *FakeResultHandler) handle(evalContext *EvalContext) error {
|
2018-02-16 10:00:13 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-03 06:26:05 -06:00
|
|
|
// A mock implementation of the AlertStore interface, allowing to override certain methods individually
|
|
|
|
type AlertStoreMock struct {
|
|
|
|
getAllAlerts func(context.Context, *models.GetAllAlertsQuery) error
|
|
|
|
getDataSource func(context.Context, *models.GetDataSourceQuery) error
|
|
|
|
getAlertNotificationsWithUidToSend func(ctx context.Context, query *models.GetAlertNotificationsWithUidToSendQuery) error
|
|
|
|
getOrCreateNotificationState func(ctx context.Context, query *models.GetOrCreateNotificationStateQuery) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AlertStoreMock) GetDataSource(c context.Context, cmd *models.GetDataSourceQuery) error {
|
|
|
|
if a.getDataSource != nil {
|
|
|
|
return a.getDataSource(c, cmd)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AlertStoreMock) GetAllAlertQueryHandler(c context.Context, cmd *models.GetAllAlertsQuery) error {
|
|
|
|
if a.getAllAlerts != nil {
|
|
|
|
return a.getAllAlerts(c, cmd)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AlertStoreMock) GetAlertNotificationsWithUidToSend(c context.Context, cmd *models.GetAlertNotificationsWithUidToSendQuery) error {
|
|
|
|
if a.getAlertNotificationsWithUidToSend != nil {
|
|
|
|
return a.getAlertNotificationsWithUidToSend(c, cmd)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AlertStoreMock) GetOrCreateAlertNotificationState(c context.Context, cmd *models.GetOrCreateNotificationStateQuery) error {
|
|
|
|
if a.getOrCreateNotificationState != nil {
|
|
|
|
return a.getOrCreateNotificationState(c, cmd)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AlertStoreMock) GetDashboardUIDById(_ context.Context, _ *models.GetDashboardRefByIdQuery) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AlertStoreMock) SetAlertNotificationStateToCompleteCommand(_ context.Context, _ *models.SetAlertNotificationStateToCompleteCommand) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AlertStoreMock) SetAlertNotificationStateToPendingCommand(_ context.Context, _ *models.SetAlertNotificationStateToPendingCommand) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AlertStoreMock) SetAlertState(_ context.Context, _ *models.SetAlertStateCommand) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-16 10:00:13 -06:00
|
|
|
func TestEngineProcessJob(t *testing.T) {
|
2021-10-26 06:15:09 -05:00
|
|
|
bus := bus.New()
|
|
|
|
usMock := &usagestats.UsageStatsMock{T: t}
|
2022-01-20 04:10:12 -06:00
|
|
|
tracer, err := tracing.InitializeTracerForTest()
|
|
|
|
require.NoError(t, err)
|
2022-02-03 06:26:05 -06:00
|
|
|
|
|
|
|
store := &AlertStoreMock{}
|
2022-02-28 02:54:56 -06:00
|
|
|
engine := ProvideAlertEngine(nil, bus, nil, nil, usMock, ossencryption.ProvideService(), nil, tracer, store, setting.NewCfg(), nil)
|
2021-10-26 06:15:09 -05:00
|
|
|
setting.AlertingEvaluationTimeout = 30 * time.Second
|
|
|
|
setting.AlertingNotificationTimeout = 30 * time.Second
|
|
|
|
setting.AlertingMaxAttempts = 3
|
|
|
|
engine.resultHandler = &FakeResultHandler{}
|
|
|
|
job := &Job{running: true, Rule: &Rule{}}
|
|
|
|
|
|
|
|
t.Run("Should register usage metrics func", func(t *testing.T) {
|
2022-02-03 06:26:05 -06:00
|
|
|
store.getAllAlerts = func(ctx context.Context, q *models.GetAllAlertsQuery) error {
|
2021-10-26 06:15:09 -05:00
|
|
|
settings, err := simplejson.NewJson([]byte(`{"conditions": [{"query": { "datasourceId": 1}}]}`))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
q.Result = []*models.Alert{{Settings: settings}}
|
|
|
|
return nil
|
2022-02-03 06:26:05 -06:00
|
|
|
}
|
2021-09-22 20:12:12 -05:00
|
|
|
|
2022-02-03 06:26:05 -06:00
|
|
|
store.getDataSource = func(ctx context.Context, q *models.GetDataSourceQuery) error {
|
2021-10-26 06:15:09 -05:00
|
|
|
q.Result = &models.DataSource{Id: 1, Type: models.DS_PROMETHEUS}
|
|
|
|
return nil
|
2022-02-03 06:26:05 -06:00
|
|
|
}
|
2018-02-16 10:00:13 -06:00
|
|
|
|
2021-10-26 06:15:09 -05:00
|
|
|
report, err := usMock.GetUsageReport(context.Background())
|
|
|
|
require.Nil(t, err)
|
2018-02-16 10:00:13 -06:00
|
|
|
|
2021-10-26 06:15:09 -05:00
|
|
|
require.Equal(t, 1, report.Metrics["stats.alerting.ds.prometheus.count"])
|
|
|
|
require.Equal(t, 0, report.Metrics["stats.alerting.ds.other.count"])
|
|
|
|
})
|
2018-02-16 10:00:13 -06:00
|
|
|
|
2021-10-26 06:15:09 -05:00
|
|
|
t.Run("Should trigger retry if needed", func(t *testing.T) {
|
|
|
|
t.Run("error + not last attempt -> retry", func(t *testing.T) {
|
|
|
|
engine.evalHandler = NewFakeEvalHandler(0)
|
2018-02-16 10:00:13 -06:00
|
|
|
|
2021-10-26 06:15:09 -05:00
|
|
|
for i := 1; i < setting.AlertingMaxAttempts; i++ {
|
2018-02-16 10:00:13 -06:00
|
|
|
attemptChan := make(chan int, 1)
|
2019-03-29 00:58:37 -05:00
|
|
|
cancelChan := make(chan context.CancelFunc, setting.AlertingMaxAttempts)
|
2018-02-16 10:00:13 -06:00
|
|
|
|
2021-10-26 06:15:09 -05:00
|
|
|
engine.processJob(i, attemptChan, cancelChan, job)
|
2018-02-16 10:00:13 -06:00
|
|
|
nextAttemptID, more := <-attemptChan
|
|
|
|
|
2021-10-26 06:15:09 -05:00
|
|
|
require.Equal(t, i+1, nextAttemptID)
|
|
|
|
require.Equal(t, true, more)
|
|
|
|
require.NotNil(t, <-cancelChan)
|
|
|
|
}
|
|
|
|
})
|
2018-02-16 10:00:13 -06:00
|
|
|
|
2021-10-26 06:15:09 -05:00
|
|
|
t.Run("error + last attempt -> no retry", func(t *testing.T) {
|
|
|
|
engine.evalHandler = NewFakeEvalHandler(0)
|
|
|
|
attemptChan := make(chan int, 1)
|
|
|
|
cancelChan := make(chan context.CancelFunc, setting.AlertingMaxAttempts)
|
2018-02-16 10:00:13 -06:00
|
|
|
|
2021-10-26 06:15:09 -05:00
|
|
|
engine.processJob(setting.AlertingMaxAttempts, attemptChan, cancelChan, job)
|
|
|
|
nextAttemptID, more := <-attemptChan
|
2018-02-16 10:00:13 -06:00
|
|
|
|
2021-10-26 06:15:09 -05:00
|
|
|
require.Equal(t, 0, nextAttemptID)
|
|
|
|
require.Equal(t, false, more)
|
|
|
|
require.NotNil(t, <-cancelChan)
|
2018-02-16 10:00:13 -06:00
|
|
|
})
|
|
|
|
|
2021-10-26 06:15:09 -05:00
|
|
|
t.Run("no error -> no retry", func(t *testing.T) {
|
|
|
|
engine.evalHandler = NewFakeEvalHandler(1)
|
|
|
|
attemptChan := make(chan int, 1)
|
|
|
|
cancelChan := make(chan context.CancelFunc, setting.AlertingMaxAttempts)
|
|
|
|
|
|
|
|
engine.processJob(1, attemptChan, cancelChan, job)
|
|
|
|
nextAttemptID, more := <-attemptChan
|
|
|
|
|
|
|
|
require.Equal(t, 0, nextAttemptID)
|
|
|
|
require.Equal(t, false, more)
|
|
|
|
require.NotNil(t, <-cancelChan)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Should trigger as many retries as needed", func(t *testing.T) {
|
|
|
|
t.Run("never success -> max retries number", func(t *testing.T) {
|
|
|
|
expectedAttempts := setting.AlertingMaxAttempts
|
|
|
|
evalHandler := NewFakeEvalHandler(0)
|
|
|
|
engine.evalHandler = evalHandler
|
|
|
|
|
2021-12-20 10:05:33 -06:00
|
|
|
err := engine.processJobWithRetry(context.Background(), job)
|
2021-10-26 06:15:09 -05:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, expectedAttempts, evalHandler.CallNb)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("always success -> never retry", func(t *testing.T) {
|
|
|
|
expectedAttempts := 1
|
|
|
|
evalHandler := NewFakeEvalHandler(1)
|
|
|
|
engine.evalHandler = evalHandler
|
|
|
|
|
2021-12-20 10:05:33 -06:00
|
|
|
err := engine.processJobWithRetry(context.Background(), job)
|
2021-10-26 06:15:09 -05:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, expectedAttempts, evalHandler.CallNb)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("some errors before success -> some retries", func(t *testing.T) {
|
|
|
|
expectedAttempts := int(math.Ceil(float64(setting.AlertingMaxAttempts) / 2))
|
|
|
|
evalHandler := NewFakeEvalHandler(expectedAttempts)
|
|
|
|
engine.evalHandler = evalHandler
|
|
|
|
|
2021-12-20 10:05:33 -06:00
|
|
|
err := engine.processJobWithRetry(context.Background(), job)
|
2021-10-26 06:15:09 -05:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, expectedAttempts, evalHandler.CallNb)
|
2018-02-16 10:00:13 -06:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|