eval_handler.go 860 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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. for _, condition := range context.Rule.Conditions {
  19. condition.Eval(context)
  20. // break if condition could not be evaluated
  21. if context.Error != nil {
  22. break
  23. }
  24. // break if result has not triggered yet
  25. if context.Firing == false {
  26. break
  27. }
  28. }
  29. context.EndTime = time.Now()
  30. elapsedTime := context.EndTime.Sub(context.StartTime) / time.Millisecond
  31. metrics.M_Alerting_Exeuction_Time.Update(elapsedTime)
  32. }