Skip to content

Commit

Permalink
use single html template for all alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
useEffects committed Nov 28, 2023
1 parent 29c8ff7 commit 1701d41
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 37 deletions.
58 changes: 21 additions & 37 deletions pkg/services/ngalert/notifier/channels/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,35 @@ func (en *EmailNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool,
}
}
}
Dispatcher := func(data ExtendedData, isNoDataAlert bool) (bool, error) {
Dispatcher := func(data ExtendedData) (bool, error) {
dataAlerts := []ExtendedAlert{}
noDataAlerts := []ExtendedAlert{}
for _, alert := range data.Alerts {
if alert.Labels["alertname"] == "DatasourceNoData" {
newAlert := ExtendedAlert{}
deepcopier.Copy(alert).To(&newAlert)
noDataAlerts = append(noDataAlerts, newAlert)
} else {
newAlert := ExtendedAlert{}
deepcopier.Copy(alert).To(&newAlert)
dataAlerts = append(dataAlerts, newAlert)
}
}
cmd := &models.SendEmailCommandSync{
SendEmailCommand: models.SendEmailCommand{
Subject: title,
Data: map[string]interface{}{
"Title": title,
"Message": tmpl(en.Message),
"Status": data.Status,
"Alerts": data.Alerts,
"Alerts": dataAlerts,
"GroupLabels": data.GroupLabels,
"CommonLabels": data.CommonLabels,
"CommonAnnotations": data.CommonAnnotations,
"ExternalURL": data.ExternalURL,
"RuleUrl": ruleURL,
"AlertPageUrl": alertPageURL,
"NoDataAlerts": noDataAlerts,
},
To: en.Addresses,
SingleEmail: en.SingleEmail,
Expand All @@ -131,50 +145,20 @@ func (en *EmailNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool,
if tmplErr != nil {
en.log.Warn("failed to template email message", "err", tmplErr.Error())
}
if isNoDataAlert {
cmd.Subject = "No Data Alert"
cmd.Template = "no_data_alert"
}

if err := bus.Dispatch(ctx, cmd); err != nil {
return false, err
}
return true, nil
}
dataAlerts := []ExtendedAlert{}
noDataAlerts := []ExtendedAlert{}
for _, alert := range data.Alerts {
if alert.Labels["alertname"] == "DatasourceNoData" {
newAlert := ExtendedAlert{}
deepcopier.Copy(alert).To(&newAlert)
noDataAlerts = append(noDataAlerts, newAlert)
} else {
newAlert := ExtendedAlert{}
deepcopier.Copy(alert).To(&newAlert)
dataAlerts = append(dataAlerts, newAlert)
}
}
if len(dataAlerts) > 0 {
newData := &ExtendedData{}
deepcopier.Copy(data).To(newData)
newData.Alerts = dataAlerts
ok, err := Dispatcher(*newData, false)
if !ok {
return ok, err
}
}
if len(noDataAlerts) > 0 {
newData := &ExtendedData{}
deepcopier.Copy(data).To(newData)
newData.Alerts = noDataAlerts
ok, err := Dispatcher(*newData, true)
if !ok {
return ok, err
}
ok, err := Dispatcher(*data);
if !ok {
return ok, err
}
data.Alerts = ExtendedAlerts{}
return true, nil
}

func (en *EmailNotifier) SendResolved() bool {
return !en.GetDisableResolveMessage()
}
}
59 changes: 59 additions & 0 deletions public/emails/default_alert.html
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,65 @@ <h4>Annotations:</h4>
</table>
{{ end }}

{{ if or (.Labels) (.URLLabels) }}
<h4>Labels:</h4>
<table>
{{ range $key, $val := .Labels }}
<tr>
<td>{{$key}}</td>
<td>{{$val}}</td>
</tr>
{{end}}
{{range $key, $val := .URLLabels}}
<tr>
<td>{{$key}}</td>
<td><a href="{{$val}}">{{$val}}<a></td>
</tr>
{{end}}
</table>
{{ end }}
{{end}}
{{range .NoDataAlerts}}
<p class="alert-name"> Alert name: {{.Labels.alertname}}</p>
{{ $once := false }}
{{range $Value := .Values}}
{{ range $key, $value := $Value.Values}}
{{ if not $once }}
<h4> Evaluations: </h4>
{{$once = true}}
{{end}}
{{end}}
{{end}}
{{ if gt (len .Values) 0 }}
<table>
{{ range $it, $Value := .Values }}
{{ range $key, $val := $Value.Values }}
<tr>
<td>{{$key}}</td>
<td>{{$val.Value}}</td>
</tr>
{{end}}
{{end}}
</table>
{{ end }}
{{ if or (.Annotations) (.URLAnnotations) }}
<h4>Annotations:</h4>
<table>
{{ range $key, $val := .Annotations }}
<tr>
<td>{{$key}}</td>
<td>{{$val}}</td>
</tr>
{{end}}
{{range $key, $val := .URLAnnotations}}
<tr>
<td>{{$key}}</td>
<td><a href="{{$val}}">{{$val}}<a></td>
</tr>
{{end}}
</table>
{{ end }}

{{ if or (.Labels) (.URLLabels) }}
<h4>Labels:</h4>
<table>
Expand Down

0 comments on commit 1701d41

Please sign in to comment.