2016-06-06 03:31:21 -05:00
|
|
|
package alerting
|
2016-06-06 04:56:58 -05:00
|
|
|
|
|
|
|
import (
|
2016-09-27 09:38:19 -05:00
|
|
|
"context"
|
2017-09-11 13:57:08 -05:00
|
|
|
"fmt"
|
2016-06-06 04:56:58 -05:00
|
|
|
"time"
|
|
|
|
|
2017-09-10 07:13:57 -05:00
|
|
|
"github.com/opentracing/opentracing-go"
|
2017-09-11 13:57:08 -05:00
|
|
|
"github.com/opentracing/opentracing-go/ext"
|
|
|
|
tlog "github.com/opentracing/opentracing-go/log"
|
2017-09-10 07:13:57 -05:00
|
|
|
|
2016-06-06 04:56:58 -05:00
|
|
|
"github.com/benbjohnson/clock"
|
2016-06-06 06:50:47 -05:00
|
|
|
"github.com/grafana/grafana/pkg/log"
|
2018-04-27 06:41:58 -05:00
|
|
|
"github.com/grafana/grafana/pkg/registry"
|
2018-05-24 08:26:27 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/rendering"
|
2018-04-27 06:41:58 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2016-09-27 09:38:19 -05:00
|
|
|
"golang.org/x/sync/errgroup"
|
2016-06-06 04:56:58 -05:00
|
|
|
)
|
|
|
|
|
2018-05-08 04:55:54 -05:00
|
|
|
type AlertingService struct {
|
2018-05-24 08:26:27 -05:00
|
|
|
RenderService rendering.Service `inject:""`
|
|
|
|
|
2018-04-18 15:23:49 -05:00
|
|
|
execQueue chan *Job
|
|
|
|
//clock clock.Clock
|
2016-07-27 09:29:28 -05:00
|
|
|
ticker *Ticker
|
|
|
|
scheduler Scheduler
|
|
|
|
evalHandler EvalHandler
|
|
|
|
ruleReader RuleReader
|
|
|
|
log log.Logger
|
|
|
|
resultHandler ResultHandler
|
2016-06-06 04:56:58 -05:00
|
|
|
}
|
|
|
|
|
2018-04-27 06:41:58 -05:00
|
|
|
func init() {
|
2018-05-08 04:55:54 -05:00
|
|
|
registry.RegisterService(&AlertingService{})
|
2018-04-27 06:41:58 -05:00
|
|
|
}
|
2016-06-06 04:56:58 -05:00
|
|
|
|
2018-05-08 04:55:54 -05:00
|
|
|
func NewEngine() *AlertingService {
|
|
|
|
e := &AlertingService{}
|
2018-04-27 06:41:58 -05:00
|
|
|
e.Init()
|
2016-06-06 04:56:58 -05:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2018-05-08 04:55:54 -05:00
|
|
|
func (e *AlertingService) IsDisabled() bool {
|
2018-04-27 06:41:58 -05:00
|
|
|
return !setting.AlertingEnabled || !setting.ExecuteAlerts
|
|
|
|
}
|
2016-06-06 06:50:47 -05:00
|
|
|
|
2018-05-08 04:55:54 -05:00
|
|
|
func (e *AlertingService) Init() error {
|
2018-04-27 06:41:58 -05:00
|
|
|
e.ticker = NewTicker(time.Now(), time.Second*0, clock.New())
|
|
|
|
e.execQueue = make(chan *Job, 1000)
|
|
|
|
e.scheduler = NewScheduler()
|
|
|
|
e.evalHandler = NewEvalHandler()
|
|
|
|
e.ruleReader = NewRuleReader()
|
|
|
|
e.log = log.New("alerting.engine")
|
2018-05-24 08:26:27 -05:00
|
|
|
e.resultHandler = NewResultHandler(e.RenderService)
|
2018-04-27 06:41:58 -05:00
|
|
|
return nil
|
|
|
|
}
|
2016-09-27 09:38:19 -05:00
|
|
|
|
2018-05-08 04:55:54 -05:00
|
|
|
func (e *AlertingService) Run(ctx context.Context) error {
|
2018-04-27 06:41:58 -05:00
|
|
|
alertGroup, ctx := errgroup.WithContext(ctx)
|
2016-10-03 02:38:03 -05:00
|
|
|
alertGroup.Go(func() error { return e.alertingTicker(ctx) })
|
|
|
|
alertGroup.Go(func() error { return e.runJobDispatcher(ctx) })
|
2016-09-27 09:38:19 -05:00
|
|
|
|
2016-10-03 02:38:03 -05:00
|
|
|
err := alertGroup.Wait()
|
2016-09-28 14:06:00 -05:00
|
|
|
return err
|
2016-06-06 04:56:58 -05:00
|
|
|
}
|
|
|
|
|
2018-05-08 04:55:54 -05:00
|
|
|
func (e *AlertingService) alertingTicker(grafanaCtx context.Context) error {
|
2016-06-11 06:49:11 -05:00
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
2016-06-22 06:43:11 -05:00
|
|
|
e.log.Error("Scheduler Panic: stopping alertingTicker", "error", err, "stack", log.Stack(1))
|
2016-06-11 06:49:11 -05:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-06-06 06:50:47 -05:00
|
|
|
tickIndex := 0
|
|
|
|
|
2016-06-06 04:56:58 -05:00
|
|
|
for {
|
|
|
|
select {
|
2016-09-27 09:38:19 -05:00
|
|
|
case <-grafanaCtx.Done():
|
|
|
|
return grafanaCtx.Err()
|
2016-06-06 04:56:58 -05:00
|
|
|
case tick := <-e.ticker.C:
|
2016-06-06 10:11:46 -05:00
|
|
|
// TEMP SOLUTION update rules ever tenth tick
|
2016-06-06 06:50:47 -05:00
|
|
|
if tickIndex%10 == 0 {
|
|
|
|
e.scheduler.Update(e.ruleReader.Fetch())
|
|
|
|
}
|
|
|
|
|
2016-06-06 04:56:58 -05:00
|
|
|
e.scheduler.Tick(tick, e.execQueue)
|
2016-06-06 07:24:14 -05:00
|
|
|
tickIndex++
|
2016-06-06 04:56:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-08 04:55:54 -05:00
|
|
|
func (e *AlertingService) runJobDispatcher(grafanaCtx context.Context) error {
|
2016-10-03 02:38:03 -05:00
|
|
|
dispatcherGroup, alertCtx := errgroup.WithContext(grafanaCtx)
|
|
|
|
|
2016-09-27 09:38:19 -05:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-grafanaCtx.Done():
|
2016-10-03 02:38:03 -05:00
|
|
|
return dispatcherGroup.Wait()
|
2016-09-27 09:38:19 -05:00
|
|
|
case job := <-e.execQueue:
|
2018-02-16 10:00:13 -06:00
|
|
|
dispatcherGroup.Go(func() error { return e.processJobWithRetry(alertCtx, job) })
|
2016-09-27 09:38:19 -05:00
|
|
|
}
|
2016-06-06 06:50:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-03 02:38:03 -05:00
|
|
|
var (
|
2018-04-27 15:14:36 -05:00
|
|
|
unfinishedWorkTimeout = time.Second * 5
|
2016-10-03 02:38:03 -05:00
|
|
|
)
|
|
|
|
|
2018-05-08 04:55:54 -05:00
|
|
|
func (e *AlertingService) processJobWithRetry(grafanaCtx context.Context, job *Job) error {
|
2018-02-16 10:00:13 -06:00
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
e.log.Error("Alert Panic", "error", err, "stack", log.Stack(1))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-03-29 00:58:37 -05:00
|
|
|
cancelChan := make(chan context.CancelFunc, setting.AlertingMaxAttempts*2)
|
2018-02-16 10:00:13 -06:00
|
|
|
attemptChan := make(chan int, 1)
|
|
|
|
|
|
|
|
// Initialize with first attemptID=1
|
|
|
|
attemptChan <- 1
|
|
|
|
job.Running = true
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-grafanaCtx.Done():
|
|
|
|
// In case grafana server context is cancel, let a chance to job processing
|
|
|
|
// to finish gracefully - by waiting a timeout duration - before forcing its end.
|
|
|
|
unfinishedWorkTimer := time.NewTimer(unfinishedWorkTimeout)
|
|
|
|
select {
|
|
|
|
case <-unfinishedWorkTimer.C:
|
|
|
|
return e.endJob(grafanaCtx.Err(), cancelChan, job)
|
|
|
|
case <-attemptChan:
|
|
|
|
return e.endJob(nil, cancelChan, job)
|
|
|
|
}
|
|
|
|
case attemptID, more := <-attemptChan:
|
|
|
|
if !more {
|
|
|
|
return e.endJob(nil, cancelChan, job)
|
|
|
|
}
|
|
|
|
go e.processJob(attemptID, attemptChan, cancelChan, job)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-08 04:55:54 -05:00
|
|
|
func (e *AlertingService) endJob(err error, cancelChan chan context.CancelFunc, job *Job) error {
|
2018-02-16 10:00:13 -06:00
|
|
|
job.Running = false
|
|
|
|
close(cancelChan)
|
|
|
|
for cancelFn := range cancelChan {
|
|
|
|
cancelFn()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-08 04:55:54 -05:00
|
|
|
func (e *AlertingService) processJob(attemptID int, attemptChan chan int, cancelChan chan context.CancelFunc, job *Job) {
|
2016-07-21 10:31:46 -05:00
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
2016-10-03 02:38:03 -05:00
|
|
|
e.log.Error("Alert Panic", "error", err, "stack", log.Stack(1))
|
2016-07-21 10:31:46 -05:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-03-29 00:58:37 -05:00
|
|
|
alertCtx, cancelFn := context.WithTimeout(context.Background(), setting.AlertingEvaluationTimeout)
|
2018-02-16 10:00:13 -06:00
|
|
|
cancelChan <- cancelFn
|
2017-09-11 13:57:08 -05:00
|
|
|
span := opentracing.StartSpan("alert execution")
|
|
|
|
alertCtx = opentracing.ContextWithSpan(alertCtx, span)
|
2016-10-03 02:38:03 -05:00
|
|
|
|
|
|
|
evalContext := NewEvalContext(alertCtx, job.Rule)
|
2017-09-10 07:13:57 -05:00
|
|
|
evalContext.Ctx = alertCtx
|
|
|
|
|
2016-09-27 09:38:19 -05:00
|
|
|
go func() {
|
2016-10-21 07:46:55 -05:00
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
e.log.Error("Alert Panic", "error", err, "stack", log.Stack(1))
|
2017-09-11 13:57:08 -05:00
|
|
|
ext.Error.Set(span, true)
|
2017-09-12 03:31:57 -05:00
|
|
|
span.LogFields(
|
|
|
|
tlog.Error(fmt.Errorf("%v", err)),
|
|
|
|
tlog.String("message", "failed to execute alert rule. panic was recovered."),
|
|
|
|
)
|
2017-09-11 13:57:08 -05:00
|
|
|
span.Finish()
|
2018-02-16 10:00:13 -06:00
|
|
|
close(attemptChan)
|
2016-10-21 07:46:55 -05:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-10-03 02:38:03 -05:00
|
|
|
e.evalHandler.Eval(evalContext)
|
2017-09-11 11:48:20 -05:00
|
|
|
|
|
|
|
span.SetTag("alertId", evalContext.Rule.Id)
|
|
|
|
span.SetTag("dashboardId", evalContext.Rule.DashboardId)
|
|
|
|
span.SetTag("firing", evalContext.Firing)
|
2017-09-18 07:59:59 -05:00
|
|
|
span.SetTag("nodatapoints", evalContext.NoDataFound)
|
2018-02-16 10:00:13 -06:00
|
|
|
span.SetTag("attemptID", attemptID)
|
|
|
|
|
2017-09-18 07:53:30 -05:00
|
|
|
if evalContext.Error != nil {
|
|
|
|
ext.Error.Set(span, true)
|
|
|
|
span.LogFields(
|
|
|
|
tlog.Error(evalContext.Error),
|
2018-02-16 10:00:13 -06:00
|
|
|
tlog.String("message", "alerting execution attempt failed"),
|
2017-09-18 07:53:30 -05:00
|
|
|
)
|
2019-03-29 00:58:37 -05:00
|
|
|
if attemptID < setting.AlertingMaxAttempts {
|
2018-02-16 10:00:13 -06:00
|
|
|
span.Finish()
|
|
|
|
e.log.Debug("Job Execution attempt triggered retry", "timeMs", evalContext.GetDurationMs(), "alertId", evalContext.Rule.Id, "name", evalContext.Rule.Name, "firing", evalContext.Firing, "attemptID", attemptID)
|
|
|
|
attemptChan <- (attemptID + 1)
|
|
|
|
return
|
|
|
|
}
|
2017-09-18 07:53:30 -05:00
|
|
|
}
|
|
|
|
|
2019-01-28 00:56:31 -06:00
|
|
|
// create new context with timeout for notifications
|
2019-03-29 00:58:37 -05:00
|
|
|
resultHandleCtx, resultHandleCancelFn := context.WithTimeout(context.Background(), setting.AlertingNotificationTimeout)
|
2019-01-15 02:30:51 -06:00
|
|
|
cancelChan <- resultHandleCancelFn
|
2019-01-28 00:56:31 -06:00
|
|
|
|
|
|
|
// override the context used for evaluation with a new context for notifications.
|
|
|
|
// This makes it possible for notifiers to execute when datasources
|
|
|
|
// dont respond within the timeout limit. We should rewrite this so notifications
|
|
|
|
// dont reuse the evalContext and get its own context.
|
2019-01-15 02:30:51 -06:00
|
|
|
evalContext.Ctx = resultHandleCtx
|
2018-03-21 14:48:29 -05:00
|
|
|
evalContext.Rule.State = evalContext.GetNewState()
|
2018-02-16 10:00:13 -06:00
|
|
|
e.resultHandler.Handle(evalContext)
|
2017-09-10 07:13:57 -05:00
|
|
|
span.Finish()
|
2018-02-16 10:00:13 -06:00
|
|
|
e.log.Debug("Job Execution completed", "timeMs", evalContext.GetDurationMs(), "alertId", evalContext.Rule.Id, "name", evalContext.Rule.Name, "firing", evalContext.Firing, "attemptID", attemptID)
|
|
|
|
close(attemptChan)
|
2016-09-27 09:38:19 -05:00
|
|
|
}()
|
2016-06-06 04:56:58 -05:00
|
|
|
}
|