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

Improve logs #2299

Merged
merged 3 commits into from
Sep 24, 2018
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
2 changes: 1 addition & 1 deletion cmd/bosun/conf/actionNotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (n *Notification) RunOnActionType(at models.ActionType) bool {

// Prepate an action notification, but don't send yet.
func (n *Notification) PrepareAction(at models.ActionType, t *Template, c SystemConfProvider, states []*models.IncidentState, user, message string) *PreparedNotifications {
pn := &PreparedNotifications{}
pn := &PreparedNotifications{Name: n.Name, Print: n.Print}
// get template keys to use for actions. Merge with default sets
tks := n.ActionTemplateKeys[at].Combine(n.ActionTemplateKeys[models.ActionNone])
buf := &bytes.Buffer{}
Expand Down
110 changes: 88 additions & 22 deletions cmd/bosun/conf/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ import (
"github.com/jordan-wright/email"
)

const (
sendLogSuccessFmt = "%s; name: %s; transport: %s; dst: %s; body: %s"
sendLogErrorFmt = "%s; name: %s; transport: %s; dst: %s; body: %s; error: %s"
httpSendErrorFmt = "bad response for '%s' %s notification using template key '%s' for alert keys %v method %s: %d"
)

func init() {
metadata.AddMetricMeta(
"bosun.email.sent", metadata.Counter, metadata.PerSecond,
Expand All @@ -38,33 +44,72 @@ func init() {
type PreparedNotifications struct {
Email *PreparedEmail
HTTP []*PreparedHttp
Print string
Print bool
Name string
Errors []string
}

func (p *PreparedNotifications) Send(c SystemConfProvider) (errs []error) {
if p.Email != nil {
if err := p.Email.Send(c); err != nil {
slog.Errorf("sending email: %s", err)
slog.Errorf(
sendLogErrorFmt,
fmt.Sprintf("subject: %s", p.Email.Subject),
p.Name,
"email",
strings.Join(p.Email.To, ","),
p.Email.Body,
err.Error(),
)
errs = append(errs, err)
} else if p.Print {
slog.Infof(
sendLogSuccessFmt,
fmt.Sprintf("subject: %s", p.Email.Subject),
p.Name,
"email",
strings.Join(p.Email.To, ","),
p.Email.Body,
)
}
}
for _, h := range p.HTTP {
var logPrefix string
if h.Details.At != "" {
logPrefix = fmt.Sprintf("action_type: %s", h.Details.At)
} else {
logPrefix = "type: alert"
}
if _, err := h.Send(); err != nil {
slog.Errorf("sending http: %s", err)
slog.Errorf(
sendLogErrorFmt,
logPrefix,
h.Details.NotifyName,
"http_"+h.Method,
h.URL,
h.Body,
err.Error(),
)
errs = append(errs, err)
} else if p.Print {
slog.Infof(
sendLogSuccessFmt,
logPrefix,
h.Details.NotifyName,
"http_"+h.Method,
h.URL,
h.Body,
)
}
}
if p.Print != "" {
slog.Infoln(p.Print)
}

return
}

// PrepareAlert does all of the work of selecting what content to send to which sources. It does not actually send any notifications,
// but the returned object can be used to send them.
func (n *Notification) PrepareAlert(rt *models.RenderedTemplates, ak string, attachments ...*models.Attachment) *PreparedNotifications {
pn := &PreparedNotifications{}
pn := &PreparedNotifications{Name: n.Name, Print: n.Print}
if len(n.Email) > 0 {
subject := rt.GetDefault(n.EmailSubjectTemplate, "emailSubject")
body := rt.GetDefault(n.BodyTemplate, "emailBody")
Expand Down Expand Up @@ -101,13 +146,6 @@ func (n *Notification) PrepareAlert(rt *models.RenderedTemplates, ak string, att
}
pn.HTTP = append(pn.HTTP, n.PrepHttp("GET", url, "", details))
}
if n.Print {
if n.BodyTemplate != "" {
pn.Print = "Subject: " + rt.Subject + ", Body: " + rt.Get(n.BodyTemplate)
} else {
pn.Print = rt.Subject
}
}
return pn
}

Expand Down Expand Up @@ -163,17 +201,45 @@ func (p *PreparedHttp) Send() (int, error) {
collect.Add("post.sent_failed", nil, 1)
switch p.Details.NotifyType {
case alert:
return resp.StatusCode, fmt.Errorf("bad response for '%s' alert notification using template key '%s' for alert keys %v method %s: %d",
p.Details.NotifyName, p.Details.TemplateKey, strings.Join(p.Details.Ak, ","), p.Method, resp.StatusCode)
return resp.StatusCode, fmt.Errorf(
httpSendErrorFmt,
p.Details.NotifyName,
"alert",
p.Details.TemplateKey,
strings.Join(p.Details.Ak, ","),
p.Method,
resp.StatusCode,
)
case unknown:
return resp.StatusCode, fmt.Errorf("bad response for '%s' unknown notification using template key '%s' for alert keys %v method %s: %d",
p.Details.NotifyName, p.Details.TemplateKey, strings.Join(p.Details.Ak, ","), p.Method, resp.StatusCode)
return resp.StatusCode, fmt.Errorf(
httpSendErrorFmt,
p.Details.NotifyName,
"unknown",
p.Details.TemplateKey,
strings.Join(p.Details.Ak, ","),
p.Method,
resp.StatusCode,
)
case multiunknown:
return resp.StatusCode, fmt.Errorf("bad response for '%s' multi-unknown notification using template key '%s' for alert keys %v method %s: %d",
p.Details.NotifyName, p.Details.TemplateKey, strings.Join(p.Details.Ak, ","), p.Method, resp.StatusCode)
return resp.StatusCode, fmt.Errorf(
httpSendErrorFmt,
p.Details.NotifyName,
"multi-unknown",
p.Details.TemplateKey,
strings.Join(p.Details.Ak, ","),
p.Method,
resp.StatusCode,
)
default:
return resp.StatusCode, fmt.Errorf("bad response for '%s' action '%s' notification using template key '%s' for alert keys %v method %s: %d",
p.Details.NotifyName, p.Details.At, p.Details.TemplateKey, strings.Join(p.Details.Ak, ","), p.Method, resp.StatusCode)
return resp.StatusCode, fmt.Errorf(
httpSendErrorFmt,
p.Details.NotifyName,
fmt.Sprintf("action '%s'", p.Details.At),
p.Details.TemplateKey,
strings.Join(p.Details.Ak, ","),
p.Method,
resp.StatusCode,
)
}
}
collect.Add("post.sent", nil, 1)
Expand Down
1 change: 0 additions & 1 deletion cmd/bosun/conf/unknownNotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ func (n *Notification) NotifyMultipleUnknowns(t *Template, c SystemConfProvider,

// code common to PrepareAction / PrepareUnknown / PrepareMultipleUnknowns
func (n *Notification) prepareFromTemplateKeys(pn *PreparedNotifications, tks NotificationTemplateKeys, render func(string, *template.Template) (string, error), defaults defaultTemplates, alertDetails *NotificationDetails) {

if len(n.Email) > 0 || n.Post != nil || tks.PostTemplate != "" {
body, _ := render(tks.BodyTemplate, defaults.body)
if subject, err := render(tks.EmailSubjectTemplate, defaults.subject); err == nil {
Expand Down
4 changes: 3 additions & 1 deletion cmd/bosun/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"strings"
"time"

"bosun.org/_version"
version "bosun.org/_version"
"bosun.org/annotate/backend"
"bosun.org/annotate/web"
"bosun.org/cmd/bosun/conf"
Expand Down Expand Up @@ -789,6 +789,8 @@ func Action(t miniprofiler.Timer, w http.ResponseWriter, r *http.Request) (inter
if err != nil {
return nil, err
}
} else {
slog.Infof("action without notification. user: %s, type: %s, keys: %v, ids: %v", data.User, data.Type, data.Keys, data.Ids)
}
return nil, nil
}
Expand Down