Render new email template and fix the title (#32314)

* Render new email template and fix the title

Signed-off-by: Ganesh Vernekar <ganeshvern@gmail.com>

* Fix nit

Signed-off-by: Ganesh Vernekar <ganeshvern@gmail.com>
This commit is contained in:
Ganesh Vernekar
2021-03-26 12:53:14 +05:30
committed by GitHub
parent 600f63f627
commit a0db4dce32
10 changed files with 395 additions and 13 deletions

View File

@@ -2,7 +2,9 @@ package channels
import (
"context"
"fmt"
"net/url"
"strings"
gokit_log "github.com/go-kit/kit/log"
"github.com/grafana/grafana/pkg/infra/log"
@@ -66,12 +68,13 @@ func (en *EmailNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool,
// We only need ExternalURL from this template object. This hack should go away with https://github.com/prometheus/alertmanager/pull/2508.
data := notify.GetTemplateData(ctx, &template.Template{ExternalURL: en.externalUrl}, as, gokit_log.NewNopLogger())
title := getTitleFromTemplateData(data)
cmd := &models.SendEmailCommandSync{
SendEmailCommand: models.SendEmailCommand{
Subject: "TODO",
Subject: title,
Data: map[string]interface{}{
"Title": "TODO",
"Subject": "TODO",
"Title": title,
"Receiver": data.Receiver,
"Status": data.Status,
"Alerts": data.Alerts,
@@ -95,6 +98,18 @@ func (en *EmailNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool,
return true, nil
}
func getTitleFromTemplateData(data *template.Data) string {
title := "[" + data.Status
if data.Status == string(model.AlertFiring) {
title += fmt.Sprintf(":%d", len(data.Alerts.Firing()))
}
title += "]" + strings.Join(data.GroupLabels.SortedPairs().Values(), " ") + " "
if len(data.CommonLabels) > len(data.GroupLabels) {
title += "(" + strings.Join(data.CommonLabels.Remove(data.GroupLabels.Names()).Values(), " ") + ")"
}
return title
}
func (en *EmailNotifier) SendResolved() bool {
// TODO: implement this.
return true