[release-12.0.2] Alerting: Fix group-level labels and query_offset in the import API (#106392)

Alerting: Fix group-level labels and query_offset in the import API (#106379)

What is this feature?

Fixes a bug when group-level query_offset and labels parameters are ignored and not saved

Why do we need this feature?

In the import API Prometheus YAML rule definitions are supported:

groups:
  - name: group-1
    interval: 1m
    query_offset: 10m
    labels:
      severity: "warning"
    rules:
      - alert: Alert 0 > 0
        expr: vector(0) > 0

But applying group-level labels and query_offset is broken and they are not saved right now because during the conversion of the API model to PrometheusRuleGroup they aren't saved to the new structure.

(cherry picked from commit f7a52bc04e)
This commit is contained in:
Alexander Akhmetov
2025-06-06 12:04:17 +02:00
committed by GitHub
parent d6b7bb6759
commit c53915cb87
11 changed files with 174 additions and 84 deletions
@@ -2,6 +2,7 @@ package alerting
import (
"encoding/json"
"maps"
"net/http"
"testing"
"time"
@@ -1195,3 +1196,69 @@ func TestIntegrationConvertPrometheusEndpoints_Delete(t *testing.T) {
runTest(t, true)
})
}
func TestIntegrationConvertPrometheusEndpoints_GroupLabels(t *testing.T) {
testinfra.SQLiteIntegrationTest(t)
dir, gpath := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{
DisableLegacyAlerting: true,
EnableUnifiedAlerting: true,
DisableAnonymous: true,
AppModeProduction: true,
EnableRecordingRules: true,
})
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, gpath)
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
apiClient := newAlertingApiClient(grafanaListedAddr, "admin", "admin")
ds := apiClient.CreateDatasource(t, datasources.DS_PROMETHEUS)
testGroup := apimodels.PrometheusRuleGroup{
Name: "test-group-with-labels",
Interval: prommodel.Duration(60 * time.Second),
Labels: map[string]string{
"group_label": "value-1",
},
Rules: []apimodels.PrometheusRule{
{
Alert: "TestAlert",
Expr: "up == 0",
For: util.Pointer(prommodel.Duration(2 * time.Minute)),
Labels: map[string]string{
"rule_label": "value-2",
},
Annotations: map[string]string{
"annotation-1": "annotation-value",
},
},
},
}
namespace := "test-namespace-1"
namespaceUID := util.GenerateShortUID()
apiClient.CreateFolder(t, namespaceUID, namespace)
apiClient.ConvertPrometheusPostRuleGroup(t, namespace, ds.Body.Datasource.UID, testGroup, nil)
expectedLabels := make(map[string]string)
maps.Copy(expectedLabels, testGroup.Labels)
maps.Copy(expectedLabels, testGroup.Rules[0].Labels)
// Verify the Import API returns the expected merged format
group := apiClient.ConvertPrometheusGetRuleGroupRules(t, namespace, testGroup.Name, nil)
testGroup.Labels = nil
testGroup.Rules[0].Labels = expectedLabels
require.Equal(t, testGroup, group)
ruleGroup, _, _ := apiClient.GetRulesGroupWithStatus(t, namespaceUID, testGroup.Name)
require.Len(t, ruleGroup.Rules, 1)
rule := ruleGroup.Rules[0]
require.Equal(t, expectedLabels, rule.Labels)
}