|
|
|
@@ -107,6 +107,8 @@ func TestProcessTicks(t *testing.T) {
|
|
|
|
|
alertRule1 := gen.With(gen.WithOrgID(mainOrgID), gen.WithInterval(cfg.BaseInterval), gen.WithTitle("rule-1")).GenerateRef()
|
|
|
|
|
ruleStore.PutRule(ctx, alertRule1)
|
|
|
|
|
|
|
|
|
|
folderWithRuleGroup1 := fmt.Sprintf("%s;%s", ruleStore.getNamespaceTitle(alertRule1.NamespaceUID), alertRule1.RuleGroup)
|
|
|
|
|
|
|
|
|
|
t.Run("on 1st tick alert rule should be evaluated", func(t *testing.T) {
|
|
|
|
|
tick = tick.Add(cfg.BaseInterval)
|
|
|
|
|
|
|
|
|
@@ -124,9 +126,9 @@ func TestProcessTicks(t *testing.T) {
|
|
|
|
|
expectedMetric := fmt.Sprintf(
|
|
|
|
|
`# HELP grafana_alerting_rule_group_rules The number of alert rules that are scheduled, both active and paused.
|
|
|
|
|
# TYPE grafana_alerting_rule_group_rules gauge
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",state="active"} 1
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",state="paused"} 0
|
|
|
|
|
`, alertRule1.OrgID)
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",rule_group="%[2]s",state="active"} 1
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",rule_group="%[2]s",state="paused"} 0
|
|
|
|
|
`, alertRule1.OrgID, folderWithRuleGroup1)
|
|
|
|
|
|
|
|
|
|
err := testutil.GatherAndCompare(reg, bytes.NewBufferString(expectedMetric), "grafana_alerting_rule_group_rules")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
@@ -136,6 +138,8 @@ func TestProcessTicks(t *testing.T) {
|
|
|
|
|
alertRule2 := gen.With(gen.WithOrgID(mainOrgID), gen.WithInterval(3*cfg.BaseInterval), gen.WithTitle("rule-2")).GenerateRef()
|
|
|
|
|
ruleStore.PutRule(ctx, alertRule2)
|
|
|
|
|
|
|
|
|
|
folderWithRuleGroup2 := fmt.Sprintf("%s;%s", ruleStore.getNamespaceTitle(alertRule2.NamespaceUID), alertRule2.RuleGroup)
|
|
|
|
|
|
|
|
|
|
t.Run("on 2nd tick first alert rule should be evaluated", func(t *testing.T) {
|
|
|
|
|
tick = tick.Add(cfg.BaseInterval)
|
|
|
|
|
scheduled, stopped, updated := sched.processTick(ctx, dispatcherGroup, tick)
|
|
|
|
@@ -148,13 +152,15 @@ func TestProcessTicks(t *testing.T) {
|
|
|
|
|
assertEvalRun(t, evalAppliedCh, tick, alertRule1.GetKey())
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("after 2nd tick rule metrics should report two active alert rules", func(t *testing.T) {
|
|
|
|
|
t.Run("after 2nd tick rule metrics should report two active alert rules in two groups", func(t *testing.T) {
|
|
|
|
|
expectedMetric := fmt.Sprintf(
|
|
|
|
|
`# HELP grafana_alerting_rule_group_rules The number of alert rules that are scheduled, both active and paused.
|
|
|
|
|
# TYPE grafana_alerting_rule_group_rules gauge
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",state="active"} 2
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",state="paused"} 0
|
|
|
|
|
`, alertRule1.OrgID)
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",rule_group="%[2]s",state="active"} 1
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",rule_group="%[2]s",state="paused"} 0
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",rule_group="%[3]s",state="active"} 1
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",rule_group="%[3]s",state="paused"} 0
|
|
|
|
|
`, alertRule1.OrgID, folderWithRuleGroup1, folderWithRuleGroup2)
|
|
|
|
|
|
|
|
|
|
err := testutil.GatherAndCompare(reg, bytes.NewBufferString(expectedMetric), "grafana_alerting_rule_group_rules")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
@@ -204,13 +210,15 @@ func TestProcessTicks(t *testing.T) {
|
|
|
|
|
assertEvalRun(t, evalAppliedCh, tick, alertRule1.GetKey())
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("after 5th tick rule metrics should report one active and one paused alert rules", func(t *testing.T) {
|
|
|
|
|
t.Run("after 5th tick rule metrics should report one active and one paused alert rules in two groups", func(t *testing.T) {
|
|
|
|
|
expectedMetric := fmt.Sprintf(
|
|
|
|
|
`# HELP grafana_alerting_rule_group_rules The number of alert rules that are scheduled, both active and paused.
|
|
|
|
|
# TYPE grafana_alerting_rule_group_rules gauge
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",state="active"} 1
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",state="paused"} 1
|
|
|
|
|
`, alertRule1.OrgID)
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",rule_group="%[2]s",state="active"} 0
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",rule_group="%[2]s",state="paused"} 1
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",rule_group="%[3]s",state="active"} 1
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",rule_group="%[3]s",state="paused"} 0
|
|
|
|
|
`, alertRule1.OrgID, folderWithRuleGroup1, folderWithRuleGroup2)
|
|
|
|
|
|
|
|
|
|
err := testutil.GatherAndCompare(reg, bytes.NewBufferString(expectedMetric), "grafana_alerting_rule_group_rules")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
@@ -237,13 +245,15 @@ func TestProcessTicks(t *testing.T) {
|
|
|
|
|
assertEvalRun(t, evalAppliedCh, tick, keys...)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("after 6th tick rule metrics should report two paused alert rules", func(t *testing.T) {
|
|
|
|
|
t.Run("after 6th tick rule metrics should report two paused alert rules in two groups", func(t *testing.T) {
|
|
|
|
|
expectedMetric := fmt.Sprintf(
|
|
|
|
|
`# HELP grafana_alerting_rule_group_rules The number of alert rules that are scheduled, both active and paused.
|
|
|
|
|
# TYPE grafana_alerting_rule_group_rules gauge
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",state="active"} 0
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",state="paused"} 2
|
|
|
|
|
`, alertRule1.OrgID)
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",rule_group="%[2]s",state="active"} 0
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",rule_group="%[2]s",state="paused"} 1
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",rule_group="%[3]s",state="active"} 0
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",rule_group="%[3]s",state="paused"} 1
|
|
|
|
|
`, alertRule1.OrgID, folderWithRuleGroup1, folderWithRuleGroup2)
|
|
|
|
|
|
|
|
|
|
err := testutil.GatherAndCompare(reg, bytes.NewBufferString(expectedMetric), "grafana_alerting_rule_group_rules")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
@@ -265,13 +275,15 @@ func TestProcessTicks(t *testing.T) {
|
|
|
|
|
assertEvalRun(t, evalAppliedCh, tick, alertRule1.GetKey())
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("after 7th tick rule metrics should report two active alert rules", func(t *testing.T) {
|
|
|
|
|
t.Run("after 7th tick rule metrics should report two active alert rules in two groups", func(t *testing.T) {
|
|
|
|
|
expectedMetric := fmt.Sprintf(
|
|
|
|
|
`# HELP grafana_alerting_rule_group_rules The number of alert rules that are scheduled, both active and paused.
|
|
|
|
|
# TYPE grafana_alerting_rule_group_rules gauge
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",state="active"} 2
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",state="paused"} 0
|
|
|
|
|
`, alertRule1.OrgID)
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",rule_group="%[2]s",state="active"} 1
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",rule_group="%[2]s",state="paused"} 0
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",rule_group="%[3]s",state="active"} 1
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",rule_group="%[3]s",state="paused"} 0
|
|
|
|
|
`, alertRule1.OrgID, folderWithRuleGroup1, folderWithRuleGroup2)
|
|
|
|
|
|
|
|
|
|
err := testutil.GatherAndCompare(reg, bytes.NewBufferString(expectedMetric), "grafana_alerting_rule_group_rules")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
@@ -295,10 +307,10 @@ func TestProcessTicks(t *testing.T) {
|
|
|
|
|
t.Run("after 8th tick rule metrics should report one active alert rule", func(t *testing.T) {
|
|
|
|
|
expectedMetric := fmt.Sprintf(
|
|
|
|
|
`# HELP grafana_alerting_rule_group_rules The number of alert rules that are scheduled, both active and paused.
|
|
|
|
|
# TYPE grafana_alerting_rule_group_rules gauge
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",state="active"} 1
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",state="paused"} 0
|
|
|
|
|
`, alertRule1.OrgID)
|
|
|
|
|
# TYPE grafana_alerting_rule_group_rules gauge
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",rule_group="%[2]s",state="active"} 1
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",rule_group="%[2]s",state="paused"} 0
|
|
|
|
|
`, alertRule2.OrgID, folderWithRuleGroup2)
|
|
|
|
|
|
|
|
|
|
err := testutil.GatherAndCompare(reg, bytes.NewBufferString(expectedMetric), "grafana_alerting_rule_group_rules")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
@@ -398,6 +410,299 @@ func TestProcessTicks(t *testing.T) {
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSchedule_updateRulesMetrics(t *testing.T) {
|
|
|
|
|
ruleStore := newFakeRulesStore()
|
|
|
|
|
reg := prometheus.NewPedanticRegistry()
|
|
|
|
|
sch := setupScheduler(t, ruleStore, nil, reg, nil, nil)
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
const firstOrgID int64 = 1
|
|
|
|
|
|
|
|
|
|
t.Run("grafana_alerting_rule_group_rules metric should reflect the current state", func(t *testing.T) {
|
|
|
|
|
// Without any rules there are no metrics
|
|
|
|
|
t.Run("it should not show metrics", func(t *testing.T) {
|
|
|
|
|
sch.updateRulesMetrics([]*models.AlertRule{})
|
|
|
|
|
|
|
|
|
|
expectedMetric := ""
|
|
|
|
|
err := testutil.GatherAndCompare(reg, bytes.NewBufferString(expectedMetric), "grafana_alerting_rule_group_rules")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
alertRule1 := models.RuleGen.With(models.RuleGen.WithOrgID(firstOrgID)).GenerateRef()
|
|
|
|
|
folderWithRuleGroup1 := fmt.Sprintf("%s;%s", ruleStore.getNamespaceTitle(alertRule1.NamespaceUID), alertRule1.RuleGroup)
|
|
|
|
|
ruleStore.PutRule(ctx, alertRule1)
|
|
|
|
|
|
|
|
|
|
_, err := sch.updateSchedulableAlertRules(ctx) // to update folderTitles
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
t.Run("it should show one active rule in a single group", func(t *testing.T) {
|
|
|
|
|
sch.updateRulesMetrics([]*models.AlertRule{alertRule1})
|
|
|
|
|
|
|
|
|
|
expectedMetric := fmt.Sprintf(
|
|
|
|
|
`# HELP grafana_alerting_rule_group_rules The number of alert rules that are scheduled, both active and paused.
|
|
|
|
|
# TYPE grafana_alerting_rule_group_rules gauge
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",rule_group="%[2]s",state="active"} 1
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",rule_group="%[2]s",state="paused"} 0
|
|
|
|
|
`, alertRule1.OrgID, folderWithRuleGroup1)
|
|
|
|
|
|
|
|
|
|
err := testutil.GatherAndCompare(reg, bytes.NewBufferString(expectedMetric), "grafana_alerting_rule_group_rules")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Add a new rule alertRule2 and check that it is reflected in the metrics
|
|
|
|
|
alertRule2 := models.RuleGen.With(models.RuleGen.WithOrgID(firstOrgID)).GenerateRef()
|
|
|
|
|
folderWithRuleGroup2 := fmt.Sprintf("%s;%s", ruleStore.getNamespaceTitle(alertRule2.NamespaceUID), alertRule2.RuleGroup)
|
|
|
|
|
ruleStore.PutRule(ctx, alertRule2)
|
|
|
|
|
|
|
|
|
|
_, err = sch.updateSchedulableAlertRules(ctx) // to update folderTitles
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
t.Run("it should show two active rules in two groups", func(t *testing.T) {
|
|
|
|
|
sch.updateRulesMetrics([]*models.AlertRule{alertRule1, alertRule2})
|
|
|
|
|
|
|
|
|
|
expectedMetric := fmt.Sprintf(
|
|
|
|
|
`# HELP grafana_alerting_rule_group_rules The number of alert rules that are scheduled, both active and paused.
|
|
|
|
|
# TYPE grafana_alerting_rule_group_rules gauge
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",rule_group="%[2]s",state="active"} 1
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",rule_group="%[2]s",state="paused"} 0
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",rule_group="%[3]s",state="active"} 1
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",rule_group="%[3]s",state="paused"} 0
|
|
|
|
|
`, alertRule1.OrgID, folderWithRuleGroup1, folderWithRuleGroup2)
|
|
|
|
|
|
|
|
|
|
err := testutil.GatherAndCompare(reg, bytes.NewBufferString(expectedMetric), "grafana_alerting_rule_group_rules")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Now remove the alertRule2
|
|
|
|
|
t.Run("it should show one active rules in one groups", func(t *testing.T) {
|
|
|
|
|
sch.updateRulesMetrics([]*models.AlertRule{alertRule1, alertRule2})
|
|
|
|
|
|
|
|
|
|
expectedMetric := fmt.Sprintf(
|
|
|
|
|
`# HELP grafana_alerting_rule_group_rules The number of alert rules that are scheduled, both active and paused.
|
|
|
|
|
# TYPE grafana_alerting_rule_group_rules gauge
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",rule_group="%[2]s",state="active"} 1
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",rule_group="%[2]s",state="paused"} 0
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",rule_group="%[3]s",state="active"} 1
|
|
|
|
|
grafana_alerting_rule_group_rules{org="%[1]d",rule_group="%[3]s",state="paused"} 0
|
|
|
|
|
`, alertRule1.OrgID, folderWithRuleGroup1, folderWithRuleGroup2)
|
|
|
|
|
|
|
|
|
|
err := testutil.GatherAndCompare(reg, bytes.NewBufferString(expectedMetric), "grafana_alerting_rule_group_rules")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// and remove the alertRule1 so there should be no metrics now
|
|
|
|
|
t.Run("it should show one active rules in one groups", func(t *testing.T) {
|
|
|
|
|
sch.updateRulesMetrics([]*models.AlertRule{})
|
|
|
|
|
|
|
|
|
|
expectedMetric := ""
|
|
|
|
|
err := testutil.GatherAndCompare(reg, bytes.NewBufferString(expectedMetric), "grafana_alerting_rule_group_rules")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("rule_groups metric should reflect the current state", func(t *testing.T) {
|
|
|
|
|
const firstOrgID int64 = 1
|
|
|
|
|
const secondOrgID int64 = 2
|
|
|
|
|
|
|
|
|
|
// Without any rules there are no metrics
|
|
|
|
|
t.Run("it should not show metrics", func(t *testing.T) {
|
|
|
|
|
sch.updateRulesMetrics([]*models.AlertRule{})
|
|
|
|
|
|
|
|
|
|
expectedMetric := ""
|
|
|
|
|
err := testutil.GatherAndCompare(reg, bytes.NewBufferString(expectedMetric), "grafana_alerting_rule_groups")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
alertRule1 := models.RuleGen.With(models.RuleGen.WithOrgID(firstOrgID)).GenerateRef()
|
|
|
|
|
|
|
|
|
|
t.Run("it should show one rule group in a single org", func(t *testing.T) {
|
|
|
|
|
sch.updateRulesMetrics([]*models.AlertRule{alertRule1})
|
|
|
|
|
|
|
|
|
|
expectedMetric := fmt.Sprintf(
|
|
|
|
|
`# HELP grafana_alerting_rule_groups The number of alert rule groups
|
|
|
|
|
# TYPE grafana_alerting_rule_groups gauge
|
|
|
|
|
grafana_alerting_rule_groups{org="%[1]d"} 1
|
|
|
|
|
`, alertRule1.OrgID)
|
|
|
|
|
|
|
|
|
|
err := testutil.GatherAndCompare(reg, bytes.NewBufferString(expectedMetric), "grafana_alerting_rule_groups")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
alertRule2 := models.RuleGen.With(models.RuleGen.WithOrgID(secondOrgID)).GenerateRef()
|
|
|
|
|
|
|
|
|
|
t.Run("it should show two rule groups in two orgs", func(t *testing.T) {
|
|
|
|
|
sch.updateRulesMetrics([]*models.AlertRule{alertRule1, alertRule2})
|
|
|
|
|
|
|
|
|
|
expectedMetric := fmt.Sprintf(
|
|
|
|
|
`# HELP grafana_alerting_rule_groups The number of alert rule groups
|
|
|
|
|
# TYPE grafana_alerting_rule_groups gauge
|
|
|
|
|
grafana_alerting_rule_groups{org="%[1]d"} 1
|
|
|
|
|
grafana_alerting_rule_groups{org="%[2]d"} 1
|
|
|
|
|
`, alertRule1.OrgID, alertRule2.OrgID)
|
|
|
|
|
|
|
|
|
|
err := testutil.GatherAndCompare(reg, bytes.NewBufferString(expectedMetric), "grafana_alerting_rule_groups")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("when the first rule is removed it should show one rule group", func(t *testing.T) {
|
|
|
|
|
sch.updateRulesMetrics([]*models.AlertRule{alertRule2})
|
|
|
|
|
|
|
|
|
|
expectedMetric := fmt.Sprintf(
|
|
|
|
|
`# HELP grafana_alerting_rule_groups The number of alert rule groups
|
|
|
|
|
# TYPE grafana_alerting_rule_groups gauge
|
|
|
|
|
grafana_alerting_rule_groups{org="%[1]d"} 1
|
|
|
|
|
`, alertRule2.OrgID)
|
|
|
|
|
|
|
|
|
|
err := testutil.GatherAndCompare(reg, bytes.NewBufferString(expectedMetric), "grafana_alerting_rule_groups")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("simple_routing_rules metric should reflect the current state", func(t *testing.T) {
|
|
|
|
|
const firstOrgID int64 = 1
|
|
|
|
|
const secondOrgID int64 = 2
|
|
|
|
|
|
|
|
|
|
// Has no NotificationSettings, should not be in the metrics
|
|
|
|
|
alertRuleWithoutNotificationSettings := models.RuleGen.With(
|
|
|
|
|
models.RuleGen.WithOrgID(firstOrgID),
|
|
|
|
|
models.RuleGen.WithNoNotificationSettings(),
|
|
|
|
|
).GenerateRef()
|
|
|
|
|
|
|
|
|
|
// Without any rules there are no metrics
|
|
|
|
|
t.Run("it should not show metrics", func(t *testing.T) {
|
|
|
|
|
sch.updateRulesMetrics([]*models.AlertRule{alertRuleWithoutNotificationSettings})
|
|
|
|
|
|
|
|
|
|
// Because alertRuleWithoutNotificationSettings.orgID is present,
|
|
|
|
|
// the metric is also present but set to 0 because the org has no rules with NotificationSettings.
|
|
|
|
|
expectedMetric := fmt.Sprintf(
|
|
|
|
|
`# HELP grafana_alerting_simple_routing_rules The number of alert rules using simplified routing.
|
|
|
|
|
# TYPE grafana_alerting_simple_routing_rules gauge
|
|
|
|
|
grafana_alerting_simple_routing_rules{org="%[1]d"} 0
|
|
|
|
|
`, alertRuleWithoutNotificationSettings.OrgID)
|
|
|
|
|
err := testutil.GatherAndCompare(reg, bytes.NewBufferString(expectedMetric), "grafana_alerting_simple_routing_rules")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
alertRule1 := models.RuleGen.With(
|
|
|
|
|
models.RuleGen.WithOrgID(firstOrgID),
|
|
|
|
|
models.RuleGen.WithNotificationSettingsGen(models.NotificationSettingsGen()),
|
|
|
|
|
).GenerateRef()
|
|
|
|
|
|
|
|
|
|
t.Run("it should show one rule in a single org", func(t *testing.T) {
|
|
|
|
|
sch.updateRulesMetrics([]*models.AlertRule{alertRuleWithoutNotificationSettings, alertRule1})
|
|
|
|
|
|
|
|
|
|
expectedMetric := fmt.Sprintf(
|
|
|
|
|
`# HELP grafana_alerting_simple_routing_rules The number of alert rules using simplified routing.
|
|
|
|
|
# TYPE grafana_alerting_simple_routing_rules gauge
|
|
|
|
|
grafana_alerting_simple_routing_rules{org="%[1]d"} 1
|
|
|
|
|
`, alertRule1.OrgID)
|
|
|
|
|
|
|
|
|
|
err := testutil.GatherAndCompare(reg, bytes.NewBufferString(expectedMetric), "grafana_alerting_simple_routing_rules")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
alertRule2 := models.RuleGen.With(
|
|
|
|
|
models.RuleGen.WithOrgID(secondOrgID),
|
|
|
|
|
models.RuleGen.WithNotificationSettingsGen(models.NotificationSettingsGen()),
|
|
|
|
|
).GenerateRef()
|
|
|
|
|
|
|
|
|
|
t.Run("it should show two rules in two orgs", func(t *testing.T) {
|
|
|
|
|
sch.updateRulesMetrics([]*models.AlertRule{alertRuleWithoutNotificationSettings, alertRule1, alertRule2})
|
|
|
|
|
|
|
|
|
|
expectedMetric := fmt.Sprintf(
|
|
|
|
|
`# HELP grafana_alerting_simple_routing_rules The number of alert rules using simplified routing.
|
|
|
|
|
# TYPE grafana_alerting_simple_routing_rules gauge
|
|
|
|
|
grafana_alerting_simple_routing_rules{org="%[1]d"} 1
|
|
|
|
|
grafana_alerting_simple_routing_rules{org="%[2]d"} 1
|
|
|
|
|
`, alertRule1.OrgID, alertRule2.OrgID)
|
|
|
|
|
|
|
|
|
|
err := testutil.GatherAndCompare(reg, bytes.NewBufferString(expectedMetric), "grafana_alerting_simple_routing_rules")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("after removing one of the rules it should show one present rule and two org", func(t *testing.T) {
|
|
|
|
|
sch.updateRulesMetrics([]*models.AlertRule{alertRuleWithoutNotificationSettings, alertRule2})
|
|
|
|
|
|
|
|
|
|
// Because alertRuleWithoutNotificationSettings.orgID is present,
|
|
|
|
|
// the metric is also present but set to 0 because the org has no rules with NotificationSettings.
|
|
|
|
|
expectedMetric := fmt.Sprintf(
|
|
|
|
|
`# HELP grafana_alerting_simple_routing_rules The number of alert rules using simplified routing.
|
|
|
|
|
# TYPE grafana_alerting_simple_routing_rules gauge
|
|
|
|
|
grafana_alerting_simple_routing_rules{org="%[1]d"} 0
|
|
|
|
|
grafana_alerting_simple_routing_rules{org="%[2]d"} 1
|
|
|
|
|
`, alertRuleWithoutNotificationSettings.OrgID, alertRule2.OrgID)
|
|
|
|
|
|
|
|
|
|
err := testutil.GatherAndCompare(reg, bytes.NewBufferString(expectedMetric), "grafana_alerting_simple_routing_rules")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("after removing all rules it should not show any metrics", func(t *testing.T) {
|
|
|
|
|
sch.updateRulesMetrics([]*models.AlertRule{})
|
|
|
|
|
|
|
|
|
|
expectedMetric := ""
|
|
|
|
|
err := testutil.GatherAndCompare(reg, bytes.NewBufferString(expectedMetric), "grafana_alerting_simple_routing_rules")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("rule_groups metric should reflect the current state", func(t *testing.T) {
|
|
|
|
|
const firstOrgID int64 = 1
|
|
|
|
|
const secondOrgID int64 = 2
|
|
|
|
|
|
|
|
|
|
// Without any rules there are no metrics
|
|
|
|
|
t.Run("it should not show metrics", func(t *testing.T) {
|
|
|
|
|
sch.updateRulesMetrics([]*models.AlertRule{})
|
|
|
|
|
|
|
|
|
|
expectedMetric := ""
|
|
|
|
|
err := testutil.GatherAndCompare(reg, bytes.NewBufferString(expectedMetric), "grafana_alerting_rule_groups")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
alertRule1 := models.RuleGen.With(models.RuleGen.WithOrgID(firstOrgID)).GenerateRef()
|
|
|
|
|
|
|
|
|
|
t.Run("it should show one rule group in a single org", func(t *testing.T) {
|
|
|
|
|
sch.updateRulesMetrics([]*models.AlertRule{alertRule1})
|
|
|
|
|
|
|
|
|
|
expectedMetric := fmt.Sprintf(
|
|
|
|
|
`# HELP grafana_alerting_rule_groups The number of alert rule groups
|
|
|
|
|
# TYPE grafana_alerting_rule_groups gauge
|
|
|
|
|
grafana_alerting_rule_groups{org="%[1]d"} 1
|
|
|
|
|
`, alertRule1.OrgID)
|
|
|
|
|
|
|
|
|
|
err := testutil.GatherAndCompare(reg, bytes.NewBufferString(expectedMetric), "grafana_alerting_rule_groups")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
alertRule2 := models.RuleGen.With(models.RuleGen.WithOrgID(secondOrgID)).GenerateRef()
|
|
|
|
|
|
|
|
|
|
t.Run("it should show two rule groups in two orgs", func(t *testing.T) {
|
|
|
|
|
sch.updateRulesMetrics([]*models.AlertRule{alertRule1, alertRule2})
|
|
|
|
|
|
|
|
|
|
expectedMetric := fmt.Sprintf(
|
|
|
|
|
`# HELP grafana_alerting_rule_groups The number of alert rule groups
|
|
|
|
|
# TYPE grafana_alerting_rule_groups gauge
|
|
|
|
|
grafana_alerting_rule_groups{org="%[1]d"} 1
|
|
|
|
|
grafana_alerting_rule_groups{org="%[2]d"} 1
|
|
|
|
|
`, alertRule1.OrgID, alertRule2.OrgID)
|
|
|
|
|
|
|
|
|
|
err := testutil.GatherAndCompare(reg, bytes.NewBufferString(expectedMetric), "grafana_alerting_rule_groups")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("when the first rule is removed it should show one rule group", func(t *testing.T) {
|
|
|
|
|
sch.updateRulesMetrics([]*models.AlertRule{alertRule2})
|
|
|
|
|
|
|
|
|
|
expectedMetric := fmt.Sprintf(
|
|
|
|
|
`# HELP grafana_alerting_rule_groups The number of alert rule groups
|
|
|
|
|
# TYPE grafana_alerting_rule_groups gauge
|
|
|
|
|
grafana_alerting_rule_groups{org="%[1]d"} 1
|
|
|
|
|
`, alertRule2.OrgID)
|
|
|
|
|
|
|
|
|
|
err := testutil.GatherAndCompare(reg, bytes.NewBufferString(expectedMetric), "grafana_alerting_rule_groups")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSchedule_deleteAlertRule(t *testing.T) {
|
|
|
|
|
t.Run("when rule exists", func(t *testing.T) {
|
|
|
|
|
t.Run("it should stop evaluation loop and remove the controller from registry", func(t *testing.T) {
|
|
|
|
|