mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: Add alert instance labels to Loki log lines in addition to stream labels (#65403)
Add instance labels to log line
This commit is contained in:
parent
dd04757fc9
commit
de1637afe5
@ -268,6 +268,7 @@ func statesToStreams(rule history_model.RuleMeta, states []state.StateTransition
|
|||||||
Values: valuesAsDataBlob(state.State),
|
Values: valuesAsDataBlob(state.State),
|
||||||
DashboardUID: rule.DashboardUID,
|
DashboardUID: rule.DashboardUID,
|
||||||
PanelID: rule.PanelID,
|
PanelID: rule.PanelID,
|
||||||
|
InstanceLabels: state.Labels,
|
||||||
}
|
}
|
||||||
if state.State.State == eval.Error {
|
if state.State.State == eval.Error {
|
||||||
entry.Error = state.Error.Error()
|
entry.Error = state.Error.Error()
|
||||||
@ -319,6 +320,9 @@ type lokiEntry struct {
|
|||||||
Values *simplejson.Json `json:"values"`
|
Values *simplejson.Json `json:"values"`
|
||||||
DashboardUID string `json:"dashboardUID"`
|
DashboardUID string `json:"dashboardUID"`
|
||||||
PanelID int64 `json:"panelID"`
|
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 {
|
func valuesAsDataBlob(state *state.State) *simplejson.Json {
|
||||||
|
@ -130,6 +130,38 @@ func TestRemoteLokiBackend(t *testing.T) {
|
|||||||
require.NotContains(t, res[0].Stream, "__private__")
|
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) {
|
t.Run("serializes values when regular", func(t *testing.T) {
|
||||||
rule := createTestRule()
|
rule := createTestRule()
|
||||||
l := log.NewNopLogger()
|
l := log.NewNopLogger()
|
||||||
|
Loading…
Reference in New Issue
Block a user