Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow filling alert details template with HTML fields/tags #2246

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ func newAlertNode(et *ExecutingTask, n *pipeline.AlertNode, d NodeDiagnostic) (a

return html.JS(tmpBuffer.String())
},
"safeHtml": func(v interface{}) html.HTML {
return html.HTML(v.(string))
},
}).Parse(n.Details)
if err != nil {
return nil, err
Expand Down
99 changes: 99 additions & 0 deletions integrations/streamer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import (
"html"
"io/ioutil"
"math/rand"
"mime/quotedprintable"
"net/http"
"net/http/httptest"
"net/mail"
"os"
"path"
"path/filepath"
"reflect"
"strings"
"sync/atomic"
"testing"
"text/template"
Expand Down Expand Up @@ -10200,6 +10202,103 @@ Value: 10
}
}

func TestStream_AlertEmail_HtmlEscape(t *testing.T) {
var script = `
stream
|from()
.measurement('cpu')
.where(lambda: "host" == 'serverA')
.groupBy('host')
|window()
.period(10s)
.every(10s)
|count('value')
|eval(lambda: 'HTML <i arg="1">field</i>')
.keep()
.as('htmlFieldTest')
|eval(lambda: 'HTML <i arg="1">tag</i>')
.keep()
.as('htmlTagTest')
.tags('htmlTagTest')
|alert()
.id('')
.details('
Field, escaped: {{ index .Fields "htmlFieldTest" }}
Field, safeHtml: {{ index .Fields "htmlFieldTest" | safeHtml }}
Tag, escaped: {{ index .Tags "htmlTagTest" }}
Tag, safeHtml: {{ index .Tags "htmlTagTest" | safeHtml }}
')
.crit(lambda: "count" > 8.0)
.email('[email protected]')
`
expField := "HTML <i arg=\"1\">field</i>"
expTag := "HTML <i arg=\"1\">tag</i>"
expDetails := fmt.Sprintf(
"\nField, escaped: %v\nField, safeHtml: %v\nTag, escaped: %v\nTag, safeHtml: %v\n",
html.EscapeString(expField),
expField,
html.EscapeString(expTag),
expTag)
var quotedPrintableDetails bytes.Buffer
quotedPrintableWriter := quotedprintable.NewWriter(&quotedPrintableDetails)
_, _ = quotedPrintableWriter.Write([]byte(expDetails))
_ = quotedPrintableWriter.Close()

expMail := []*smtptest.Message{
{
Header: mail.Header{
"Mime-Version": []string{"1.0"},
"Content-Type": []string{"text/html; charset=UTF-8"},
"Content-Transfer-Encoding": []string{"quoted-printable"},
},
Body: strings.Replace(quotedPrintableDetails.String(), "\r", "", -1),
},
}

smtpServer, err := smtptest.NewServer()
if err != nil {
t.Fatal(err)
}
defer smtpServer.Close()
sc := smtp.Config{
Enabled: true,
Host: smtpServer.Host,
Port: smtpServer.Port,
From: "[email protected]",
}
smtpService := smtp.NewService(sc, diagService.NewSMTPHandler())
if err := smtpService.Open(); err != nil {
t.Fatal(err)
}
defer smtpService.Close()

tmInit := func(tm *kapacitor.TaskMaster) {
tm.SMTPService = smtpService
}

testStreamerNoOutput(t, "TestStream_Alert", script, 13*time.Second, tmInit)

// Close both client and server to ensure all message are processed
smtpService.Close()
smtpServer.Close()

errors := smtpServer.Errors()
if got, exp := len(errors), 0; got != exp {
t.Errorf("unexpected smtp server errors: %v", errors)
}

msgs := smtpServer.SentMessages()
if got, exp := len(msgs), len(expMail); got != exp {
t.Errorf("unexpected number of messages sent: got %d exp %d", got, exp)
}
for i, exp := range expMail {
got := msgs[i]
if err := exp.Compare(got); err != nil {
t.Errorf("%d %s", i, err)
}
}
}

func TestStream_AlertSNMPTrap(t *testing.T) {

var script = `
Expand Down