Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature:add active time in alert.go #502

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions model/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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")
}
Expand Down
36 changes: 34 additions & 2 deletions model/alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,61 +23,91 @@ import (

func TestAlertValidate(t *testing.T) {
ts := time.Now()

var cases = []struct {
alert *Alert
err string
}{
{
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,
},
},
{
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),
},
},
{
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),
},
err: "start time must be before end time",
},
{
alert: &Alert{
ActiveAt: ts,
StartsAt: ts,
},
err: "at least one label pair required",
},
{
alert: &Alert{
Labels: LabelSet{"a": "b", "!bad": "label"},
ActiveAt: ts,
StartsAt: ts,
},
err: "invalid label set: invalid name",
},
{
alert: &Alert{
Labels: LabelSet{"a": "b", "bad": "\xfflabel"},
ActiveAt: ts,
StartsAt: ts,
},
err: "invalid label set: invalid value",
Expand All @@ -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",
Expand All @@ -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",
Expand Down