Alerting: Capture refID of rule's condition expression in Loki state history entries (#66419)

* Capture condition from rule

* Add test
This commit is contained in:
Alexander Weaver 2023-04-18 14:21:28 -05:00 committed by GitHub
parent a21fdd9c81
commit cf7157f683
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 0 deletions

View File

@ -260,6 +260,7 @@ func statesToStream(rule history_model.RuleMeta, states []state.StateTransition,
Previous: state.PreviousFormatted(),
Current: state.Formatted(),
Values: valuesAsDataBlob(state.State),
Condition: rule.Condition,
DashboardUID: rule.DashboardUID,
PanelID: rule.PanelID,
InstanceLabels: removePrivateLabels(state.Labels),
@ -302,6 +303,7 @@ type lokiEntry struct {
Current string `json:"current"`
Error string `json:"error,omitempty"`
Values *simplejson.Json `json:"values"`
Condition string `json:"condition"`
DashboardUID string `json:"dashboardUID"`
PanelID int64 `json:"panelID"`
// InstanceLabels is exactly the set of labels associated with the alert instance in Alertmanager.

View File

@ -139,6 +139,21 @@ func TestRemoteLokiBackend(t *testing.T) {
require.InDelta(t, 2.0, entry.Values.Get("A").MustFloat64(), 1e-4)
require.InDelta(t, 5.5, entry.Values.Get("B").MustFloat64(), 1e-4)
})
t.Run("captures condition from rule", func(t *testing.T) {
rule := createTestRule()
rule.Condition = "some-condition"
l := log.NewNopLogger()
states := singleFromNormal(&state.State{
State: eval.Alerting,
Labels: data.Labels{"a": "b"},
})
res := statesToStream(rule, states, nil, l)
entry := requireSingleEntry(t, res)
require.Equal(t, rule.Condition, entry.Condition)
})
})
t.Run("selector string", func(t *testing.T) {

View File

@ -19,6 +19,7 @@ type RuleMeta struct {
NamespaceUID string
DashboardUID string
PanelID int64
Condition string
}
func NewRuleMeta(r *models.AlertRule, log log.Logger) RuleMeta {
@ -43,6 +44,7 @@ func NewRuleMeta(r *models.AlertRule, log log.Logger) RuleMeta {
NamespaceUID: r.NamespaceUID,
DashboardUID: dashUID,
PanelID: panelID,
Condition: r.Condition,
}
}