eval_handler.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package alerting
  2. import (
  3. "time"
  4. "github.com/grafana/grafana/pkg/log"
  5. "github.com/grafana/grafana/pkg/metrics"
  6. )
  7. type DefaultEvalHandler struct {
  8. log log.Logger
  9. alertJobTimeout time.Duration
  10. }
  11. func NewEvalHandler() *DefaultEvalHandler {
  12. return &DefaultEvalHandler{
  13. log: log.New("alerting.evalHandler"),
  14. alertJobTimeout: time.Second * 5,
  15. }
  16. }
  17. func (e *DefaultEvalHandler) Eval(context *EvalContext) {
  18. firing := true
  19. for _, condition := range context.Rule.Conditions {
  20. cr, err := condition.Eval(context)
  21. if err != nil {
  22. context.Error = err
  23. }
  24. // break if condition could not be evaluated
  25. if context.Error != nil {
  26. break
  27. }
  28. // break if result has not triggered yet
  29. if cr.Firing == false {
  30. firing = false
  31. break
  32. }
  33. context.EvalMatches = append(context.EvalMatches, cr.EvalMatches...)
  34. }
  35. context.Firing = firing
  36. context.EndTime = time.Now()
  37. elapsedTime := context.EndTime.Sub(context.StartTime) / time.Millisecond
  38. metrics.M_Alerting_Exeuction_Time.Update(elapsedTime)
  39. }