Alerting: Include rules imported in the UI into prometheus_imported_rules metric (#106229)

This commit is contained in:
Alexander Akhmetov
2025-06-02 12:47:09 +02:00
committed by GitHub
parent 31a9ddc47a
commit 6ff67722b8
2 changed files with 17 additions and 9 deletions
+2 -1
View File
@@ -88,7 +88,8 @@ func (sch *schedule) updateRulesMetrics(alertRules []*models.AlertRule) {
}
}
if rule.ImportedFromPrometheus() {
_, hasConvertedPrometheusRuleLabel := rule.GetLabels()[models.ConvertedPrometheusRuleLabel]
if rule.ImportedFromPrometheus() || hasConvertedPrometheusRuleLabel {
orgsRulesPrometheusImported[rule.OrgID]++
}
@@ -729,38 +729,45 @@ func TestSchedule_updateRulesMetrics(t *testing.T) {
require.NoError(t, err)
})
// The metric includes alert rules with either internal ConvertedPrometheusRuleLabel label,
// or when AlertRule.ImportedFromPrometheus() returns true.
alertRule1 := models.RuleGen.With(
models.RuleGen.WithOrgID(firstOrgID),
models.RuleGen.WithPrometheusOriginalRuleDefinition("1"),
).GenerateRef()
t.Run("it should show one imported rule in a single org", func(t *testing.T) {
sch.updateRulesMetrics([]*models.AlertRule{alertRule1})
alertRule2 := models.RuleGen.With(
models.RuleGen.WithOrgID(firstOrgID),
models.RuleGen.WithLabel(models.ConvertedPrometheusRuleLabel, "true"),
).GenerateRef()
t.Run("it should show two imported rules in a single org", func(t *testing.T) {
sch.updateRulesMetrics([]*models.AlertRule{alertRule1, alertRule2})
expectedMetric := fmt.Sprintf(
`# HELP grafana_alerting_prometheus_imported_rules The number of rules imported from a Prometheus-compatible source.
# TYPE grafana_alerting_prometheus_imported_rules gauge
grafana_alerting_prometheus_imported_rules{org="%[1]d"} 1
grafana_alerting_prometheus_imported_rules{org="%[1]d"} 2
`, alertRule1.OrgID)
err := testutil.GatherAndCompare(reg, bytes.NewBufferString(expectedMetric), "grafana_alerting_prometheus_imported_rules")
require.NoError(t, err)
})
alertRule2 := models.RuleGen.With(
alertRule3 := models.RuleGen.With(
models.RuleGen.WithOrgID(secondOrgID),
models.RuleGen.WithPrometheusOriginalRuleDefinition("1"),
).GenerateRef()
t.Run("it should show two imported rules in two orgs", func(t *testing.T) {
sch.updateRulesMetrics([]*models.AlertRule{alertRule1, alertRule2})
t.Run("it should show three imported rules in two orgs", func(t *testing.T) {
sch.updateRulesMetrics([]*models.AlertRule{alertRule1, alertRule2, alertRule3})
expectedMetric := fmt.Sprintf(
`# HELP grafana_alerting_prometheus_imported_rules The number of rules imported from a Prometheus-compatible source.
# TYPE grafana_alerting_prometheus_imported_rules gauge
grafana_alerting_prometheus_imported_rules{org="%[1]d"} 1
grafana_alerting_prometheus_imported_rules{org="%[1]d"} 2
grafana_alerting_prometheus_imported_rules{org="%[2]d"} 1
`, alertRule1.OrgID, alertRule2.OrgID)
`, firstOrgID, secondOrgID)
err := testutil.GatherAndCompare(reg, bytes.NewBufferString(expectedMetric), "grafana_alerting_prometheus_imported_rules")
require.NoError(t, err)