Skip to content

Commit

Permalink
Merge pull request prometheus#2731 from brancz/lset-non-cloned
Browse files Browse the repository at this point in the history
notifier: clone and not reuse LabelSet in AM discovery
  • Loading branch information
brancz authored May 18, 2017
2 parents b38e977 + 94e8b43 commit b916b37
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
4 changes: 3 additions & 1 deletion notifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
}
Expand Down
27 changes: 27 additions & 0 deletions notifier/notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
}

0 comments on commit b916b37

Please sign in to comment.