diff --git a/model/alert.go b/model/alert.go index 35e739c7..a0c7a667 100644 --- a/model/alert.go +++ b/model/alert.go @@ -34,6 +34,8 @@ type Alert struct { // Extra key/value information which does not define alert identity. Annotations LabelSet `json:"annotations"` + ActiveAt time.Time `json:"activeAt,omitempty"` + // The known time range for this alert. Both ends are optional. StartsAt time.Time `json:"startsAt,omitempty"` EndsAt time.Time `json:"endsAt,omitempty"` @@ -83,9 +85,15 @@ func (a *Alert) Status() AlertStatus { // Validate checks whether the alert data is inconsistent. func (a *Alert) Validate() error { + if a.ActiveAt.IsZero() { + return fmt.Errorf("active time missing") + } if a.StartsAt.IsZero() { return fmt.Errorf("start time missing") } + if a.StartsAt.Before(a.ActiveAt) { + return fmt.Errorf("active time must be before start time") + } if !a.EndsAt.IsZero() && a.EndsAt.Before(a.StartsAt) { return fmt.Errorf("start time must be before end time") } diff --git a/model/alert_test.go b/model/alert_test.go index 8910da7a..0de45717 100644 --- a/model/alert_test.go +++ b/model/alert_test.go @@ -23,7 +23,6 @@ import ( func TestAlertValidate(t *testing.T) { ts := time.Now() - var cases = []struct { alert *Alert err string @@ -31,18 +30,27 @@ func TestAlertValidate(t *testing.T) { { alert: &Alert{ Labels: LabelSet{"a": "b"}, - StartsAt: ts, + ActiveAt: ts, + StartsAt: ts.Add(time.Second), }, }, { alert: &Alert{ Labels: LabelSet{"a": "b"}, }, + err: "active time missing", + }, + { + alert: &Alert{ + Labels: LabelSet{"a": "b"}, + ActiveAt: ts, + }, err: "start time missing", }, { alert: &Alert{ Labels: LabelSet{"a": "b"}, + ActiveAt: ts, StartsAt: ts, EndsAt: ts, }, @@ -50,6 +58,15 @@ func TestAlertValidate(t *testing.T) { { alert: &Alert{ Labels: LabelSet{"a": "b"}, + ActiveAt: ts, + StartsAt: ts.Add(time.Second), + EndsAt: ts.Add(1 * time.Minute), + }, + }, + { + alert: &Alert{ + Labels: LabelSet{"a": "b"}, + ActiveAt: ts, StartsAt: ts, EndsAt: ts.Add(1 * time.Minute), }, @@ -57,6 +74,16 @@ func TestAlertValidate(t *testing.T) { { alert: &Alert{ Labels: LabelSet{"a": "b"}, + ActiveAt: ts, + StartsAt: ts.Add(-1 * time.Minute), + EndsAt: ts.Add(-1 * time.Minute), + }, + err: "active time must be before start time", + }, + { + alert: &Alert{ + Labels: LabelSet{"a": "b"}, + ActiveAt: ts, StartsAt: ts, EndsAt: ts.Add(-1 * time.Minute), }, @@ -64,6 +91,7 @@ func TestAlertValidate(t *testing.T) { }, { alert: &Alert{ + ActiveAt: ts, StartsAt: ts, }, err: "at least one label pair required", @@ -71,6 +99,7 @@ func TestAlertValidate(t *testing.T) { { alert: &Alert{ Labels: LabelSet{"a": "b", "!bad": "label"}, + ActiveAt: ts, StartsAt: ts, }, err: "invalid label set: invalid name", @@ -78,6 +107,7 @@ func TestAlertValidate(t *testing.T) { { alert: &Alert{ Labels: LabelSet{"a": "b", "bad": "\xfflabel"}, + ActiveAt: ts, StartsAt: ts, }, err: "invalid label set: invalid value", @@ -86,6 +116,7 @@ func TestAlertValidate(t *testing.T) { alert: &Alert{ Labels: LabelSet{"a": "b"}, Annotations: LabelSet{"!bad": "label"}, + ActiveAt: ts, StartsAt: ts, }, err: "invalid annotations: invalid name", @@ -94,6 +125,7 @@ func TestAlertValidate(t *testing.T) { alert: &Alert{ Labels: LabelSet{"a": "b"}, Annotations: LabelSet{"bad": "\xfflabel"}, + ActiveAt: ts, StartsAt: ts, }, err: "invalid annotations: invalid value",