Remove empty lines between labels in email notifications.

Also add test for email formatting.

Change-Id: I309018b021730dee80f9f5bd05eb1a96bf0d237e
This commit is contained in:
Julius Volz 2013-08-16 14:50:51 +02:00
parent 1388e0f411
commit 6b17d8cb6a
2 changed files with 68 additions and 13 deletions

View File

@ -18,6 +18,7 @@ import (
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
@ -30,19 +31,17 @@ import (
const contentTypeJson = "application/json"
const bodyTmpl = `Subject: [ALERT] {{.Labels.alertname}}: {{.Summary}}
var bodyTmpl = template.Must(template.New("message").Parse(`Subject: [ALERT] {{.Labels.alertname}}: {{.Summary}}
{{.Description}}
Grouping labels:
{{range $label, $value := .Labels}}
{{$label}} = "{{$value}}"
{{end}}
{{$label}} = "{{$value}}"{{end}}
Payload labels:
{{range $label, $value := .Payload}}
{{$label}} = "{{$value}}"
{{end}}`
{{$label}} = "{{$value}}"{{end}}`))
var (
notificationBufferSize = flag.Int("notificationBufferSize", 1000, "Size of buffer for pending notifications.")
@ -157,6 +156,13 @@ func (n *notifier) sendPagerDutyNotification(serviceKey string, event *Event) er
return nil
}
func writeEmailBody(w io.Writer, event *Event) error {
if err := bodyTmpl.Execute(w, event); err != nil {
return err
}
return nil
}
func (n *notifier) sendEmailNotification(email string, event *Event) error {
// Connect to the SMTP smarthost.
c, err := smtp.Dial(*smtpSmartHost)
@ -176,14 +182,7 @@ func (n *notifier) sendEmailNotification(email string, event *Event) error {
}
defer wc.Close()
t := template.New("message")
if _, err := t.Parse(bodyTmpl); err != nil {
return err
}
if err := t.Execute(wc, event); err != nil {
return err
}
return nil
return writeEmailBody(wc, event)
}
func (n *notifier) handleNotification(event *Event, config *pb.NotificationConfig, i IsInhibitedInterrogator) {

56
manager/notifier_test.go Normal file
View File

@ -0,0 +1,56 @@
// Copyright 2013 Prometheus Team
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package manager
import (
"bytes"
"testing"
)
func TestWriteEmailBody(t *testing.T) {
event := &Event{
Summary: "Testsummary",
Description: "Test alert description, something went wrong here.",
Labels: EventLabels{
"alertname": "TestAlert",
"grouping_label1": "grouping_value1",
"grouping_label2": "grouping_value2",
},
Payload: EventPayload{
"payload_label1": "payload_value1",
"payload_label2": "payload_value2",
},
}
buf := &bytes.Buffer{}
writeEmailBody(buf, event)
expected := `Subject: [ALERT] TestAlert: Testsummary
Test alert description, something went wrong here.
Grouping labels:
alertname = "TestAlert"
grouping_label1 = "grouping_value1"
grouping_label2 = "grouping_value2"
Payload labels:
payload_label1 = "payload_value1"
payload_label2 = "payload_value2"`
if buf.String() != expected {
t.Fatalf("Expected:\n%s\n\nGot:\n%s", expected, buf.String())
}
}