fix(alert): prevent persisted rule crash loops

This commit is contained in:
naiba committed 2026-09-02 13:30:46 +00:00
1 parent 8f1147c800
commit 19daf4ad3c
8 files changed
+479 -71

No files matched your search

+16 -4
View File
@@ -26,7 +26,7 @@ func testCycleRules(t *testing.T) {
rule: &AlertRule{
Rules: []*Rule{
{
Type: "_cycle",
Type: "transfer_in_cycle",
},
},
},
@@ -39,7 +39,7 @@ func testCycleRules(t *testing.T) {
rule: &AlertRule{
Rules: []*Rule{
{
Type: "_cycle",
Type: "transfer_in_cycle",
},
},
},
@@ -130,6 +130,7 @@ func testGeneralRules(t *testing.T) {
rule: &AlertRule{
Rules: []*Rule{
{
Type: "cpu",
Duration: 10,
},
},
@@ -143,6 +144,7 @@ func testGeneralRules(t *testing.T) {
rule: &AlertRule{
Rules: []*Rule{
{
Type: "cpu",
Duration: 10,
},
},
@@ -156,6 +158,7 @@ func testGeneralRules(t *testing.T) {
rule: &AlertRule{
Rules: []*Rule{
{
Type: "cpu",
Duration: 10,
},
},
@@ -169,6 +172,7 @@ func testGeneralRules(t *testing.T) {
rule: &AlertRule{
Rules: []*Rule{
{
Type: "cpu",
Duration: 10,
},
},
@@ -182,6 +186,7 @@ func testGeneralRules(t *testing.T) {
rule: &AlertRule{
Rules: []*Rule{
{
Type: "cpu",
Duration: 10,
},
},
@@ -210,6 +215,7 @@ func testCombinedRules(t *testing.T) {
Duration: 10,
},
{
Type: "cpu",
Duration: 10,
},
},
@@ -227,6 +233,7 @@ func testCombinedRules(t *testing.T) {
Duration: 10,
},
{
Type: "cpu",
Duration: 10,
},
},
@@ -240,6 +247,7 @@ func testCombinedRules(t *testing.T) {
rule: &AlertRule{
Rules: []*Rule{
{
Type: "cpu",
Duration: 10,
},
{
@@ -257,9 +265,11 @@ func testCombinedRules(t *testing.T) {
rule: &AlertRule{
Rules: []*Rule{
{
Type: "cpu",
Duration: 10,
},
{
Type: "memory",
Duration: 30,
},
},
@@ -273,9 +283,11 @@ func testCombinedRules(t *testing.T) {
rule: &AlertRule{
Rules: []*Rule{
{
Type: "cpu",
Duration: 10,
},
{
Type: "memory",
Duration: 30,
},
},
@@ -416,7 +428,7 @@ func TestAlertRule_RetentionWindow(t *testing.T) {
{"zero duration only", &AlertRule{Rules: []*Rule{{Type: "cpu", Duration: 0}}}, 0},
{"mixed picks max", &AlertRule{Rules: []*Rule{{Type: "cpu", Duration: 0}, {Type: "cpu", Duration: 7}}}, 7},
{"offline keeps Duration", &AlertRule{Rules: []*Rule{{Type: "offline", Duration: 30}}}, 30},
{"cycle looks back one", &AlertRule{Rules: []*Rule{{Type: "net_in_speed_cycle"}}}, 1},
{"cycle looks back one", &AlertRule{Rules: []*Rule{{Type: "transfer_in_cycle"}}}, 1},
}
for _, c := range cases {
if got := c.rule.RetentionWindow(); got != c.want {
@@ -469,7 +481,7 @@ func TestAlertRule_CombinedRuleAccumulatesSamples(t *testing.T) {
}{
{"general3+general10", &AlertRule{Rules: []*Rule{{Type: "cpu", Duration: 3}, {Type: "memory", Duration: 10}}}, []bool{false, false}, 9, 10},
{"offline5+general10", &AlertRule{Rules: []*Rule{{Type: "offline", Duration: 5}, {Type: "cpu", Duration: 10}}}, []bool{false, false}, 9, 10},
{"transfer+general8", &AlertRule{Rules: []*Rule{{Type: "net_in_speed_cycle"}, {Type: "cpu", Duration: 8}}}, []bool{false, false}, 7, 8},
{"transfer+general8", &AlertRule{Rules: []*Rule{{Type: "transfer_in_cycle"}, {Type: "cpu", Duration: 8}}}, []bool{false, false}, 7, 8},
{"offline3+offline12", &AlertRule{Rules: []*Rule{{Type: "offline", Duration: 3}, {Type: "offline", Duration: 12}}}, []bool{false, false}, 11, 12},
}
for _, c := range cases {