grafana/pkg/services/alerting/eval_handler.go

49 lines
1.0 KiB
Go
Raw Normal View History

2016-07-27 09:29:28 -05:00
package alerting
import (
"time"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/metrics"
2016-07-27 09:29:28 -05:00
)
type DefaultEvalHandler struct {
log log.Logger
alertJobTimeout time.Duration
}
func NewEvalHandler() *DefaultEvalHandler {
return &DefaultEvalHandler{
log: log.New("alerting.evalHandler"),
alertJobTimeout: time.Second * 5,
2016-07-27 09:29:28 -05:00
}
}
func (e *DefaultEvalHandler) Eval(context *EvalContext) {
firing := true
2016-07-27 09:29:28 -05:00
for _, condition := range context.Rule.Conditions {
cr, err := condition.Eval(context)
if err != nil {
context.Error = err
}
2016-07-27 09:29:28 -05:00
// break if condition could not be evaluated
if context.Error != nil {
break
}
// break if result has not triggered yet
if cr.Firing == false {
firing = false
2016-07-27 09:29:28 -05:00
break
}
context.EvalMatches = append(context.EvalMatches, cr.EvalMatches...)
2016-07-27 09:29:28 -05:00
}
context.Firing = firing
2016-07-27 09:29:28 -05:00
context.EndTime = time.Now()
elapsedTime := context.EndTime.Sub(context.StartTime) / time.Millisecond
metrics.M_Alerting_Exeuction_Time.Update(elapsedTime)
2016-07-27 09:29:28 -05:00
}