From aad7d3f9643efa46f9135e47a1a7b63289110abb Mon Sep 17 00:00:00 2001 From: liuqing Date: Fri, 7 Jul 2023 14:24:08 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feature=EF=BC=9Aadd=20active=20time=20in=20?= =?UTF-8?q?alert.go?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/alert.go | 8 ++++++++ model/alert_test.go | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) 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..050179d1 100644 --- a/model/alert_test.go +++ b/model/alert_test.go @@ -31,18 +31,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 +59,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 +75,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 +92,7 @@ func TestAlertValidate(t *testing.T) { }, { alert: &Alert{ + ActiveAt: ts, StartsAt: ts, }, err: "at least one label pair required", @@ -71,6 +100,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 +108,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 +117,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 +126,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", From 0af8c5d495ecbf4f68ee9d59729c26a07c8a463d Mon Sep 17 00:00:00 2001 From: liuqing Date: Fri, 7 Jul 2023 14:29:16 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feature=EF=BC=9Aadd=20active=20time=20in=20?= =?UTF-8?q?alert.go;=20Signed-off-by:=20liuqing=20<540596035@qq.com>?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/alert_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/model/alert_test.go b/model/alert_test.go index 050179d1..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