diff --git a/alert_generator/testsuite/cases/case_new_alerts_and_order_check.go b/alert_generator/testsuite/cases/case_new_alerts_and_order_check.go index e64a4f0..b0db22b 100644 --- a/alert_generator/testsuite/cases/case_new_alerts_and_order_check.go +++ b/alert_generator/testsuite/cases/case_new_alerts_and_order_check.go @@ -94,7 +94,7 @@ func (tc *newAlertsAndOrderCheck) RuleGroup() (rulefmt.RuleGroup, error) { Expr: r2Expr, Labels: map[string]string{"foo": "baz", "ba_dum": "tss", "rulegroup": tc.groupName}, // The expression also has alertname. So this template variable should result in r1AlertName. - Annotations: map[string]string{"description": "Based on ALERTS. Old alertname was {{$labels.alertname}}. foo was {{$labels.foo}}."}, + Annotations: map[string]string{"description": "Based on ALERTS. Old alertname was {{$labels.alertname}}. foo was {{.Labels.foo}}."}, }, }, }, nil @@ -297,7 +297,7 @@ func (tc *newAlertsAndOrderCheck) expRuleGroups(ts int64) (expRgs []v1.RuleGroup Name: tc.r2AlertName, Query: tc.r2Query, Labels: labels.FromStrings("foo", "baz", "ba_dum", "tss", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "Based on ALERTS. Old alertname was {{$labels.alertname}}. foo was {{$labels.foo}}."), + Annotations: labels.FromStrings("description", "Based on ALERTS. Old alertname was {{$labels.alertname}}. foo was {{.Labels.foo}}."), Alerts: a2, Health: "ok", Type: "alerting", @@ -490,13 +490,19 @@ func (tc *newAlertsAndOrderCheck) ExpectedAlerts() []ExpectedAlert { resendDelayMs := int64(ResendDelay / time.Millisecond) + orderingID := 0 + addAlert := func(ea ExpectedAlert) { + orderingID++ + ea.OrderingID = orderingID + exp = append(exp, ea) + } + // r11. _20th := 20 * int64(tc.rwInterval/time.Millisecond) // Firing. _73rd := 73 * int64(tc.rwInterval/time.Millisecond) // Resolved. _73rdPlus15m := _73rd + int64(15*time.Minute/time.Millisecond) for ts := _20th; ts < _73rd; ts += resendDelayMs { - exp = append(exp, ExpectedAlert{ - OrderingID: int(ts), + addAlert(ExpectedAlert{ TimeTolerance: tc.groupInterval, Ts: timestamp.Time(tc.zeroTime + ts), Resolved: false, @@ -521,8 +527,7 @@ func (tc *newAlertsAndOrderCheck) ExpectedAlerts() []ExpectedAlert { // based on this first resolved alert. tolerance = 2 * tc.groupInterval } - exp = append(exp, ExpectedAlert{ - OrderingID: int(ts), + addAlert(ExpectedAlert{ TimeTolerance: tolerance, Ts: timestamp.Time(tc.zeroTime + ts), Resolved: true, @@ -543,8 +548,7 @@ func (tc *newAlertsAndOrderCheck) ExpectedAlerts() []ExpectedAlert { _65thPlus15m := _65th + int64(15*time.Minute/time.Millisecond) //_8th_plus_gi := _8th + int64(tc.groupInterval/time.Millisecond) // Small for firing. for ts := _44th; ts < _65th; ts += resendDelayMs { - exp = append(exp, ExpectedAlert{ - OrderingID: int(ts), + addAlert(ExpectedAlert{ TimeTolerance: tc.groupInterval, Ts: timestamp.Time(tc.zeroTime + ts), Resolved: false, @@ -560,8 +564,7 @@ func (tc *newAlertsAndOrderCheck) ExpectedAlerts() []ExpectedAlert { }) // r2. - exp = append(exp, ExpectedAlert{ - OrderingID: int(ts), + addAlert(ExpectedAlert{ TimeTolerance: tc.groupInterval, Ts: timestamp.Time(tc.zeroTime + ts), Resolved: false, @@ -586,8 +589,7 @@ func (tc *newAlertsAndOrderCheck) ExpectedAlerts() []ExpectedAlert { // based on this first resolved alert. tolerance = 2 * tc.groupInterval } - exp = append(exp, ExpectedAlert{ - OrderingID: int(ts), + addAlert(ExpectedAlert{ TimeTolerance: tolerance, Ts: timestamp.Time(tc.zeroTime + ts), Resolved: true, @@ -601,8 +603,7 @@ func (tc *newAlertsAndOrderCheck) ExpectedAlerts() []ExpectedAlert { }, }) - exp = append(exp, ExpectedAlert{ - OrderingID: int(ts), + addAlert(ExpectedAlert{ TimeTolerance: tolerance, Ts: timestamp.Time(tc.zeroTime + ts), Resolved: true, diff --git a/alert_generator/testsuite/cases/case_pending_firing_resolved.go b/alert_generator/testsuite/cases/case_pending_firing_resolved.go index 7725110..f2d593d 100644 --- a/alert_generator/testsuite/cases/case_pending_firing_resolved.go +++ b/alert_generator/testsuite/cases/case_pending_firing_resolved.go @@ -83,7 +83,7 @@ func (tc *pendingAndFiringAndResolved) RuleGroup() (rulefmt.RuleGroup, error) { Labels: map[string]string{"foo": "bar", "rulegroup": tc.groupName}, Annotations: map[string]string{ "description": "SimpleAlert is firing", - "summary": "The value is {{$value}}", + "summary": "The value is {{$value}} {{.Value}}", }, }, }, @@ -162,7 +162,7 @@ func (tc *pendingAndFiringAndResolved) expAlerts(ts int64, alerts []v1.Alert) (e expAlerts = append(expAlerts, []v1.Alert{ { Labels: labels.FromStrings("alertname", tc.alertName, "foo", "bar", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 11"), + Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 11 11"), State: "pending", Value: "11", ActiveAt: &activeAt, @@ -178,7 +178,7 @@ func (tc *pendingAndFiringAndResolved) expAlerts(ts int64, alerts []v1.Alert) (e expAlerts = append(expAlerts, []v1.Alert{ { Labels: labels.FromStrings("alertname", tc.alertName, "foo", "bar", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 15"), + Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 15 15"), State: "pending", Value: "15", ActiveAt: &aa, @@ -194,7 +194,7 @@ func (tc *pendingAndFiringAndResolved) expAlerts(ts int64, alerts []v1.Alert) (e expAlerts = append(expAlerts, []v1.Alert{ { Labels: labels.FromStrings("alertname", tc.alertName, "foo", "bar", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 15"), + Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 15 15"), State: "firing", Value: "15", ActiveAt: &aa, @@ -206,7 +206,7 @@ func (tc *pendingAndFiringAndResolved) expAlerts(ts int64, alerts []v1.Alert) (e expAlerts = append(expAlerts, []v1.Alert{ { Labels: labels.FromStrings("alertname", tc.alertName, "foo", "bar", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 19"), + Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 19 19"), State: "firing", Value: "19", ActiveAt: &activeAt, @@ -238,7 +238,7 @@ func (tc *pendingAndFiringAndResolved) expRuleGroups(ts int64) (expRgs []v1.Rule Query: tc.query, Duration: float64(time.Duration(tc.forDuration) / time.Second), Labels: labels.FromStrings("foo", "bar", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is {{$value}}"), + Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is {{$value}} {{.Value}}"), Alerts: alerts, Health: "ok", Type: "alerting", @@ -254,7 +254,7 @@ func (tc *pendingAndFiringAndResolved) expRuleGroups(ts int64) (expRgs []v1.Rule expRgs = append(expRgs, getRg("pending", []*v1.Alert{ { Labels: labels.FromStrings("alertname", tc.alertName, "foo", "bar", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 11"), + Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 11 11"), State: "pending", Value: "11", ActiveAt: &activeAt, @@ -269,7 +269,7 @@ func (tc *pendingAndFiringAndResolved) expRuleGroups(ts int64) (expRgs []v1.Rule expRgs = append(expRgs, getRg("pending", []*v1.Alert{ { Labels: labels.FromStrings("alertname", tc.alertName, "foo", "bar", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 15"), + Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 15 15"), State: "pending", Value: "15", ActiveAt: &aa, @@ -284,7 +284,7 @@ func (tc *pendingAndFiringAndResolved) expRuleGroups(ts int64) (expRgs []v1.Rule expRgs = append(expRgs, getRg("firing", []*v1.Alert{ { Labels: labels.FromStrings("alertname", tc.alertName, "foo", "bar", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 15"), + Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 15 15"), State: "firing", Value: "15", ActiveAt: &aa, @@ -295,7 +295,7 @@ func (tc *pendingAndFiringAndResolved) expRuleGroups(ts int64) (expRgs []v1.Rule expRgs = append(expRgs, getRg("firing", []*v1.Alert{ { Labels: labels.FromStrings("alertname", tc.alertName, "foo", "bar", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 19"), + Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 19 19"), State: "firing", Value: "19", ActiveAt: &activeAt, @@ -379,10 +379,16 @@ func (tc *pendingAndFiringAndResolved) ExpectedAlerts() []ExpectedAlert { endsAtDelta = 4 * tc.groupInterval } + orderingID := 0 + addAlert := func(ea ExpectedAlert) { + orderingID++ + ea.OrderingID = orderingID + exp = append(exp, ea) + } + resendDelayMs := int64(ResendDelay / time.Millisecond) for ts := _32nd; ts < _53rd; ts += resendDelayMs { - exp = append(exp, ExpectedAlert{ - OrderingID: int(ts), + addAlert(ExpectedAlert{ TimeTolerance: tc.groupInterval, Ts: timestamp.Time(tc.zeroTime + ts), Resolved: false, @@ -392,15 +398,14 @@ func (tc *pendingAndFiringAndResolved) ExpectedAlerts() []ExpectedAlert { EndsAtDelta: endsAtDelta, Alert: ¬ifier.Alert{ Labels: labels.FromStrings("alertname", tc.alertName, "foo", "bar", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 15"), + Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 15 15"), StartsAt: timestamp.Time(tc.zeroTime + _32nd), }, }) } // Value change. for ts := _53rd; ts < _69th; ts += resendDelayMs { - exp = append(exp, ExpectedAlert{ - OrderingID: int(ts), + addAlert(ExpectedAlert{ TimeTolerance: tc.groupInterval, Ts: timestamp.Time(tc.zeroTime + ts), Resolved: false, @@ -410,7 +415,7 @@ func (tc *pendingAndFiringAndResolved) ExpectedAlerts() []ExpectedAlert { EndsAtDelta: endsAtDelta, Alert: ¬ifier.Alert{ Labels: labels.FromStrings("alertname", tc.alertName, "foo", "bar", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 19"), + Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 19 19"), StartsAt: timestamp.Time(tc.zeroTime + _32nd), }, }) @@ -426,8 +431,7 @@ func (tc *pendingAndFiringAndResolved) ExpectedAlerts() []ExpectedAlert { // based on this first resolved alert. tolerance = 2 * tc.groupInterval } - exp = append(exp, ExpectedAlert{ - OrderingID: int(ts), + addAlert(ExpectedAlert{ TimeTolerance: tolerance, Ts: timestamp.Time(tc.zeroTime + ts), Resolved: true, @@ -437,7 +441,7 @@ func (tc *pendingAndFiringAndResolved) ExpectedAlerts() []ExpectedAlert { EndsAtDelta: endsAtDelta, Alert: ¬ifier.Alert{ Labels: labels.FromStrings("alertname", tc.alertName, "foo", "bar", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 19"), + Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 19 19"), StartsAt: timestamp.Time(tc.zeroTime + _32nd), }, }) @@ -445,8 +449,7 @@ func (tc *pendingAndFiringAndResolved) ExpectedAlerts() []ExpectedAlert { // Firing again. for ts := _113th; ts < _134th; ts += resendDelayMs { - exp = append(exp, ExpectedAlert{ - OrderingID: int(ts), + addAlert(ExpectedAlert{ TimeTolerance: tc.groupInterval, Ts: timestamp.Time(tc.zeroTime + ts), Resolved: false, @@ -456,7 +459,7 @@ func (tc *pendingAndFiringAndResolved) ExpectedAlerts() []ExpectedAlert { EndsAtDelta: endsAtDelta, Alert: ¬ifier.Alert{ Labels: labels.FromStrings("alertname", tc.alertName, "foo", "bar", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 15"), + Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 15 15"), StartsAt: timestamp.Time(tc.zeroTime + _113th), }, }) @@ -472,8 +475,7 @@ func (tc *pendingAndFiringAndResolved) ExpectedAlerts() []ExpectedAlert { // based on this first resolved alert. tolerance = 2 * tc.groupInterval } - exp = append(exp, ExpectedAlert{ - OrderingID: int(ts), + addAlert(ExpectedAlert{ TimeTolerance: tolerance, Ts: timestamp.Time(tc.zeroTime + ts), Resolved: true, @@ -482,7 +484,7 @@ func (tc *pendingAndFiringAndResolved) ExpectedAlerts() []ExpectedAlert { EndsAtDelta: endsAtDelta, Alert: ¬ifier.Alert{ Labels: labels.FromStrings("alertname", tc.alertName, "foo", "bar", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 15"), + Annotations: labels.FromStrings("description", "SimpleAlert is firing", "summary", "The value is 15 15"), StartsAt: timestamp.Time(tc.zeroTime + _113th), }, }) diff --git a/alert_generator/testsuite/cases/case_zero_for_and_small_for.go b/alert_generator/testsuite/cases/case_zero_for_and_small_for.go index 7645772..981a99b 100644 --- a/alert_generator/testsuite/cases/case_zero_for_and_small_for.go +++ b/alert_generator/testsuite/cases/case_zero_for_and_small_for.go @@ -85,17 +85,23 @@ func (tc *zeroAndSmallFor) RuleGroup() (rulefmt.RuleGroup, error) { Interval: model.Duration(tc.groupInterval), Rules: []rulefmt.RuleNode{ { // Zero for. - Alert: zfAlert, - Expr: zfExpr, - Labels: map[string]string{"foo": "bar", "rulegroup": tc.groupName}, - Annotations: map[string]string{"description": "This should immediately fire"}, + Alert: zfAlert, + Expr: zfExpr, + Labels: map[string]string{"foo": "bar", "rulegroup": tc.groupName}, + Annotations: map[string]string{ + "description": "This should immediately fire", + "template_test": "{{humanize 1048576}} {{humanize1024 1048576}} {{humanizeDuration 135.3563}} {{humanizePercentage 0.959}} {{humanizeTimestamp 1643114203}}", + }, }, { // Small for. - Alert: sfAlert, - Expr: sfExpr, - For: tc.forDuration, - Labels: map[string]string{"ba_dum": "tss", "rulegroup": tc.groupName}, - Annotations: map[string]string{"description": "This should fire after an interval"}, + Alert: sfAlert, + Expr: sfExpr, + For: tc.forDuration, + Labels: map[string]string{"ba_dum": "tss", "rulegroup": tc.groupName}, + Annotations: map[string]string{ + "description": "This should fire after an interval", + "template_test": `{{title "this part"}} {{toUpper "is testing"}} {{toLower "THE STRINGS"}}. {{if match "[0-9]+" "1234"}}{{reReplaceAll "r.*d" "replaced" "rpld text"}}{{end}}. {{if match "[0-9]+$" "1234a"}}WRONG{{end}}.`, + }, }, }, }, nil @@ -170,14 +176,14 @@ func (tc *zeroAndSmallFor) expAlerts(ts int64, alerts []v1.Alert) (expAlerts [][ expAlerts = append(expAlerts, []v1.Alert{ { Labels: labels.FromStrings("alertname", tc.zfAlertName, "foo", "bar", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "This should immediately fire"), + Annotations: labels.FromStrings("description", "This should immediately fire", "template_test", "1.049M 1Mi 2m 15s 95.9% 2022-01-25 12:36:43 +0000 UTC"), State: "firing", Value: "15", ActiveAt: &activeAt, }, { Labels: labels.FromStrings("alertname", tc.sfAlertName, "ba_dum", "tss", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "This should fire after an interval"), + Annotations: labels.FromStrings("description", "This should fire after an interval", "template_test", "This Part IS TESTING the strings. replaced text. ."), State: "pending", Value: "15", ActiveAt: &activeAt, @@ -189,14 +195,14 @@ func (tc *zeroAndSmallFor) expAlerts(ts int64, alerts []v1.Alert) (expAlerts [][ expAlerts = append(expAlerts, []v1.Alert{ { Labels: labels.FromStrings("alertname", tc.zfAlertName, "foo", "bar", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "This should immediately fire"), + Annotations: labels.FromStrings("description", "This should immediately fire", "template_test", "1.049M 1Mi 2m 15s 95.9% 2022-01-25 12:36:43 +0000 UTC"), State: "firing", Value: "15", ActiveAt: &activeAt, }, { Labels: labels.FromStrings("alertname", tc.sfAlertName, "ba_dum", "tss", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "This should fire after an interval"), + Annotations: labels.FromStrings("description", "This should fire after an interval", "template_test", "This Part IS TESTING the strings. replaced text. ."), State: "firing", Value: "15", ActiveAt: &activeAt, @@ -208,7 +214,7 @@ func (tc *zeroAndSmallFor) expAlerts(ts int64, alerts []v1.Alert) (expAlerts [][ expAlerts = append(expAlerts, []v1.Alert{ { Labels: labels.FromStrings("alertname", tc.zfAlertName, "foo", "bar", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "This should immediately fire"), + Annotations: labels.FromStrings("description", "This should immediately fire", "template_test", "1.049M 1Mi 2m 15s 95.9% 2022-01-25 12:36:43 +0000 UTC"), State: "firing", Value: "11", ActiveAt: &activeAt2, @@ -235,25 +241,31 @@ func (tc *zeroAndSmallFor) expRuleGroups(ts int64) (expRgs []v1.RuleGroup) { Interval: float64(tc.groupInterval / time.Second), Rules: []v1.Rule{ v1.AlertingRule{ - State: s1, - Name: tc.zfAlertName, - Query: tc.zfQuery, - Labels: labels.FromStrings("foo", "bar", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "This should immediately fire"), - Alerts: a1, - Health: "ok", - Type: "alerting", + State: s1, + Name: tc.zfAlertName, + Query: tc.zfQuery, + Labels: labels.FromStrings("foo", "bar", "rulegroup", tc.groupName), + Annotations: labels.FromStrings( + "description", "This should immediately fire", + "template_test", "{{humanize 1048576}} {{humanize1024 1048576}} {{humanizeDuration 135.3563}} {{humanizePercentage 0.959}} {{humanizeTimestamp 1643114203}}", + ), + Alerts: a1, + Health: "ok", + Type: "alerting", }, v1.AlertingRule{ - State: s2, - Name: tc.sfAlertName, - Query: tc.sfQuery, - Duration: float64(time.Duration(tc.forDuration) / time.Second), - Labels: labels.FromStrings("ba_dum", "tss", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "This should fire after an interval"), - Alerts: a2, - Health: "ok", - Type: "alerting", + State: s2, + Name: tc.sfAlertName, + Query: tc.sfQuery, + Duration: float64(time.Duration(tc.forDuration) / time.Second), + Labels: labels.FromStrings("ba_dum", "tss", "rulegroup", tc.groupName), + Annotations: labels.FromStrings( + "description", "This should fire after an interval", + "template_test", `{{title "this part"}} {{toUpper "is testing"}} {{toLower "THE STRINGS"}}. {{if match "[0-9]+" "1234"}}{{reReplaceAll "r.*d" "replaced" "rpld text"}}{{end}}. {{if match "[0-9]+$" "1234a"}}WRONG{{end}}.`, + ), + Alerts: a2, + Health: "ok", + Type: "alerting", }, }, } @@ -267,7 +279,7 @@ func (tc *zeroAndSmallFor) expRuleGroups(ts int64) (expRgs []v1.RuleGroup) { []*v1.Alert{ { Labels: labels.FromStrings("alertname", tc.zfAlertName, "foo", "bar", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "This should immediately fire"), + Annotations: labels.FromStrings("description", "This should immediately fire", "template_test", "1.049M 1Mi 2m 15s 95.9% 2022-01-25 12:36:43 +0000 UTC"), State: "firing", Value: "15", ActiveAt: &activeAt, @@ -276,7 +288,7 @@ func (tc *zeroAndSmallFor) expRuleGroups(ts int64) (expRgs []v1.RuleGroup) { []*v1.Alert{ { Labels: labels.FromStrings("alertname", tc.sfAlertName, "ba_dum", "tss", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "This should fire after an interval"), + Annotations: labels.FromStrings("description", "This should fire after an interval", "template_test", "This Part IS TESTING the strings. replaced text. ."), State: "pending", Value: "15", ActiveAt: &activeAt, @@ -289,7 +301,7 @@ func (tc *zeroAndSmallFor) expRuleGroups(ts int64) (expRgs []v1.RuleGroup) { []*v1.Alert{ { Labels: labels.FromStrings("alertname", tc.zfAlertName, "foo", "bar", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "This should immediately fire"), + Annotations: labels.FromStrings("description", "This should immediately fire", "template_test", "1.049M 1Mi 2m 15s 95.9% 2022-01-25 12:36:43 +0000 UTC"), State: "firing", Value: "15", ActiveAt: &activeAt, @@ -298,7 +310,7 @@ func (tc *zeroAndSmallFor) expRuleGroups(ts int64) (expRgs []v1.RuleGroup) { []*v1.Alert{ { Labels: labels.FromStrings("alertname", tc.sfAlertName, "ba_dum", "tss", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "This should fire after an interval"), + Annotations: labels.FromStrings("description", "This should fire after an interval", "template_test", "This Part IS TESTING the strings. replaced text. ."), State: "firing", Value: "15", ActiveAt: &activeAt, @@ -311,7 +323,7 @@ func (tc *zeroAndSmallFor) expRuleGroups(ts int64) (expRgs []v1.RuleGroup) { []*v1.Alert{ { Labels: labels.FromStrings("alertname", tc.zfAlertName, "foo", "bar", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "This should immediately fire"), + Annotations: labels.FromStrings("description", "This should immediately fire", "template_test", "1.049M 1Mi 2m 15s 95.9% 2022-01-25 12:36:43 +0000 UTC"), State: "firing", Value: "11", ActiveAt: &activeAt2, @@ -403,12 +415,17 @@ func (tc *zeroAndSmallFor) ExpectedAlerts() []ExpectedAlert { endsAtDelta = 4 * tc.groupInterval } - resendDelayMs := int64(ResendDelay / time.Millisecond) + orderingID := 0 + addAlert := func(ea ExpectedAlert) { + orderingID++ + ea.OrderingID = orderingID + exp = append(exp, ea) + } + resendDelayMs := int64(ResendDelay / time.Millisecond) // Zero for. for ts := _8th; ts < _21st; ts += resendDelayMs { - exp = append(exp, ExpectedAlert{ - OrderingID: int(ts), + addAlert(ExpectedAlert{ TimeTolerance: tc.groupInterval, Ts: timestamp.Time(tc.zeroTime + ts), Resolved: false, @@ -418,7 +435,7 @@ func (tc *zeroAndSmallFor) ExpectedAlerts() []ExpectedAlert { EndsAtDelta: endsAtDelta, Alert: ¬ifier.Alert{ Labels: labels.FromStrings("alertname", tc.zfAlertName, "foo", "bar", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "This should immediately fire"), + Annotations: labels.FromStrings("description", "This should immediately fire", "template_test", "1.049M 1Mi 2m 15s 95.9% 2022-01-25 12:36:43 +0000 UTC"), StartsAt: timestamp.Time(tc.zeroTime + _8th), }, }) @@ -433,8 +450,7 @@ func (tc *zeroAndSmallFor) ExpectedAlerts() []ExpectedAlert { // based on this first resolved alert. tolerance = 2 * tc.groupInterval } - exp = append(exp, ExpectedAlert{ - OrderingID: int(ts), + addAlert(ExpectedAlert{ TimeTolerance: tolerance, Ts: timestamp.Time(tc.zeroTime + ts), Resolved: true, @@ -444,14 +460,13 @@ func (tc *zeroAndSmallFor) ExpectedAlerts() []ExpectedAlert { EndsAtDelta: endsAtDelta, Alert: ¬ifier.Alert{ Labels: labels.FromStrings("alertname", tc.zfAlertName, "foo", "bar", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "This should immediately fire"), + Annotations: labels.FromStrings("description", "This should immediately fire", "template_test", "1.049M 1Mi 2m 15s 95.9% 2022-01-25 12:36:43 +0000 UTC"), StartsAt: timestamp.Time(tc.zeroTime + _8th), }, }) } for ts := _93rd; ts < _106th; ts += resendDelayMs { - exp = append(exp, ExpectedAlert{ - OrderingID: int(ts), + addAlert(ExpectedAlert{ TimeTolerance: tc.groupInterval, Ts: timestamp.Time(tc.zeroTime + ts), Resolved: false, @@ -461,7 +476,7 @@ func (tc *zeroAndSmallFor) ExpectedAlerts() []ExpectedAlert { EndsAtDelta: endsAtDelta, Alert: ¬ifier.Alert{ Labels: labels.FromStrings("alertname", tc.zfAlertName, "foo", "bar", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "This should immediately fire"), + Annotations: labels.FromStrings("description", "This should immediately fire", "template_test", "1.049M 1Mi 2m 15s 95.9% 2022-01-25 12:36:43 +0000 UTC"), StartsAt: timestamp.Time(tc.zeroTime + _93rd), }, }) @@ -476,8 +491,7 @@ func (tc *zeroAndSmallFor) ExpectedAlerts() []ExpectedAlert { // based on this first resolved alert. tolerance = 2 * tc.groupInterval } - exp = append(exp, ExpectedAlert{ - OrderingID: int(ts), + addAlert(ExpectedAlert{ TimeTolerance: tolerance, Ts: timestamp.Time(tc.zeroTime + ts), Resolved: true, @@ -486,7 +500,7 @@ func (tc *zeroAndSmallFor) ExpectedAlerts() []ExpectedAlert { EndsAtDelta: endsAtDelta, Alert: ¬ifier.Alert{ Labels: labels.FromStrings("alertname", tc.zfAlertName, "foo", "bar", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "This should immediately fire"), + Annotations: labels.FromStrings("description", "This should immediately fire", "template_test", "1.049M 1Mi 2m 15s 95.9% 2022-01-25 12:36:43 +0000 UTC"), StartsAt: timestamp.Time(tc.zeroTime + _93rd), }, }) @@ -494,8 +508,7 @@ func (tc *zeroAndSmallFor) ExpectedAlerts() []ExpectedAlert { // Small for. for ts := _8th_plus_gi; ts < _21st; ts += resendDelayMs { - exp = append(exp, ExpectedAlert{ - OrderingID: int(ts), + addAlert(ExpectedAlert{ TimeTolerance: tc.groupInterval, Ts: timestamp.Time(tc.zeroTime + ts), Resolved: false, @@ -505,7 +518,7 @@ func (tc *zeroAndSmallFor) ExpectedAlerts() []ExpectedAlert { EndsAtDelta: endsAtDelta, Alert: ¬ifier.Alert{ Labels: labels.FromStrings("alertname", tc.sfAlertName, "ba_dum", "tss", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "This should fire after an interval"), + Annotations: labels.FromStrings("description", "This should fire after an interval", "template_test", "This Part IS TESTING the strings. replaced text. ."), StartsAt: timestamp.Time(tc.zeroTime + _8th_plus_gi), }, }) @@ -520,8 +533,7 @@ func (tc *zeroAndSmallFor) ExpectedAlerts() []ExpectedAlert { // based on this first resolved alert. tolerance = 2 * tc.groupInterval } - exp = append(exp, ExpectedAlert{ - OrderingID: int(ts), + addAlert(ExpectedAlert{ TimeTolerance: tolerance, Ts: timestamp.Time(tc.zeroTime + ts), Resolved: true, @@ -530,7 +542,7 @@ func (tc *zeroAndSmallFor) ExpectedAlerts() []ExpectedAlert { EndsAtDelta: endsAtDelta, Alert: ¬ifier.Alert{ Labels: labels.FromStrings("alertname", tc.sfAlertName, "ba_dum", "tss", "rulegroup", tc.groupName), - Annotations: labels.FromStrings("description", "This should fire after an interval"), + Annotations: labels.FromStrings("description", "This should fire after an interval", "template_test", "This Part IS TESTING the strings. replaced text. ."), StartsAt: timestamp.Time(tc.zeroTime + _8th_plus_gi), }, }) diff --git a/alert_generator/testsuite/rules.yaml b/alert_generator/testsuite/rules.yaml index 8439bca..b7a9eb2 100755 --- a/alert_generator/testsuite/rules.yaml +++ b/alert_generator/testsuite/rules.yaml @@ -10,7 +10,7 @@ groups: rulegroup: PendingAndFiringAndResolved annotations: description: SimpleAlert is firing - summary: The value is {{$value}} + summary: The value is {{$value}} {{.Value}} - name: PendingAndResolved_AlwaysInactive interval: 10s rules: @@ -40,6 +40,7 @@ groups: rulegroup: ZeroFor_SmallFor annotations: description: This should immediately fire + template_test: '{{humanize 1048576}} {{humanize1024 1048576}} {{humanizeDuration 135.3563}} {{humanizePercentage 0.959}} {{humanizeTimestamp 1643114203}}' - alert: ZeroFor_SmallFor_SmallFor expr: '{__name__="alert_generator_test_suite", alertname="ZeroFor_SmallFor_SmallFor", rulegroup="ZeroFor_SmallFor"} > 13' for: 5s @@ -48,6 +49,7 @@ groups: rulegroup: ZeroFor_SmallFor annotations: description: This should fire after an interval + template_test: '{{title "this part"}} {{toUpper "is testing"}} {{toLower "THE STRINGS"}}. {{if match "[0-9]+" "1234"}}{{reReplaceAll "r.*d" "replaced" "rpld text"}}{{end}}. {{if match "[0-9]+$" "1234a"}}WRONG{{end}}.' - name: NewAlerts_OrderCheck interval: 10s rules: @@ -66,4 +68,4 @@ groups: foo: baz rulegroup: NewAlerts_OrderCheck annotations: - description: Based on ALERTS. Old alertname was {{$labels.alertname}}. foo was {{$labels.foo}}. + description: Based on ALERTS. Old alertname was {{$labels.alertname}}. foo was {{.Labels.foo}}. diff --git a/alert_generator/testsuite/server.go b/alert_generator/testsuite/server.go index 2630511..ea14ab8 100644 --- a/alert_generator/testsuite/server.go +++ b/alert_generator/testsuite/server.go @@ -3,6 +3,7 @@ package testsuite import ( "encoding/json" "fmt" + "github.com/prometheus/prometheus/model/labels" "io/ioutil" "net/http" "sort" @@ -147,6 +148,7 @@ func (as *alertsServer) ServeHTTP(res http.ResponseWriter, req *http.Request) { continue } lastResendWasIgnored = false + fmt.Println("Missed 1") missedAlerts = append(missedAlerts, ma) } else { lastResendWasIgnored = ma.Resend @@ -167,12 +169,15 @@ func (as *alertsServer) ServeHTTP(res http.ResponseWriter, req *http.Request) { // between the first alert sent and Nth resend can be upto N*(ResendDelay+GroupInterval), and not N*ResendDelay. // Hence, we adjust our time expectation for the next alert if it is supposed to be a resend. Outer2: - for id, _ := range success { + for id, sa := range success { eas := as.expectedAlerts[id] if len(eas.alerts) == 0 { continue } for i := range eas.alerts { + if labels.Compare(eas.alerts[i].Alert.Labels, sa.Alert.Labels) != 0 { + continue + } if !eas.alerts[i].Resend { continue Outer2 } @@ -234,6 +239,7 @@ func (as *alertsServer) getPossibleAlert(now time.Time, lblsString string) []cas continue } if ea.Ts.Add(ea.TimeTolerance + (2 * cases.MaxRTT)).Before(now) { + fmt.Println("Missed 2") if !ea.CanBeIgnored() { missedAlerts = append(missedAlerts, ea) } diff --git a/alert_generator/testsuite/testsuite.go b/alert_generator/testsuite/testsuite.go index 3b3ee6f..cfbc793 100644 --- a/alert_generator/testsuite/testsuite.go +++ b/alert_generator/testsuite/testsuite.go @@ -459,12 +459,13 @@ func (ts *TestSuite) WasTestSuccessful() (yes bool, describe string) { if ma.Resolved { state = "resolved" } - describe += fmt.Sprintf("\t\t%d: Expected time: %s, Labels: %s, Annotations: %s, State: %s\n", + describe += fmt.Sprintf("\t\t%d: Expected time: %s, Labels: %s, Annotations: %s, State: %s, Resend: %t\n", i+1, ma.Ts.Format(time.RFC3339Nano), ma.Alert.Labels.String(), ma.Alert.Annotations.String(), state, + ma.Resend, ) } }