fix issue#3053 (#3182)

vmalert: prevent duplicating label `alertname` for notifications

The issue has no impact on alerting procedure. But still needs to be fixed
for clarity. 

https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3053

Signed-off-by: lihaowei <haoweili35@gmail.com>
This commit is contained in:
Howie 2022-10-10 15:44:58 +08:00 committed by GitHub
parent 921918cb49
commit e384d88abf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 66 deletions

View File

@ -15,10 +15,10 @@
"endsAt":{%q= alert.End.Format(time.RFC3339Nano) %}, "endsAt":{%q= alert.End.Format(time.RFC3339Nano) %},
{% endif %} {% endif %}
"labels": { "labels": {
"alertname":{%q= alert.Name %}
{% code lbls := alert.toPromLabels(relabelCfg) %} {% code lbls := alert.toPromLabels(relabelCfg) %}
{% for _, l := range lbls %} {% code ll := len(lbls) %}
,{%q= l.Name %}:{%q= l.Value %} {% for idx, l := range lbls %}
{%q= l.Name %}:{%q= l.Value %}{% if idx != ll-1 %}, {% endif %}
{% endfor %} {% endfor %}
}, },
"annotations": { "annotations": {

View File

@ -1,135 +1,140 @@
// Code generated by qtc from "alertmanager_request.qtpl". DO NOT EDIT. // Code generated by qtc from "alertmanager_request.qtpl". DO NOT EDIT.
// See https://github.com/valyala/quicktemplate for details. // See https://github.com/valyala/quicktemplate for details.
//line app/vmalert/notifier/alertmanager_request.qtpl:1 //line alertmanager_request.qtpl:1
package notifier package notifier
//line app/vmalert/notifier/alertmanager_request.qtpl:1 //line alertmanager_request.qtpl:1
import ( import (
"time" "time"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promrelabel" "github.com/VictoriaMetrics/VictoriaMetrics/lib/promrelabel"
) )
//line app/vmalert/notifier/alertmanager_request.qtpl:8 //line alertmanager_request.qtpl:8
import ( import (
qtio422016 "io" qtio422016 "io"
qt422016 "github.com/valyala/quicktemplate" qt422016 "github.com/valyala/quicktemplate"
) )
//line app/vmalert/notifier/alertmanager_request.qtpl:8 //line alertmanager_request.qtpl:8
var ( var (
_ = qtio422016.Copy _ = qtio422016.Copy
_ = qt422016.AcquireByteBuffer _ = qt422016.AcquireByteBuffer
) )
//line app/vmalert/notifier/alertmanager_request.qtpl:8 //line alertmanager_request.qtpl:8
func streamamRequest(qw422016 *qt422016.Writer, alerts []Alert, generatorURL func(Alert) string, relabelCfg *promrelabel.ParsedConfigs) { func streamamRequest(qw422016 *qt422016.Writer, alerts []Alert, generatorURL func(Alert) string, relabelCfg *promrelabel.ParsedConfigs) {
//line app/vmalert/notifier/alertmanager_request.qtpl:8 //line alertmanager_request.qtpl:8
qw422016.N().S(`[`) qw422016.N().S(`[`)
//line app/vmalert/notifier/alertmanager_request.qtpl:10 //line alertmanager_request.qtpl:10
for i, alert := range alerts { for i, alert := range alerts {
//line app/vmalert/notifier/alertmanager_request.qtpl:10 //line alertmanager_request.qtpl:10
qw422016.N().S(`{"startsAt":`) qw422016.N().S(`{"startsAt":`)
//line app/vmalert/notifier/alertmanager_request.qtpl:12 //line alertmanager_request.qtpl:12
qw422016.N().Q(alert.Start.Format(time.RFC3339Nano)) qw422016.N().Q(alert.Start.Format(time.RFC3339Nano))
//line app/vmalert/notifier/alertmanager_request.qtpl:12 //line alertmanager_request.qtpl:12
qw422016.N().S(`,"generatorURL":`) qw422016.N().S(`,"generatorURL":`)
//line app/vmalert/notifier/alertmanager_request.qtpl:13 //line alertmanager_request.qtpl:13
qw422016.N().Q(generatorURL(alert)) qw422016.N().Q(generatorURL(alert))
//line app/vmalert/notifier/alertmanager_request.qtpl:13 //line alertmanager_request.qtpl:13
qw422016.N().S(`,`) qw422016.N().S(`,`)
//line app/vmalert/notifier/alertmanager_request.qtpl:14 //line alertmanager_request.qtpl:14
if !alert.End.IsZero() { if !alert.End.IsZero() {
//line app/vmalert/notifier/alertmanager_request.qtpl:14 //line alertmanager_request.qtpl:14
qw422016.N().S(`"endsAt":`) qw422016.N().S(`"endsAt":`)
//line app/vmalert/notifier/alertmanager_request.qtpl:15 //line alertmanager_request.qtpl:15
qw422016.N().Q(alert.End.Format(time.RFC3339Nano)) qw422016.N().Q(alert.End.Format(time.RFC3339Nano))
//line app/vmalert/notifier/alertmanager_request.qtpl:15 //line alertmanager_request.qtpl:15
qw422016.N().S(`,`) qw422016.N().S(`,`)
//line app/vmalert/notifier/alertmanager_request.qtpl:16 //line alertmanager_request.qtpl:16
} }
//line app/vmalert/notifier/alertmanager_request.qtpl:16 //line alertmanager_request.qtpl:16
qw422016.N().S(`"labels": {"alertname":`) qw422016.N().S(`"labels": {`)
//line app/vmalert/notifier/alertmanager_request.qtpl:18 //line alertmanager_request.qtpl:18
qw422016.N().Q(alert.Name)
//line app/vmalert/notifier/alertmanager_request.qtpl:19
lbls := alert.toPromLabels(relabelCfg) lbls := alert.toPromLabels(relabelCfg)
//line app/vmalert/notifier/alertmanager_request.qtpl:20 //line alertmanager_request.qtpl:19
for _, l := range lbls { ll := len(lbls)
//line app/vmalert/notifier/alertmanager_request.qtpl:20
qw422016.N().S(`,`) //line alertmanager_request.qtpl:20
//line app/vmalert/notifier/alertmanager_request.qtpl:21 for idx, l := range lbls {
//line alertmanager_request.qtpl:21
qw422016.N().Q(l.Name) qw422016.N().Q(l.Name)
//line app/vmalert/notifier/alertmanager_request.qtpl:21 //line alertmanager_request.qtpl:21
qw422016.N().S(`:`) qw422016.N().S(`:`)
//line app/vmalert/notifier/alertmanager_request.qtpl:21 //line alertmanager_request.qtpl:21
qw422016.N().Q(l.Value) qw422016.N().Q(l.Value)
//line app/vmalert/notifier/alertmanager_request.qtpl:22 //line alertmanager_request.qtpl:21
if idx != ll-1 {
//line alertmanager_request.qtpl:21
qw422016.N().S(`,`)
//line alertmanager_request.qtpl:21
}
//line alertmanager_request.qtpl:22
} }
//line app/vmalert/notifier/alertmanager_request.qtpl:22 //line alertmanager_request.qtpl:22
qw422016.N().S(`},"annotations": {`) qw422016.N().S(`},"annotations": {`)
//line app/vmalert/notifier/alertmanager_request.qtpl:25 //line alertmanager_request.qtpl:25
c := len(alert.Annotations) c := len(alert.Annotations)
//line app/vmalert/notifier/alertmanager_request.qtpl:26 //line alertmanager_request.qtpl:26
for k, v := range alert.Annotations { for k, v := range alert.Annotations {
//line app/vmalert/notifier/alertmanager_request.qtpl:27 //line alertmanager_request.qtpl:27
c = c - 1 c = c - 1
//line app/vmalert/notifier/alertmanager_request.qtpl:28 //line alertmanager_request.qtpl:28
qw422016.N().Q(k) qw422016.N().Q(k)
//line app/vmalert/notifier/alertmanager_request.qtpl:28 //line alertmanager_request.qtpl:28
qw422016.N().S(`:`) qw422016.N().S(`:`)
//line app/vmalert/notifier/alertmanager_request.qtpl:28 //line alertmanager_request.qtpl:28
qw422016.N().Q(v) qw422016.N().Q(v)
//line app/vmalert/notifier/alertmanager_request.qtpl:28 //line alertmanager_request.qtpl:28
if c > 0 { if c > 0 {
//line app/vmalert/notifier/alertmanager_request.qtpl:28 //line alertmanager_request.qtpl:28
qw422016.N().S(`,`) qw422016.N().S(`,`)
//line app/vmalert/notifier/alertmanager_request.qtpl:28 //line alertmanager_request.qtpl:28
} }
//line app/vmalert/notifier/alertmanager_request.qtpl:29 //line alertmanager_request.qtpl:29
} }
//line app/vmalert/notifier/alertmanager_request.qtpl:29 //line alertmanager_request.qtpl:29
qw422016.N().S(`}}`) qw422016.N().S(`}}`)
//line app/vmalert/notifier/alertmanager_request.qtpl:32 //line alertmanager_request.qtpl:32
if i != len(alerts)-1 { if i != len(alerts)-1 {
//line app/vmalert/notifier/alertmanager_request.qtpl:32 //line alertmanager_request.qtpl:32
qw422016.N().S(`,`) qw422016.N().S(`,`)
//line app/vmalert/notifier/alertmanager_request.qtpl:32 //line alertmanager_request.qtpl:32
} }
//line app/vmalert/notifier/alertmanager_request.qtpl:33 //line alertmanager_request.qtpl:33
} }
//line app/vmalert/notifier/alertmanager_request.qtpl:33 //line alertmanager_request.qtpl:33
qw422016.N().S(`]`) qw422016.N().S(`]`)
//line app/vmalert/notifier/alertmanager_request.qtpl:35 //line alertmanager_request.qtpl:35
} }
//line app/vmalert/notifier/alertmanager_request.qtpl:35 //line alertmanager_request.qtpl:35
func writeamRequest(qq422016 qtio422016.Writer, alerts []Alert, generatorURL func(Alert) string, relabelCfg *promrelabel.ParsedConfigs) { func writeamRequest(qq422016 qtio422016.Writer, alerts []Alert, generatorURL func(Alert) string, relabelCfg *promrelabel.ParsedConfigs) {
//line app/vmalert/notifier/alertmanager_request.qtpl:35 //line alertmanager_request.qtpl:35
qw422016 := qt422016.AcquireWriter(qq422016) qw422016 := qt422016.AcquireWriter(qq422016)
//line app/vmalert/notifier/alertmanager_request.qtpl:35 //line alertmanager_request.qtpl:35
streamamRequest(qw422016, alerts, generatorURL, relabelCfg) streamamRequest(qw422016, alerts, generatorURL, relabelCfg)
//line app/vmalert/notifier/alertmanager_request.qtpl:35 //line alertmanager_request.qtpl:35
qt422016.ReleaseWriter(qw422016) qt422016.ReleaseWriter(qw422016)
//line app/vmalert/notifier/alertmanager_request.qtpl:35 //line alertmanager_request.qtpl:35
} }
//line app/vmalert/notifier/alertmanager_request.qtpl:35 //line alertmanager_request.qtpl:35
func amRequest(alerts []Alert, generatorURL func(Alert) string, relabelCfg *promrelabel.ParsedConfigs) string { func amRequest(alerts []Alert, generatorURL func(Alert) string, relabelCfg *promrelabel.ParsedConfigs) string {
//line app/vmalert/notifier/alertmanager_request.qtpl:35 //line alertmanager_request.qtpl:35
qb422016 := qt422016.AcquireByteBuffer() qb422016 := qt422016.AcquireByteBuffer()
//line app/vmalert/notifier/alertmanager_request.qtpl:35 //line alertmanager_request.qtpl:35
writeamRequest(qb422016, alerts, generatorURL, relabelCfg) writeamRequest(qb422016, alerts, generatorURL, relabelCfg)
//line app/vmalert/notifier/alertmanager_request.qtpl:35 //line alertmanager_request.qtpl:35
qs422016 := string(qb422016.B) qs422016 := string(qb422016.B)
//line app/vmalert/notifier/alertmanager_request.qtpl:35 //line alertmanager_request.qtpl:35
qt422016.ReleaseByteBuffer(qb422016) qt422016.ReleaseByteBuffer(qb422016)
//line app/vmalert/notifier/alertmanager_request.qtpl:35 //line alertmanager_request.qtpl:35
return qs422016 return qs422016
//line app/vmalert/notifier/alertmanager_request.qtpl:35 //line alertmanager_request.qtpl:35
} }

View File

@ -67,9 +67,6 @@ func TestAlertManager_Send(t *testing.T) {
if a[0].GeneratorURL != "0/0" { if a[0].GeneratorURL != "0/0" {
t.Errorf("expected 0/0 as generatorURL got %s", a[0].GeneratorURL) t.Errorf("expected 0/0 as generatorURL got %s", a[0].GeneratorURL)
} }
if a[0].Labels["alertname"] != "alert0" {
t.Errorf("expected alert0 as alert name got %s", a[0].Labels["alertname"])
}
if a[0].StartsAt.IsZero() { if a[0].StartsAt.IsZero() {
t.Errorf("expected non-zero start time") t.Errorf("expected non-zero start time")
} }