2016-06-06 07:24:14 -05:00
|
|
|
package alerting
|
|
|
|
|
|
|
|
import (
|
2021-11-03 08:10:39 -05:00
|
|
|
"context"
|
2016-06-06 07:24:14 -05:00
|
|
|
"sync"
|
|
|
|
|
2019-05-13 01:45:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2019-02-23 16:35:26 -06:00
|
|
|
"github.com/grafana/grafana/pkg/infra/metrics"
|
2023-01-23 07:19:25 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/alerting/models"
|
2016-06-06 07:24:14 -05:00
|
|
|
)
|
|
|
|
|
2019-05-20 05:13:32 -05:00
|
|
|
type ruleReader interface {
|
2021-11-03 08:10:39 -05:00
|
|
|
fetch(context.Context) []*Rule
|
2016-06-06 07:24:14 -05:00
|
|
|
}
|
|
|
|
|
2019-05-20 05:13:32 -05:00
|
|
|
type defaultRuleReader struct {
|
2016-06-06 07:24:14 -05:00
|
|
|
sync.RWMutex
|
2022-02-03 06:26:05 -06:00
|
|
|
sqlStore AlertStore
|
|
|
|
log log.Logger
|
2016-06-06 07:24:14 -05:00
|
|
|
}
|
|
|
|
|
2022-02-03 06:26:05 -06:00
|
|
|
func newRuleReader(sqlStore AlertStore) *defaultRuleReader {
|
2019-05-20 05:13:32 -05:00
|
|
|
ruleReader := &defaultRuleReader{
|
2022-02-03 06:26:05 -06:00
|
|
|
sqlStore: sqlStore,
|
|
|
|
log: log.New("alerting.ruleReader"),
|
2016-07-26 05:29:52 -05:00
|
|
|
}
|
2016-06-06 07:24:14 -05:00
|
|
|
|
|
|
|
return ruleReader
|
|
|
|
}
|
|
|
|
|
2021-11-03 08:10:39 -05:00
|
|
|
func (arr *defaultRuleReader) fetch(ctx context.Context) []*Rule {
|
2019-05-14 01:15:05 -05:00
|
|
|
cmd := &models.GetAllAlertsQuery{}
|
2016-06-06 07:24:14 -05:00
|
|
|
|
2023-02-02 02:41:05 -06:00
|
|
|
alerts, err := arr.sqlStore.GetAllAlertQueryHandler(ctx, cmd)
|
|
|
|
if err != nil {
|
2016-07-26 05:29:52 -05:00
|
|
|
arr.log.Error("Could not load alerts", "error", err)
|
2016-07-27 09:29:28 -05:00
|
|
|
return []*Rule{}
|
2016-06-06 07:24:14 -05:00
|
|
|
}
|
|
|
|
|
2016-07-27 09:29:28 -05:00
|
|
|
res := make([]*Rule, 0)
|
2023-02-02 02:41:05 -06:00
|
|
|
for _, ruleDef := range alerts {
|
2022-04-08 07:30:25 -05:00
|
|
|
if model, err := NewRuleFromDBAlert(ctx, arr.sqlStore, ruleDef, false); err != nil {
|
2023-02-02 10:22:43 -06:00
|
|
|
arr.log.Error("Could not build alert model for rule", "ruleId", ruleDef.ID, "error", err)
|
2016-07-26 05:29:52 -05:00
|
|
|
} else {
|
|
|
|
res = append(res, model)
|
|
|
|
}
|
2016-06-06 07:24:14 -05:00
|
|
|
}
|
|
|
|
|
2019-07-16 09:58:46 -05:00
|
|
|
metrics.MAlertingActiveAlerts.Set(float64(len(res)))
|
2016-06-06 07:24:14 -05:00
|
|
|
return res
|
|
|
|
}
|