Skip to content

Commit

Permalink
Alerting: Add alert instance labels to Loki log lines in addition to …
Browse files Browse the repository at this point in the history
…stream labels (#65403)

Add instance labels to log line
  • Loading branch information
alexweav committed Mar 28, 2023
1 parent dd04757 commit de1637a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
16 changes: 10 additions & 6 deletions pkg/services/ngalert/state/historian/loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,13 @@ func statesToStreams(rule history_model.RuleMeta, states []state.StateTransition
repr := string(lblJsn)

entry := lokiEntry{
SchemaVersion: 1,
Previous: state.PreviousFormatted(),
Current: state.Formatted(),
Values: valuesAsDataBlob(state.State),
DashboardUID: rule.DashboardUID,
PanelID: rule.PanelID,
SchemaVersion: 1,
Previous: state.PreviousFormatted(),
Current: state.Formatted(),
Values: valuesAsDataBlob(state.State),
DashboardUID: rule.DashboardUID,
PanelID: rule.PanelID,
InstanceLabels: state.Labels,
}
if state.State.State == eval.Error {
entry.Error = state.Error.Error()
Expand Down Expand Up @@ -319,6 +320,9 @@ type lokiEntry struct {
Values *simplejson.Json `json:"values"`
DashboardUID string `json:"dashboardUID"`
PanelID int64 `json:"panelID"`
// 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.
InstanceLabels map[string]string `json:"labels"`
}

func valuesAsDataBlob(state *state.State) *simplejson.Json {
Expand Down
32 changes: 32 additions & 0 deletions pkg/services/ngalert/state/historian/loki_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,38 @@ func TestRemoteLokiBackend(t *testing.T) {
require.NotContains(t, res[0].Stream, "__private__")
})

t.Run("includes instance labels in log line", func(t *testing.T) {
rule := createTestRule()
l := log.NewNopLogger()
states := singleFromNormal(&state.State{
State: eval.Alerting,
Labels: data.Labels{"statelabel": "labelvalue"},
})

res := statesToStreams(rule, states, nil, l)

entry := requireSingleEntry(t, res)
require.Contains(t, entry.InstanceLabels, "statelabel")
})

t.Run("does not include labels other than instance labels in log line", func(t *testing.T) {
rule := createTestRule()
l := log.NewNopLogger()
states := singleFromNormal(&state.State{
State: eval.Alerting,
Labels: data.Labels{
"statelabel": "labelvalue",
"labeltwo": "labelvalue",
"labelthree": "labelvalue",
},
})

res := statesToStreams(rule, states, nil, l)

entry := requireSingleEntry(t, res)
require.Len(t, entry.InstanceLabels, 3)
})

t.Run("serializes values when regular", func(t *testing.T) {
rule := createTestRule()
l := log.NewNopLogger()
Expand Down

0 comments on commit de1637a

Please sign in to comment.