Fix mutation of active alert elements by notifier (#2656)

This caused the external label application in the notifier to bleed back
into the rule manager's active alerting elements.
This commit is contained in:
Julius Volz 2017-04-26 10:29:42 -05:00 committed by GitHub
parent 5248118b10
commit fe11c5933a
2 changed files with 25 additions and 0 deletions

View File

@ -301,6 +301,8 @@ func (r *AlertingRule) currentAlerts() []*Alert {
for _, a := range r.active {
anew := *a
anew.Labels = anew.Labels.Clone()
anew.Annotations = anew.Annotations.Clone()
alerts = append(alerts, &anew)
}
return alerts

View File

@ -37,3 +37,26 @@ func TestAlertingRuleHTMLSnippet(t *testing.T) {
t.Fatalf("incorrect HTML snippet; want:\n\n|%v|\n\ngot:\n\n|%v|", want, got)
}
}
func TestCurrentAlertsClonesLabelsAndAnnotations(t *testing.T) {
r := AlertingRule{
active: map[model.Fingerprint]*Alert{
0: &Alert{
Labels: model.LabelSet{"test_label": "test_label_value"},
Annotations: model.LabelSet{"test_annotation": "test_annotation_value"},
},
},
}
alerts := r.currentAlerts()
alerts[0].Labels["test_label"] = "new_label_value"
alerts[0].Annotations["test_annotation"] = "new_annotation_value"
alerts = r.currentAlerts()
if want, got := model.LabelValue("test_label_value"), alerts[0].Labels["test_label"]; want != got {
t.Fatalf("unexpected label value; want %q, got %q", want, got)
}
if want, got := model.LabelValue("test_annotation_value"), alerts[0].Annotations["test_annotation"]; want != got {
t.Fatalf("unexpected annotation value; want %q, got %q", want, got)
}
}