2016-07-27 22:29:28 +08:00
|
|
|
package alerting
|
|
|
|
|
|
|
|
|
|
import (
|
2016-11-17 17:28:17 +08:00
|
|
|
"strconv"
|
2016-11-17 22:48:15 +08:00
|
|
|
"strings"
|
2016-07-27 22:29:28 +08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/log"
|
2016-08-12 03:12:39 +08:00
|
|
|
"github.com/grafana/grafana/pkg/metrics"
|
2016-07-27 22:29:28 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type DefaultEvalHandler struct {
|
|
|
|
|
log log.Logger
|
|
|
|
|
alertJobTimeout time.Duration
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewEvalHandler() *DefaultEvalHandler {
|
|
|
|
|
return &DefaultEvalHandler{
|
2016-07-28 23:03:53 +08:00
|
|
|
log: log.New("alerting.evalHandler"),
|
2016-10-03 15:38:03 +08:00
|
|
|
alertJobTimeout: time.Second * 5,
|
2016-07-27 22:29:28 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *DefaultEvalHandler) Eval(context *EvalContext) {
|
2016-11-03 22:26:17 +08:00
|
|
|
firing := true
|
2016-11-22 18:44:25 +08:00
|
|
|
noDataFound := true
|
2016-11-17 22:48:15 +08:00
|
|
|
conditionEvals := ""
|
|
|
|
|
|
2016-11-17 17:28:17 +08:00
|
|
|
for i := 0; i < len(context.Rule.Conditions); i++ {
|
|
|
|
|
condition := context.Rule.Conditions[i]
|
2016-11-03 22:26:17 +08:00
|
|
|
cr, err := condition.Eval(context)
|
|
|
|
|
if err != nil {
|
|
|
|
|
context.Error = err
|
|
|
|
|
}
|
2016-07-27 22:29:28 +08:00
|
|
|
|
|
|
|
|
// break if condition could not be evaluated
|
|
|
|
|
if context.Error != nil {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-15 22:35:25 +08:00
|
|
|
// calculating Firing based on operator
|
|
|
|
|
if cr.Operator == "or" {
|
|
|
|
|
firing = firing || cr.Firing
|
2016-11-22 18:44:25 +08:00
|
|
|
noDataFound = noDataFound || cr.NoDataFound
|
2016-11-15 22:35:25 +08:00
|
|
|
} else {
|
|
|
|
|
firing = firing && cr.Firing
|
2016-11-22 18:44:25 +08:00
|
|
|
noDataFound = noDataFound && cr.NoDataFound
|
2016-07-27 22:29:28 +08:00
|
|
|
}
|
2016-11-03 22:26:17 +08:00
|
|
|
|
2016-11-17 17:28:17 +08:00
|
|
|
if i > 0 {
|
2016-11-17 22:48:15 +08:00
|
|
|
conditionEvals = "[" + conditionEvals + " " + strings.ToUpper(cr.Operator) + " " + strconv.FormatBool(cr.Firing) + "]"
|
2016-11-17 17:28:17 +08:00
|
|
|
} else {
|
2016-11-17 22:48:15 +08:00
|
|
|
conditionEvals = strconv.FormatBool(firing)
|
2016-11-17 17:28:17 +08:00
|
|
|
}
|
|
|
|
|
|
2016-11-03 22:26:17 +08:00
|
|
|
context.EvalMatches = append(context.EvalMatches, cr.EvalMatches...)
|
2016-07-27 22:29:28 +08:00
|
|
|
}
|
|
|
|
|
|
2016-11-17 22:48:15 +08:00
|
|
|
context.ConditionEvals = conditionEvals + " = " + strconv.FormatBool(firing)
|
2016-11-03 22:26:17 +08:00
|
|
|
context.Firing = firing
|
2016-11-22 18:44:25 +08:00
|
|
|
context.NoDataFound = noDataFound
|
2016-07-27 22:29:28 +08:00
|
|
|
context.EndTime = time.Now()
|
2016-09-08 16:45:10 +08:00
|
|
|
elapsedTime := context.EndTime.Sub(context.StartTime) / time.Millisecond
|
2016-12-14 11:20:50 +08:00
|
|
|
metrics.M_Alerting_Execution_Time.Update(elapsedTime)
|
2016-07-27 22:29:28 +08:00
|
|
|
}
|