mirror of
https://github.com/prometheus/prometheus.git
synced 2024-11-12 16:44:05 -08:00
Merge pull request #12171 from bboreham/notifier-builder
Alerts: more efficient relabeling
This commit is contained in:
commit
a0e1525978
|
@ -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
|
||||||
}
|
}
|
||||||
|
@ -701,36 +698,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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue