chore(notify): make logging more consistent, converge on `group_key`

This changes the majority of our `Notify()` implementations to set up a
new logger with the group key attached under the key name `group_key`,
and then to use that logger in all subsequent calls to the logger,
including passing it through to further functions that accept loggers
via params.

A few of the notify implementations are more complicated; they either
extract the key later in their `Notify()` implementation or within sub
methods, or even conditionally like with sns. I left those mostly as is
for now, as they seem to be more snow-flake-y.

Signed-off-by: TJ Hoplock <t.hoplock@gmail.com>
This commit is contained in:
TJ Hoplock 2024-11-16 00:10:50 -05:00
parent e8d0edf057
commit 1a116a1edf
14 changed files with 70 additions and 50 deletions

View File

@ -95,10 +95,11 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
return false, err
}
n.logger.Debug("extracted group key", "key", key)
logger := n.logger.With("group_key", key)
logger.Debug("extracted group key")
alerts := types.Alerts(as...)
data := notify.GetTemplateData(ctx, n.tmpl, as, n.logger)
data := notify.GetTemplateData(ctx, n.tmpl, as, logger)
tmpl := notify.TmplText(n.tmpl, data, &err)
if err != nil {
return false, err
@ -109,14 +110,14 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
return false, err
}
if truncated {
n.logger.Warn("Truncated title", "key", key, "max_runes", maxTitleLenRunes)
logger.Warn("Truncated title", "max_runes", maxTitleLenRunes)
}
description, truncated := notify.TruncateInRunes(tmpl(n.conf.Message), maxDescriptionLenRunes)
if err != nil {
return false, err
}
if truncated {
n.logger.Warn("Truncated message", "key", key, "max_runes", maxDescriptionLenRunes)
logger.Warn("Truncated message", "max_runes", maxDescriptionLenRunes)
}
content, truncated := notify.TruncateInRunes(tmpl(n.conf.Content), maxContentLenRunes)
@ -124,7 +125,7 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
return false, err
}
if truncated {
n.logger.Warn("Truncated message", "key", key, "max_runes", maxContentLenRunes)
logger.Warn("Truncated message", "max_runes", maxContentLenRunes)
}
color := colorGrey
@ -160,7 +161,7 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
if _, err := netUrl.Parse(n.conf.AvatarURL); err == nil {
w.AvatarURL = n.conf.AvatarURL
} else {
n.logger.Warn("Bad avatar url", "key", key)
logger.Warn("Bad avatar url", "key", key)
}
}

View File

@ -72,6 +72,7 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
}
logger := n.logger.With("group_key", key.String())
logger.Debug("extracted group key")
var (
alerts = types.Alerts(as...)

View File

@ -85,9 +85,10 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
return false, err
}
n.logger.Debug("extracted group key", "key", key)
logger := n.logger.With("group_key", key)
logger.Debug("extracted group key")
data := notify.GetTemplateData(ctx, n.tmpl, as, n.logger)
data := notify.GetTemplateData(ctx, n.tmpl, as, logger)
tmpl := notify.TmplText(n.tmpl, data, &err)
if err != nil {
return false, err

View File

@ -104,9 +104,10 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
return false, err
}
n.logger.Debug("extracted group key", "key", key)
logger := n.logger.With("group_key", key)
logger.Debug("extracted group key")
data := notify.GetTemplateData(ctx, n.tmpl, as, n.logger)
data := notify.GetTemplateData(ctx, n.tmpl, as, logger)
tmpl := notify.TmplText(n.tmpl, data, &err)
if err != nil {
return false, err

View File

@ -132,9 +132,10 @@ func (n *Notifier) createRequests(ctx context.Context, as ...*types.Alert) ([]*h
if err != nil {
return nil, false, err
}
data := notify.GetTemplateData(ctx, n.tmpl, as, n.logger)
logger := n.logger.With("group_key", key)
logger.Debug("extracted group key")
n.logger.Debug("extracted group key", "key", key)
data := notify.GetTemplateData(ctx, n.tmpl, as, logger)
tmpl := notify.TmplText(n.tmpl, data, &err)
@ -174,7 +175,7 @@ func (n *Notifier) createRequests(ctx context.Context, as ...*types.Alert) ([]*h
default:
message, truncated := notify.TruncateInRunes(tmpl(n.conf.Message), maxMessageLenRunes)
if truncated {
n.logger.Warn("Truncated message", "alert", key, "max_runes", maxMessageLenRunes)
logger.Warn("Truncated message", "alert", key, "max_runes", maxMessageLenRunes)
}
createEndpointURL := n.conf.APIURL.Copy()

View File

@ -308,17 +308,18 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
if err != nil {
return false, err
}
logger := n.logger.With("group_key", key)
var (
alerts = types.Alerts(as...)
data = notify.GetTemplateData(ctx, n.tmpl, as, n.logger)
data = notify.GetTemplateData(ctx, n.tmpl, as, logger)
eventType = pagerDutyEventTrigger
)
if alerts.Status() == model.AlertResolved {
eventType = pagerDutyEventResolve
}
n.logger.Debug("extracted group key", "key", key, "eventType", eventType)
logger.Debug("extracted group key", "eventType", eventType)
details := make(map[string]string, len(n.conf.Details))
for k, v := range n.conf.Details {

View File

@ -72,10 +72,10 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
if !ok {
return false, fmt.Errorf("group key missing")
}
data := notify.GetTemplateData(ctx, n.tmpl, as, n.logger)
logger := n.logger.With("group_key", key)
logger.Debug("extracted group key")
// @tjhop: should this use `group` for the keyval like most other notify implementations?
n.logger.Debug("extracted group key", "incident", key)
data := notify.GetTemplateData(ctx, n.tmpl, as, logger)
var (
err error
@ -113,7 +113,7 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
title, truncated := notify.TruncateInRunes(tmpl(n.conf.Title), maxTitleLenRunes)
if truncated {
n.logger.Warn("Truncated title", "incident", key, "max_runes", maxTitleLenRunes)
logger.Warn("Truncated title", "incident", key, "max_runes", maxTitleLenRunes)
}
parameters.Add("title", title)
@ -126,7 +126,7 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
message, truncated = notify.TruncateInRunes(message, maxMessageLenRunes)
if truncated {
n.logger.Warn("Truncated message", "incident", key, "max_runes", maxMessageLenRunes)
logger.Warn("Truncated message", "incident", key, "max_runes", maxMessageLenRunes)
}
message = strings.TrimSpace(message)
if message == "" {
@ -137,7 +137,7 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
supplementaryURL, truncated := notify.TruncateInRunes(tmpl(n.conf.URL), maxURLLenRunes)
if truncated {
n.logger.Warn("Truncated URL", "incident", key, "max_runes", maxURLLenRunes)
logger.Warn("Truncated URL", "incident", key, "max_runes", maxURLLenRunes)
}
parameters.Add("url", supplementaryURL)
parameters.Add("url_title", tmpl(n.conf.URLTitle))
@ -163,7 +163,7 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
}
u.RawQuery = parameters.Encode()
// Don't log the URL as it contains secret data (see #1825).
n.logger.Debug("Sending message", "incident", key)
logger.Debug("Sending message", "incident", key)
resp, err := notify.PostText(ctx, n.client, u.String(), nil)
if err != nil {
return true, notify.RedactURL(err)

View File

@ -140,7 +140,14 @@ func getToken(c *config.RocketchatConfig) (string, error) {
func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
var err error
data := notify.GetTemplateData(ctx, n.tmpl, as, n.logger)
key, err := notify.ExtractGroupKey(ctx)
if err != nil {
return false, err
}
logger := n.logger.With("group_key", key)
logger.Debug("extracted group key")
data := notify.GetTemplateData(ctx, n.tmpl, as, logger)
tmplText := notify.TmplText(n.tmpl, data, &err)
if err != nil {
return false, err
@ -152,11 +159,7 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
title, truncated := notify.TruncateInRunes(title, maxTitleLenRunes)
if truncated {
key, err := notify.ExtractGroupKey(ctx)
if err != nil {
return false, err
}
n.logger.Warn("Truncated title", "key", key, "max_runes", maxTitleLenRunes)
logger.Warn("Truncated title", "max_runes", maxTitleLenRunes)
}
att := &Attachment{
Title: title,

View File

@ -93,8 +93,16 @@ type attachment struct {
// Notify implements the Notifier interface.
func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
var err error
key, err := notify.ExtractGroupKey(ctx)
if err != nil {
return false, err
}
logger := n.logger.With("group_key", key)
logger.Debug("extracted group key")
var (
data = notify.GetTemplateData(ctx, n.tmpl, as, n.logger)
data = notify.GetTemplateData(ctx, n.tmpl, as, logger)
tmplText = notify.TmplText(n.tmpl, data, &err)
)
var markdownIn []string
@ -107,11 +115,7 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
title, truncated := notify.TruncateInRunes(tmplText(n.conf.Title), maxTitleLenRunes)
if truncated {
key, err := notify.ExtractGroupKey(ctx)
if err != nil {
return false, err
}
n.logger.Warn("Truncated title", "key", key, "max_runes", maxTitleLenRunes)
logger.Warn("Truncated title", "max_runes", maxTitleLenRunes)
}
att := &attachment{
Title: title,

View File

@ -64,9 +64,17 @@ func New(conf *config.TelegramConfig, t *template.Template, l *slog.Logger, http
}
func (n *Notifier) Notify(ctx context.Context, alert ...*types.Alert) (bool, error) {
key, ok := notify.GroupKey(ctx)
if !ok {
return false, fmt.Errorf("group key missing")
}
logger := n.logger.With("group_key", key)
logger.Debug("extracted group key")
var (
err error
data = notify.GetTemplateData(ctx, n.tmpl, alert, n.logger)
data = notify.GetTemplateData(ctx, n.tmpl, alert, logger)
tmpl = notify.TmplText(n.tmpl, data, &err)
)
@ -74,14 +82,9 @@ func (n *Notifier) Notify(ctx context.Context, alert ...*types.Alert) (bool, err
tmpl = notify.TmplHTML(n.tmpl, data, &err)
}
key, ok := notify.GroupKey(ctx)
if !ok {
return false, fmt.Errorf("group key missing")
}
messageText, truncated := notify.TruncateInRunes(tmpl(n.conf.Message), maxMessageLenRunes)
if truncated {
n.logger.Warn("Truncated message", "alert", key, "max_runes", maxMessageLenRunes)
logger.Warn("Truncated message", "max_runes", maxMessageLenRunes)
}
n.client.Token, err = n.getBotToken()
@ -98,7 +101,7 @@ func (n *Notifier) Notify(ctx context.Context, alert ...*types.Alert) (bool, err
if err != nil {
return true, err
}
n.logger.Debug("Telegram message successfully published", "message_id", message.ID, "chat_id", message.Chat.ID)
logger.Debug("Telegram message successfully published", "message_id", message.ID, "chat_id", message.Chat.ID)
return false, nil
}

View File

@ -141,7 +141,7 @@ func (n *Notifier) createVictorOpsPayload(ctx context.Context, as ...*types.Aler
stateMessage, truncated := notify.TruncateInRunes(stateMessage, maxMessageLenRunes)
if truncated {
n.logger.Warn("Truncated state_message", "incident", key, "max_runes", maxMessageLenRunes)
n.logger.Warn("Truncated state_message", "group_key", key, "max_runes", maxMessageLenRunes)
}
msg := map[string]string{

View File

@ -72,9 +72,10 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
return false, err
}
n.logger.Debug("extracted group key", "key", key)
logger := n.logger.With("group_key", key)
logger.Debug("extracted group key")
data := notify.GetTemplateData(ctx, n.tmpl, as, n.logger)
data := notify.GetTemplateData(ctx, n.tmpl, as, logger)
tmpl := notify.TmplText(n.tmpl, data, &err)
if err != nil {
return false, err
@ -87,7 +88,7 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
message, truncated := notify.TruncateInBytes(message, maxMessageSize)
if truncated {
n.logger.Debug("message truncated due to exceeding maximum allowed length by webex", "truncated_message", message)
logger.Debug("message truncated due to exceeding maximum allowed length by webex", "truncated_message", message)
}
w := webhook{

View File

@ -85,7 +85,8 @@ func (n *Notifier) Notify(ctx context.Context, alerts ...*types.Alert) (bool, er
return false, err
}
n.logger.Debug("extracted group key", "incident", groupKey)
logger := n.logger.With("group_key", groupKey)
logger.Debug("extracted group key")
msg := &Message{
Version: "4",

View File

@ -86,8 +86,10 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
return false, err
}
n.logger.Debug("extracted group key", "key", key)
data := notify.GetTemplateData(ctx, n.tmpl, as, n.logger)
logger := n.logger.With("group_key", key)
logger.Debug("extracted group key")
data := notify.GetTemplateData(ctx, n.tmpl, as, logger)
tmpl := notify.TmplText(n.tmpl, data, &err)
if err != nil {
@ -174,7 +176,7 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
if err != nil {
return true, err
}
n.logger.Debug(string(body), "incident", key)
logger.Debug(string(body))
var weResp weChatResponse
if err := json.Unmarshal(body, &weResp); err != nil {