eval_handler.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package alerting
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/grafana/grafana/pkg/log"
  6. )
  7. var (
  8. descriptionFmt = "Actual value: %1.2f for %s. "
  9. )
  10. type DefaultEvalHandler struct {
  11. log log.Logger
  12. alertJobTimeout time.Duration
  13. }
  14. func NewEvalHandler() *DefaultEvalHandler {
  15. return &DefaultEvalHandler{
  16. log: log.New("alerting.evalHandler"),
  17. alertJobTimeout: time.Second * 5,
  18. }
  19. }
  20. func (e *DefaultEvalHandler) Eval(context *EvalContext) {
  21. go e.eval(context)
  22. select {
  23. case <-time.After(e.alertJobTimeout):
  24. context.Error = fmt.Errorf("Timeout")
  25. context.EndTime = time.Now()
  26. e.log.Debug("Job Execution timeout", "alertId", context.Rule.Id)
  27. case <-context.DoneChan:
  28. e.log.Debug("Job Execution done", "timeMs", context.GetDurationMs(), "alertId", context.Rule.Id, "firing", context.Firing)
  29. }
  30. }
  31. func (e *DefaultEvalHandler) eval(context *EvalContext) {
  32. for _, condition := range context.Rule.Conditions {
  33. condition.Eval(context)
  34. // break if condition could not be evaluated
  35. if context.Error != nil {
  36. break
  37. }
  38. // break if result has not triggered yet
  39. if context.Firing == false {
  40. break
  41. }
  42. }
  43. context.EndTime = time.Now()
  44. context.DoneChan <- true
  45. }