From eeadfccdc56eb17938b6827b96bacb7966666cfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=82=96=E8=B4=9D=E8=B4=9D?= Date: Sun, 26 Apr 2020 18:30:10 +0800 Subject: [PATCH] fix: fix vmalert template label not complete bug (#435) Co-authored-by: xiaobeibei --- app/vmalert/rule.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/app/vmalert/rule.go b/app/vmalert/rule.go index c51d4ce1c..b68538937 100644 --- a/app/vmalert/rule.go +++ b/app/vmalert/rule.go @@ -171,15 +171,31 @@ func (r *Rule) newAlert(m datasource.Metric) (*notifier.Alert, error) { Start: time.Now(), // TODO: support End time } + + // 1. use data labels for _, l := range m.Labels { a.Labels[l.Name] = l.Value } + + // 2. template rule labels with data labels + rLabels, err := a.ExecTemplate(r.Labels) + if err != nil { + return a, err + } + + // 3. merge data labels and rule labels // metric labels may be overridden by // rule labels - for k, v := range r.Labels { + for k, v := range rLabels { a.Labels[k] = v } - var err error + + // 4. template merged labels + a.Labels, err = a.ExecTemplate(a.Labels) + if err != nil { + return a, err + } + a.Annotations, err = a.ExecTemplate(r.Annotations) return a, err }