Skip to content

Commit

Permalink
adding max_alerts parameter to slack webhook config
Browse files Browse the repository at this point in the history
Signed-off-by: Prashant Balachandran <pnair@redhat.com>

correcting formatting errors
  • Loading branch information
Prashant Balachandran committed Nov 26, 2021
1 parent e2a1011 commit b83a629
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 5 additions & 0 deletions config/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,11 @@ type SlackConfig struct {

HTTPConfig *commoncfg.HTTPClientConfig `yaml:"http_config,omitempty" json:"http_config,omitempty"`

// MaxAlerts is the maximum number of alerts to be sent per webhook message.
// Alerts exceeding this threshold will be truncated. Setting this to 0
// allows an unlimited number of alerts.
MaxAlerts uint64 `yaml:"max_alerts" json:"max_alerts"`

APIURL *SecretURL `yaml:"api_url,omitempty" json:"api_url,omitempty"`
APIURLFile string `yaml:"api_url_file,omitempty" json:"api_url_file,omitempty"`

Expand Down
10 changes: 9 additions & 1 deletion notify/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,19 @@ type attachment struct {
MrkdwnIn []string `json:"mrkdwn_in,omitempty"`
}

func truncateAlerts(maxAlerts uint64, alerts []*types.Alert) []*types.Alert {
if maxAlerts != 0 && uint64(len(alerts)) > maxAlerts {
return alerts[:maxAlerts]
}
return alerts
}

// Notify implements the Notifier interface.
func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
alerts := truncateAlerts(n.conf.MaxAlerts, as)
var err error
var (
data = notify.GetTemplateData(ctx, n.tmpl, as, n.logger)
data = notify.GetTemplateData(ctx, n.tmpl, alerts, n.logger)
tmplText = notify.TmplText(n.tmpl, data, &err)
)
var markdownIn []string
Expand Down

0 comments on commit b83a629

Please sign in to comment.