notifier: clone and not reuse LabelSet in AM discovery

This commit is contained in:
Frederic Branczyk 2017-05-17 16:24:22 +02:00
parent b38e977fd8
commit 94e8b43aae
No known key found for this signature in database
GPG Key ID: CA14788B1E48B256
2 changed files with 30 additions and 1 deletions

View File

@ -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
}

View File

@ -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",
}
}