Alerting: Metric to count imported from Prometheus rules (#100847)

This commit is contained in:
Alexander Akhmetov
2025-03-05 14:02:28 +01:00
committed by GitHub
parent 2638de6aeb
commit d44728f4e5
9 changed files with 161 additions and 22 deletions
@@ -457,9 +457,9 @@ func grafanaRuleGroupToPrometheus(group string, rules []models.AlertRule) (apimo
}
for i, rule := range rules {
promDefinition := rule.PrometheusRuleDefinition()
if promDefinition == "" {
return apimodels.PrometheusRuleGroup{}, fmt.Errorf("failed to get the Prometheus definition of the rule with UID %s", rule.UID)
promDefinition, err := rule.PrometheusRuleDefinition()
if err != nil {
return apimodels.PrometheusRuleGroup{}, fmt.Errorf("failed to get the Prometheus definition of the rule with UID %s: %w", rule.UID, err)
}
var r apimodels.PrometheusRule
if err := yaml.Unmarshal([]byte(promDefinition), &r); err != nil {
@@ -115,7 +115,10 @@ func TestRouteConvertPrometheusPostRuleGroup(t *testing.T) {
require.Equal(t, fmt.Sprintf("[%s] %s", simpleGroup.Name, simpleGroup.Rules[0].Alert), remaining[0].Title)
promRuleYAML, err := yaml.Marshal(simpleGroup.Rules[0])
require.NoError(t, err)
require.Equal(t, string(promRuleYAML), remaining[0].PrometheusRuleDefinition())
promDefinition, err := remaining[0].PrometheusRuleDefinition()
require.NoError(t, err)
require.Equal(t, string(promRuleYAML), promDefinition)
})
t.Run("should fail to replace a provisioned rule group", func(t *testing.T) {