2016-07-27 09:29:28 -05:00
|
|
|
package alerting
|
|
|
|
|
|
|
|
import (
|
2016-11-17 03:28:17 -06:00
|
|
|
"strconv"
|
2016-11-17 08:48:15 -06:00
|
|
|
"strings"
|
2016-07-27 09:29:28 -05:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/log"
|
2016-08-11 14:12:39 -05:00
|
|
|
"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{
|
2016-07-28 10:03:53 -05:00
|
|
|
log: log.New("alerting.evalHandler"),
|
2016-10-03 02:38:03 -05:00
|
|
|
alertJobTimeout: time.Second * 5,
|
2016-07-27 09:29:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *DefaultEvalHandler) Eval(context *EvalContext) {
|
2016-11-03 09:26:17 -05:00
|
|
|
firing := true
|
2016-11-17 08:48:15 -06:00
|
|
|
conditionEvals := ""
|
|
|
|
|
2016-11-17 03:28:17 -06:00
|
|
|
for i := 0; i < len(context.Rule.Conditions); i++ {
|
|
|
|
condition := context.Rule.Conditions[i]
|
2016-11-03 09:26:17 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-11-15 08:35:25 -06:00
|
|
|
// calculating Firing based on operator
|
|
|
|
if cr.Operator == "or" {
|
|
|
|
firing = firing || cr.Firing
|
|
|
|
} else {
|
|
|
|
firing = firing && cr.Firing
|
2016-07-27 09:29:28 -05:00
|
|
|
}
|
2016-11-03 09:26:17 -05:00
|
|
|
|
2016-11-17 03:28:17 -06:00
|
|
|
if i > 0 {
|
2016-11-17 08:48:15 -06:00
|
|
|
conditionEvals = "[" + conditionEvals + " " + strings.ToUpper(cr.Operator) + " " + strconv.FormatBool(cr.Firing) + "]"
|
2016-11-17 03:28:17 -06:00
|
|
|
} else {
|
2016-11-17 08:48:15 -06:00
|
|
|
conditionEvals = strconv.FormatBool(firing)
|
2016-11-17 03:28:17 -06:00
|
|
|
}
|
|
|
|
|
2016-11-03 09:26:17 -05:00
|
|
|
context.EvalMatches = append(context.EvalMatches, cr.EvalMatches...)
|
2016-07-27 09:29:28 -05:00
|
|
|
}
|
|
|
|
|
2016-11-17 08:48:15 -06:00
|
|
|
context.ConditionEvals = conditionEvals + " = " + strconv.FormatBool(firing)
|
2016-11-03 09:26:17 -05:00
|
|
|
context.Firing = firing
|
2016-07-27 09:29:28 -05:00
|
|
|
context.EndTime = time.Now()
|
2016-09-08 03:45:10 -05:00
|
|
|
elapsedTime := context.EndTime.Sub(context.StartTime) / time.Millisecond
|
2016-08-11 14:12:39 -05:00
|
|
|
metrics.M_Alerting_Exeuction_Time.Update(elapsedTime)
|
2016-07-27 09:29:28 -05:00
|
|
|
}
|