2021-04-23 14:32:25 -05:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/eval"
|
|
|
|
ngModels "github.com/grafana/grafana/pkg/services/ngalert/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
type State struct {
|
|
|
|
AlertRuleUID string
|
|
|
|
OrgID int64
|
|
|
|
CacheId string
|
|
|
|
State eval.State
|
|
|
|
Results []Evaluation
|
|
|
|
StartsAt time.Time
|
|
|
|
EndsAt time.Time
|
|
|
|
LastEvaluationTime time.Time
|
|
|
|
EvaluationDuration time.Duration
|
2021-05-19 15:15:09 -05:00
|
|
|
LastSentAt time.Time
|
2021-04-23 14:32:25 -05:00
|
|
|
Annotations map[string]string
|
2021-05-19 15:15:09 -05:00
|
|
|
Labels data.Labels
|
2021-05-04 12:08:12 -05:00
|
|
|
Error error
|
2021-04-23 14:32:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type Evaluation struct {
|
2021-05-18 08:12:39 -05:00
|
|
|
EvaluationTime time.Time
|
|
|
|
EvaluationState eval.State
|
|
|
|
EvaluationString string
|
2021-04-23 14:32:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func resultNormal(alertState *State, result eval.Result) *State {
|
|
|
|
newState := alertState
|
|
|
|
if alertState.State != eval.Normal {
|
|
|
|
newState.EndsAt = result.EvaluatedAt
|
2021-05-04 12:08:12 -05:00
|
|
|
newState.StartsAt = result.EvaluatedAt
|
2021-04-23 14:32:25 -05:00
|
|
|
}
|
2021-05-04 12:08:12 -05:00
|
|
|
newState.Error = result.Error // should be nil since state is not error
|
2021-04-23 14:32:25 -05:00
|
|
|
newState.State = eval.Normal
|
|
|
|
return newState
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *State) resultAlerting(alertRule *ngModels.AlertRule, result eval.Result) *State {
|
|
|
|
switch a.State {
|
|
|
|
case eval.Alerting:
|
|
|
|
if !(alertRule.For > 0) {
|
|
|
|
// If there is not For set, we will set EndsAt to be twice the evaluation interval
|
|
|
|
// to avoid flapping with every evaluation
|
|
|
|
a.EndsAt = result.EvaluatedAt.Add(time.Duration(alertRule.IntervalSeconds*2) * time.Second)
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
a.EndsAt = result.EvaluatedAt.Add(alertRule.For)
|
|
|
|
case eval.Pending:
|
|
|
|
if result.EvaluatedAt.Sub(a.StartsAt) > alertRule.For {
|
|
|
|
a.State = eval.Alerting
|
|
|
|
a.StartsAt = result.EvaluatedAt
|
|
|
|
a.EndsAt = result.EvaluatedAt.Add(alertRule.For)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
a.StartsAt = result.EvaluatedAt
|
|
|
|
if !(alertRule.For > 0) {
|
|
|
|
a.EndsAt = result.EvaluatedAt.Add(time.Duration(alertRule.IntervalSeconds*2) * time.Second)
|
|
|
|
a.State = eval.Alerting
|
|
|
|
} else {
|
|
|
|
a.EndsAt = result.EvaluatedAt.Add(alertRule.For)
|
|
|
|
if result.EvaluatedAt.Sub(a.StartsAt) > alertRule.For {
|
|
|
|
a.State = eval.Alerting
|
|
|
|
} else {
|
|
|
|
a.State = eval.Pending
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *State) resultError(alertRule *ngModels.AlertRule, result eval.Result) *State {
|
2021-05-04 12:08:12 -05:00
|
|
|
a.Error = result.Error
|
2021-04-23 14:32:25 -05:00
|
|
|
if a.StartsAt.IsZero() {
|
|
|
|
a.StartsAt = result.EvaluatedAt
|
|
|
|
}
|
|
|
|
if !(alertRule.For > 0) {
|
|
|
|
a.EndsAt = result.EvaluatedAt.Add(time.Duration(alertRule.IntervalSeconds*2) * time.Second)
|
|
|
|
} else {
|
|
|
|
a.EndsAt = result.EvaluatedAt.Add(alertRule.For)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch alertRule.ExecErrState {
|
|
|
|
case ngModels.AlertingErrState:
|
|
|
|
a.State = eval.Alerting
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *State) resultNoData(alertRule *ngModels.AlertRule, result eval.Result) *State {
|
|
|
|
if a.StartsAt.IsZero() {
|
|
|
|
a.StartsAt = result.EvaluatedAt
|
|
|
|
}
|
|
|
|
if !(alertRule.For > 0) {
|
|
|
|
a.EndsAt = result.EvaluatedAt.Add(time.Duration(alertRule.IntervalSeconds*2) * time.Second)
|
|
|
|
} else {
|
|
|
|
a.EndsAt = result.EvaluatedAt.Add(alertRule.For)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch alertRule.NoDataState {
|
|
|
|
case ngModels.Alerting:
|
|
|
|
a.State = eval.Alerting
|
|
|
|
case ngModels.NoData:
|
|
|
|
a.State = eval.NoData
|
|
|
|
case ngModels.OK:
|
|
|
|
a.State = eval.Normal
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2021-05-19 15:15:09 -05:00
|
|
|
func (a *State) NeedsSending(resendDelay time.Duration) bool {
|
|
|
|
if a.State != eval.Alerting {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// if LastSentAt is before or equal to LastEvaluationTime + resendDelay, send again
|
|
|
|
return a.LastSentAt.Add(resendDelay).Before(a.LastEvaluationTime) ||
|
|
|
|
a.LastSentAt.Add(resendDelay).Equal(a.LastEvaluationTime)
|
|
|
|
}
|
|
|
|
|
2021-04-23 14:32:25 -05:00
|
|
|
func (a *State) Equals(b *State) bool {
|
|
|
|
return a.AlertRuleUID == b.AlertRuleUID &&
|
|
|
|
a.OrgID == b.OrgID &&
|
|
|
|
a.CacheId == b.CacheId &&
|
|
|
|
a.Labels.String() == b.Labels.String() &&
|
|
|
|
a.State.String() == b.State.String() &&
|
|
|
|
a.StartsAt == b.StartsAt &&
|
|
|
|
a.EndsAt == b.EndsAt &&
|
2021-04-30 13:23:12 -05:00
|
|
|
a.LastEvaluationTime == b.LastEvaluationTime &&
|
|
|
|
data.Labels(a.Annotations).String() == data.Labels(b.Annotations).String()
|
2021-04-23 14:32:25 -05:00
|
|
|
}
|
2021-05-18 12:56:14 -05:00
|
|
|
|
|
|
|
func (a *State) TrimResults(alertRule *ngModels.AlertRule) {
|
|
|
|
numBuckets := 2 * (int64(alertRule.For.Seconds()) / alertRule.IntervalSeconds)
|
|
|
|
if numBuckets == 0 {
|
|
|
|
numBuckets = 10 // keep at least 10 evaluations in the event For is set to 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(a.Results) < int(numBuckets) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
newResults := make([]Evaluation, numBuckets)
|
|
|
|
copy(newResults, a.Results[len(a.Results)-int(numBuckets):])
|
|
|
|
a.Results = newResults
|
|
|
|
}
|