Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.
Merged
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
52 changes: 39 additions & 13 deletions cmd/bosun/sched/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,37 +265,63 @@ func (s *Schedule) executeTemplates(state *State, event *Event, a *conf.Alert, r
state.EmailSubject = nil
state.Attachments = nil
if event.Status != StUnknown {
var errs []error
metric := "template.render"
//Render subject
endTiming := collect.StartTimer(metric, opentsdb.TagSet{"alert": a.Name, "type": "subject"})
subject, serr := s.ExecuteSubject(r, a, state, false)
if serr != nil {
slog.Infof("%s: %v", state.AlertKey(), serr)
subject, err := s.ExecuteSubject(r, a, state, false)
if err != nil {
slog.Infof("%s: %v", state.AlertKey(), err)
errs = append(errs, err)
} else if subject == nil {
err = fmt.Errorf("Empty subject on %s", state.AlertKey())
slog.Error(err)
errs = append(errs, err)
}
endTiming()

//Render body
endTiming = collect.StartTimer(metric, opentsdb.TagSet{"alert": a.Name, "type": "body"})
body, _, berr := s.ExecuteBody(r, a, state, false)
if berr != nil {
slog.Infof("%s: %v", state.AlertKey(), berr)
body, _, err := s.ExecuteBody(r, a, state, false)
if err != nil {
slog.Infof("%s: %v", state.AlertKey(), err)
errs = append(errs, err)
} else if subject == nil {
err = fmt.Errorf("Empty body on %s", state.AlertKey())
slog.Error(err)
errs = append(errs, err)
}
endTiming()

//Render email body
endTiming = collect.StartTimer(metric, opentsdb.TagSet{"alert": a.Name, "type": "emailbody"})
emailbody, attachments, merr := s.ExecuteBody(r, a, state, true)
if merr != nil {
slog.Infof("%s: %v", state.AlertKey(), merr)
emailbody, attachments, err := s.ExecuteBody(r, a, state, true)
if err != nil {
slog.Infof("%s: %v", state.AlertKey(), err)
errs = append(errs, err)
} else if subject == nil {
err = fmt.Errorf("Empty email body on %s", state.AlertKey())
slog.Error(err)
errs = append(errs, err)
}
endTiming()

//Render email subject
endTiming = collect.StartTimer(metric, opentsdb.TagSet{"alert": a.Name, "type": "emailsubject"})
emailsubject, eserr := s.ExecuteSubject(r, a, state, true)
emailsubject, err := s.ExecuteSubject(r, a, state, true)
if err != nil {
slog.Infof("%s: %v", state.AlertKey(), err)
errs = append(errs, err)
} else if subject == nil {
err = fmt.Errorf("Empty email subject on %s", state.AlertKey())
slog.Error(err)
errs = append(errs, err)
}
endTiming()
if serr != nil || berr != nil || merr != nil || eserr != nil {
var err error

if errs != nil {
endTiming = collect.StartTimer(metric, opentsdb.TagSet{"alert": a.Name, "type": "bad"})
subject, body, err = s.ExecuteBadTemplate(serr, berr, r, a, state)
subject, body, err = s.ExecuteBadTemplate(errs, r, a, state)
endTiming()

if err != nil {
Expand Down
28 changes: 8 additions & 20 deletions cmd/bosun/sched/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,11 @@ func (s *Schedule) ExecuteSubject(rh *RunHistory, a *conf.Alert, st *State, isEm

var error_body = template.Must(template.New("body_error_template").Parse(`
<p>There was a runtime error processing alert {{.State.AlertKey}} using the {{.Alert.Template.Name}} template. The following errors occurred:</p>
{{if .Serr}}
<p>Subject: {{.Serr}}</p>
{{end}}
{{if .Berr}}
<p>Body: {{.Berr}}</p>
<ul>
{{range .Errors}}
<li>{{.}}</li>
{{end}}
</ul>
<p>Use <a href="{{.Rule}}">this link</a> to the rule page to correct this.</p>
<h2>Generic Alert Information</h2>
<p>Status: {{.Last.Status}}</p>
Expand All @@ -167,24 +166,13 @@ var error_body = template.Must(template.New("body_error_template").Parse(`
</tr>
{{end}}</table>`))

func (s *Schedule) ExecuteBadTemplate(s_err, b_err error, rh *RunHistory, a *conf.Alert, st *State) (subject, body []byte, err error) {
sub := "error: template rendering error in the "
if s_err != nil {
sub += "subject"
}
if s_err != nil && b_err != nil {
sub += " and "
}
if b_err != nil {
sub += "body"
}
sub += fmt.Sprintf(" for alert %v", st.AlertKey())
func (s *Schedule) ExecuteBadTemplate(errs []error, rh *RunHistory, a *conf.Alert, st *State) (subject, body []byte, err error) {
sub := fmt.Sprintf("error: template rendering error for alert %v", st.AlertKey())
c := struct {
Serr, Berr error
Errors []error
*Context
}{
Serr: s_err,
Berr: b_err,
Errors: errs,
Context: s.Data(rh, st, a, true),
}
buf := new(bytes.Buffer)
Expand Down
2 changes: 1 addition & 1 deletion cmd/bosun/web/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func procRule(t miniprofiler.Timer, c *conf.Conf, a *conf.Alert, now time.Time,
}()
if s_err != nil || b_err != nil {
var err error
subject, body, err = s.ExecuteBadTemplate(s_err, b_err, rh, a, instance)
subject, body, err = s.ExecuteBadTemplate([]error{s_err, b_err}, rh, a, instance)
if err != nil {
subject = []byte(fmt.Sprintf("unable to create tempalate error notification: %v", err))
}
Expand Down