Alerts: more efficient relabel on Send

Re-use `labels.Builder` and use `relabel.ProcessBuilder` to skip a
conversion step.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
pull/12171/head
Bryan Boreham 2023-03-22 12:26:17 +00:00
parent c3f267d862
commit 3711339a7d
1 changed files with 15 additions and 18 deletions

View File

@ -349,19 +349,6 @@ func (n *Manager) Send(alerts ...*Alert) {
n.mtx.Lock() n.mtx.Lock()
defer n.mtx.Unlock() defer n.mtx.Unlock()
// Attach external labels before relabelling and sending.
for _, a := range alerts {
lb := labels.NewBuilder(a.Labels)
n.opts.ExternalLabels.Range(func(l labels.Label) {
if a.Labels.Get(l.Name) == "" {
lb.Set(l.Name, l.Value)
}
})
a.Labels = lb.Labels()
}
alerts = n.relabelAlerts(alerts) alerts = n.relabelAlerts(alerts)
if len(alerts) == 0 { if len(alerts) == 0 {
return return
@ -390,15 +377,25 @@ func (n *Manager) Send(alerts ...*Alert) {
n.setMore() n.setMore()
} }
// Attach external labels and process relabelling rules.
func (n *Manager) relabelAlerts(alerts []*Alert) []*Alert { func (n *Manager) relabelAlerts(alerts []*Alert) []*Alert {
lb := labels.NewBuilder(labels.EmptyLabels())
var relabeledAlerts []*Alert var relabeledAlerts []*Alert
for _, alert := range alerts { for _, a := range alerts {
labels, keep := relabel.Process(alert.Labels, n.opts.RelabelConfigs...) lb.Reset(a.Labels)
if keep { n.opts.ExternalLabels.Range(func(l labels.Label) {
alert.Labels = labels if a.Labels.Get(l.Name) == "" {
relabeledAlerts = append(relabeledAlerts, alert) lb.Set(l.Name, l.Value)
}
})
keep := relabel.ProcessBuilder(lb, n.opts.RelabelConfigs...)
if !keep {
continue
} }
a.Labels = lb.Labels()
relabeledAlerts = append(relabeledAlerts, a)
} }
return relabeledAlerts return relabeledAlerts
} }