eval_context.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package alerting
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "github.com/grafana/grafana/pkg/bus"
  7. "github.com/grafana/grafana/pkg/infra/log"
  8. m "github.com/grafana/grafana/pkg/models"
  9. "github.com/grafana/grafana/pkg/setting"
  10. )
  11. type EvalContext struct {
  12. Firing bool
  13. IsTestRun bool
  14. EvalMatches []*EvalMatch
  15. Logs []*ResultLogEntry
  16. Error error
  17. ConditionEvals string
  18. StartTime time.Time
  19. EndTime time.Time
  20. Rule *Rule
  21. log log.Logger
  22. dashboardRef *m.DashboardRef
  23. ImagePublicUrl string
  24. ImageOnDiskPath string
  25. NoDataFound bool
  26. PrevAlertState m.AlertStateType
  27. Ctx context.Context
  28. }
  29. func NewEvalContext(alertCtx context.Context, rule *Rule) *EvalContext {
  30. return &EvalContext{
  31. Ctx: alertCtx,
  32. StartTime: time.Now(),
  33. Rule: rule,
  34. Logs: make([]*ResultLogEntry, 0),
  35. EvalMatches: make([]*EvalMatch, 0),
  36. log: log.New("alerting.evalContext"),
  37. PrevAlertState: rule.State,
  38. }
  39. }
  40. type StateDescription struct {
  41. Color string
  42. Text string
  43. Data string
  44. }
  45. func (c *EvalContext) GetStateModel() *StateDescription {
  46. switch c.Rule.State {
  47. case m.AlertStateOK:
  48. return &StateDescription{
  49. Color: "#36a64f",
  50. Text: "OK",
  51. }
  52. case m.AlertStateNoData:
  53. return &StateDescription{
  54. Color: "#888888",
  55. Text: "No Data",
  56. }
  57. case m.AlertStateAlerting:
  58. return &StateDescription{
  59. Color: "#D63232",
  60. Text: "Alerting",
  61. }
  62. case m.AlertStateUnknown:
  63. return &StateDescription{
  64. Color: "#888888",
  65. Text: "Unknown",
  66. }
  67. default:
  68. panic("Unknown rule state for alert " + c.Rule.State)
  69. }
  70. }
  71. func (c *EvalContext) ShouldUpdateAlertState() bool {
  72. return c.Rule.State != c.PrevAlertState
  73. }
  74. func (a *EvalContext) GetDurationMs() float64 {
  75. return float64(a.EndTime.Nanosecond()-a.StartTime.Nanosecond()) / float64(1000000)
  76. }
  77. func (c *EvalContext) GetNotificationTitle() string {
  78. return "[" + c.GetStateModel().Text + "] " + c.Rule.Name
  79. }
  80. func (c *EvalContext) GetDashboardUID() (*m.DashboardRef, error) {
  81. if c.dashboardRef != nil {
  82. return c.dashboardRef, nil
  83. }
  84. uidQuery := &m.GetDashboardRefByIdQuery{Id: c.Rule.DashboardId}
  85. if err := bus.Dispatch(uidQuery); err != nil {
  86. return nil, err
  87. }
  88. c.dashboardRef = uidQuery.Result
  89. return c.dashboardRef, nil
  90. }
  91. const urlFormat = "%s?fullscreen&edit&tab=alert&panelId=%d&orgId=%d"
  92. func (c *EvalContext) GetRuleUrl() (string, error) {
  93. if c.IsTestRun {
  94. return setting.AppUrl, nil
  95. }
  96. ref, err := c.GetDashboardUID()
  97. if err != nil {
  98. return "", err
  99. }
  100. return fmt.Sprintf(urlFormat, m.GetFullDashboardUrl(ref.Uid, ref.Slug), c.Rule.PanelId, c.Rule.OrgId), nil
  101. }
  102. // GetNewState returns the new state from the alert rule evaluation
  103. func (c *EvalContext) GetNewState() m.AlertStateType {
  104. ns := getNewStateInternal(c)
  105. if ns != m.AlertStateAlerting || c.Rule.For == 0 {
  106. return ns
  107. }
  108. since := time.Since(c.Rule.LastStateChange)
  109. if c.PrevAlertState == m.AlertStatePending && since > c.Rule.For {
  110. return m.AlertStateAlerting
  111. }
  112. if c.PrevAlertState == m.AlertStateAlerting {
  113. return m.AlertStateAlerting
  114. }
  115. return m.AlertStatePending
  116. }
  117. func getNewStateInternal(c *EvalContext) m.AlertStateType {
  118. if c.Error != nil {
  119. c.log.Error("Alert Rule Result Error",
  120. "ruleId", c.Rule.Id,
  121. "name", c.Rule.Name,
  122. "error", c.Error,
  123. "changing state to", c.Rule.ExecutionErrorState.ToAlertState())
  124. if c.Rule.ExecutionErrorState == m.ExecutionErrorKeepState {
  125. return c.PrevAlertState
  126. }
  127. return c.Rule.ExecutionErrorState.ToAlertState()
  128. }
  129. if c.Firing {
  130. return m.AlertStateAlerting
  131. }
  132. if c.NoDataFound {
  133. c.log.Info("Alert Rule returned no data",
  134. "ruleId", c.Rule.Id,
  135. "name", c.Rule.Name,
  136. "changing state to", c.Rule.NoDataState.ToAlertState())
  137. if c.Rule.NoDataState == m.NoDataKeepState {
  138. return c.PrevAlertState
  139. }
  140. return c.Rule.NoDataState.ToAlertState()
  141. }
  142. return m.AlertStateOK
  143. }