From 94e8b43aaeac879069681c9c1482bd016f128465 Mon Sep 17 00:00:00 2001 From: Frederic Branczyk Date: Wed, 17 May 2017 16:24:22 +0200 Subject: [PATCH] notifier: clone and not reuse LabelSet in AM discovery --- notifier/notifier.go | 4 +++- notifier/notifier_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/notifier/notifier.go b/notifier/notifier.go index ecb28f489..238d51fdb 100644 --- a/notifier/notifier.go +++ b/notifier/notifier.go @@ -500,6 +500,7 @@ func alertmanagerFromGroup(tg *config.TargetGroup, cfg *config.AlertmanagerConfi var res []alertmanager for _, lset := range tg.Targets { + lset = lset.Clone() // Set configured scheme as the initial scheme label for overwrite. lset[model.SchemeLabel] = model.LabelValue(cfg.Scheme) lset[pathLabel] = model.LabelValue(postPath(cfg.PathPrefix)) @@ -510,7 +511,8 @@ func alertmanagerFromGroup(tg *config.TargetGroup, cfg *config.AlertmanagerConfi lset[ln] = lv } } - lset := relabel.Process(lset, cfg.RelabelConfigs...) + + lset = relabel.Process(lset, cfg.RelabelConfigs...) if lset == nil { continue } diff --git a/notifier/notifier_test.go b/notifier/notifier_test.go index 3587a1a8a..9190d0a8f 100644 --- a/notifier/notifier_test.go +++ b/notifier/notifier_test.go @@ -418,3 +418,30 @@ type alertmanagerMock struct { func (a alertmanagerMock) url() string { return a.urlf() } + +func TestLabelSetNotReused(t *testing.T) { + tg := makeInputTargetGroup() + _, err := alertmanagerFromGroup(tg, &config.AlertmanagerConfig{}) + if err != nil { + t.Fatal(err) + } + + if !reflect.DeepEqual(tg, makeInputTargetGroup()) { + t.Fatal("Target modified during alertmanager extraction") + } +} + +func makeInputTargetGroup() *config.TargetGroup { + return &config.TargetGroup{ + Targets: []model.LabelSet{ + model.LabelSet{ + model.AddressLabel: model.LabelValue("1.1.1.1:9090"), + model.LabelName("notcommon1"): model.LabelValue("label"), + }, + }, + Labels: model.LabelSet{ + model.LabelName("common"): model.LabelValue("label"), + }, + Source: "testsource", + } +}