From 9171bf92bb3b4d65a30689800355c730a68899d1 Mon Sep 17 00:00:00 2001 From: William Wernert Date: Thu, 14 Dec 2023 13:06:23 -0500 Subject: [PATCH] Alerting: Add rule ID and title to alert state history Loki entry (#79481) * Add rule ID and title to Loki entry * Combine related tests --- pkg/services/ngalert/state/historian/loki.go | 4 ++++ pkg/services/ngalert/state/historian/loki_test.go | 8 ++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/pkg/services/ngalert/state/historian/loki.go b/pkg/services/ngalert/state/historian/loki.go index f5392fb709a..5275531e806 100644 --- a/pkg/services/ngalert/state/historian/loki.go +++ b/pkg/services/ngalert/state/historian/loki.go @@ -255,6 +255,8 @@ func statesToStream(rule history_model.RuleMeta, states []state.StateTransition, DashboardUID: rule.DashboardUID, PanelID: rule.PanelID, Fingerprint: labelFingerprint(sanitizedLabels), + RuleTitle: rule.Title, + RuleID: rule.ID, RuleUID: rule.UID, InstanceLabels: sanitizedLabels, } @@ -300,6 +302,8 @@ type lokiEntry struct { DashboardUID string `json:"dashboardUID"` PanelID int64 `json:"panelID"` Fingerprint string `json:"fingerprint"` + RuleTitle string `json:"ruleTitle"` + RuleID int64 `json:"ruleID"` RuleUID string `json:"ruleUID"` // InstanceLabels is exactly the set of labels associated with the alert instance in Alertmanager. // These should not be conflated with labels associated with log streams. diff --git a/pkg/services/ngalert/state/historian/loki_test.go b/pkg/services/ngalert/state/historian/loki_test.go index 586ecaa5cf8..02971c8c0bb 100644 --- a/pkg/services/ngalert/state/historian/loki_test.go +++ b/pkg/services/ngalert/state/historian/loki_test.go @@ -89,7 +89,7 @@ func TestRemoteLokiBackend(t *testing.T) { require.NotContains(t, res.Stream, "__private__") }) - t.Run("includes ruleUID in log line", func(t *testing.T) { + t.Run("includes rule data in log line", func(t *testing.T) { rule := createTestRule() l := log.NewNopLogger() states := singleFromNormal(&state.State{ @@ -98,8 +98,10 @@ func TestRemoteLokiBackend(t *testing.T) { }) res := statesToStream(rule, states, nil, l) - entry := requireSingleEntry(t, res) + + require.Equal(t, rule.Title, entry.RuleTitle) + require.Equal(t, rule.ID, entry.RuleID) require.Equal(t, rule.UID, entry.RuleUID) }) @@ -525,11 +527,13 @@ func singleFromNormal(st *state.State) []state.StateTransition { func createTestRule() history_model.RuleMeta { return history_model.RuleMeta{ OrgID: 1, + ID: 123, UID: "rule-uid", Group: "my-group", NamespaceUID: "my-folder", DashboardUID: "dash-uid", PanelID: 123, + Title: "my-title", } }