diff --git a/app/vmalert/web_types.go b/app/vmalert/web_types.go index 639236591..31423b8f2 100644 --- a/app/vmalert/web_types.go +++ b/app/vmalert/web_types.go @@ -215,8 +215,10 @@ func recordingToAPI(rr *rule.RecordingRule) apiRule { Updates: rule.GetAllRuleState(rr), // encode as strings to avoid rounding - ID: fmt.Sprintf("%d", rr.ID()), - GroupID: fmt.Sprintf("%d", rr.GroupID), + ID: fmt.Sprintf("%d", rr.ID()), + GroupID: fmt.Sprintf("%d", rr.GroupID), + GroupName: rr.GroupName, + File: rr.File, } if lastState.Err != nil { r.LastError = lastState.Err.Error() diff --git a/app/vmalert/web_types_test.go b/app/vmalert/web_types_test.go index 40f5404a3..61574c170 100644 --- a/app/vmalert/web_types_test.go +++ b/app/vmalert/web_types_test.go @@ -1,9 +1,56 @@ package main import ( + "fmt" + "github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/config" + "github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/datasource" + "github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/rule" + "reflect" "testing" ) +func TestRecordingToApi(t *testing.T) { + fq := &datasource.FakeQuerier{} + fq.Add(datasource.Metric{ + Values: []float64{1}, Timestamps: []int64{0}, + }) + g := &rule.Group{ + Name: "group", + File: "rules.yaml", + Concurrency: 1, + } + + entriesLimit := 44 + rr := rule.NewRecordingRule(fq, g, config.Rule{ + ID: 1248, + Record: "record_name", + Expr: "up", + Labels: map[string]string{"label": "value"}, + UpdateEntriesLimit: &entriesLimit, + }) + + expectedRes := apiRule{ + Name: "record_name", + Query: "up", + Labels: map[string]string{"label": "value"}, + Health: "ok", + Type: ruleTypeRecording, + DatasourceType: "prometheus", + ID: "1248", + GroupID: fmt.Sprintf("%d", g.ID()), + GroupName: "group", + File: "rules.yaml", + MaxUpdates: 44, + Updates: make([]rule.StateEntry, 0), + } + + res := recordingToAPI(rr) + + if !reflect.DeepEqual(res, expectedRes) { + t.Fatalf("expected to have: \n%v;\ngot: \n%v", expectedRes, res) + } +} + func TestUrlValuesToStrings(t *testing.T) { mapQueryParams := map[string][]string{ "param1": {"param1"}, diff --git a/docs/changelog/CHANGELOG.md b/docs/changelog/CHANGELOG.md index 4dfc658a7..50d3fb9cf 100644 --- a/docs/changelog/CHANGELOG.md +++ b/docs/changelog/CHANGELOG.md @@ -18,6 +18,8 @@ See also [LTS releases](https://docs.victoriametrics.com/lts-releases/). ## tip +* BUGFIX: [vmalert](https://docs.victoriametrics.com/vmalert): properly set `group_name` and `file` fields for recording rules in `/api/v1/rules`. + ## [v1.105.0](https://github.com/VictoriaMetrics/VictoriaMetrics/releases/tag/v1.105.0) Released at 2024-10-21