Alerts: more efficient generation of target labels

Use a label builder instead of a slice when creating labels for the
target alertmanagers. This can be passed directly to
`relabel.ProcessBuilder`, skipping a copy.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham 2023-03-22 12:11:49 +00:00
parent b727e69b76
commit c3f267d862
1 changed files with 11 additions and 9 deletions

View File

@ -701,36 +701,38 @@ func postPath(pre string, v config.AlertmanagerAPIVersion) string {
func AlertmanagerFromGroup(tg *targetgroup.Group, cfg *config.AlertmanagerConfig) ([]alertmanager, []alertmanager, error) { func AlertmanagerFromGroup(tg *targetgroup.Group, cfg *config.AlertmanagerConfig) ([]alertmanager, []alertmanager, error) {
var res []alertmanager var res []alertmanager
var droppedAlertManagers []alertmanager var droppedAlertManagers []alertmanager
lb := labels.NewBuilder(labels.EmptyLabels())
for _, tlset := range tg.Targets { for _, tlset := range tg.Targets {
lbls := make([]labels.Label, 0, len(tlset)+2+len(tg.Labels)) lb.Reset(labels.EmptyLabels())
for ln, lv := range tlset { for ln, lv := range tlset {
lbls = append(lbls, labels.Label{Name: string(ln), Value: string(lv)}) lb.Set(string(ln), string(lv))
} }
// Set configured scheme as the initial scheme label for overwrite. // Set configured scheme as the initial scheme label for overwrite.
lbls = append(lbls, labels.Label{Name: model.SchemeLabel, Value: cfg.Scheme}) lb.Set(model.SchemeLabel, cfg.Scheme)
lbls = append(lbls, labels.Label{Name: pathLabel, Value: postPath(cfg.PathPrefix, cfg.APIVersion)}) lb.Set(pathLabel, postPath(cfg.PathPrefix, cfg.APIVersion))
// Combine target labels with target group labels. // Combine target labels with target group labels.
for ln, lv := range tg.Labels { for ln, lv := range tg.Labels {
if _, ok := tlset[ln]; !ok { if _, ok := tlset[ln]; !ok {
lbls = append(lbls, labels.Label{Name: string(ln), Value: string(lv)}) lb.Set(string(ln), string(lv))
} }
} }
lset, keep := relabel.Process(labels.New(lbls...), cfg.RelabelConfigs...) preRelabel := lb.Labels()
keep := relabel.ProcessBuilder(lb, cfg.RelabelConfigs...)
if !keep { if !keep {
droppedAlertManagers = append(droppedAlertManagers, alertmanagerLabels{labels.New(lbls...)}) droppedAlertManagers = append(droppedAlertManagers, alertmanagerLabels{preRelabel})
continue continue
} }
addr := lset.Get(model.AddressLabel) addr := lb.Get(model.AddressLabel)
if err := config.CheckTargetAddress(model.LabelValue(addr)); err != nil { if err := config.CheckTargetAddress(model.LabelValue(addr)); err != nil {
return nil, nil, err return nil, nil, err
} }
res = append(res, alertmanagerLabels{lset}) res = append(res, alertmanagerLabels{lb.Labels()})
} }
return res, droppedAlertManagers, nil return res, droppedAlertManagers, nil
} }