2019-06-18 13:34:46 +00:00
|
|
|
// Copyright 2019 Prometheus Team
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package slack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2020-02-05 08:17:15 +00:00
|
|
|
"fmt"
|
2023-04-28 13:05:56 +00:00
|
|
|
"io"
|
2019-06-18 13:34:46 +00:00
|
|
|
"net/http"
|
2022-07-18 12:25:32 +00:00
|
|
|
"os"
|
2022-09-09 14:37:31 +00:00
|
|
|
"strings"
|
2019-06-18 13:34:46 +00:00
|
|
|
|
2021-07-30 08:11:43 +00:00
|
|
|
"github.com/go-kit/log"
|
2022-03-25 16:59:51 +00:00
|
|
|
"github.com/go-kit/log/level"
|
|
|
|
"github.com/pkg/errors"
|
2019-06-18 13:34:46 +00:00
|
|
|
commoncfg "github.com/prometheus/common/config"
|
|
|
|
|
|
|
|
"github.com/prometheus/alertmanager/config"
|
|
|
|
"github.com/prometheus/alertmanager/notify"
|
|
|
|
"github.com/prometheus/alertmanager/template"
|
|
|
|
"github.com/prometheus/alertmanager/types"
|
|
|
|
)
|
|
|
|
|
2022-11-25 19:47:20 +00:00
|
|
|
// https://api.slack.com/reference/messaging/attachments#legacy_fields - 1024, no units given, assuming runes or characters.
|
|
|
|
const maxTitleLenRunes = 1024
|
|
|
|
|
2019-06-18 13:34:46 +00:00
|
|
|
// Notifier implements a Notifier for Slack notifications.
|
|
|
|
type Notifier struct {
|
2019-08-02 14:17:40 +00:00
|
|
|
conf *config.SlackConfig
|
|
|
|
tmpl *template.Template
|
|
|
|
logger log.Logger
|
|
|
|
client *http.Client
|
|
|
|
retrier *notify.Retrier
|
2023-04-28 13:05:56 +00:00
|
|
|
|
|
|
|
postJSONFunc func(ctx context.Context, client *http.Client, url string, body io.Reader) (*http.Response, error)
|
2019-06-18 13:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new Slack notification handler.
|
2021-04-16 09:30:31 +00:00
|
|
|
func New(c *config.SlackConfig, t *template.Template, l log.Logger, httpOpts ...commoncfg.HTTPClientOption) (*Notifier, error) {
|
2021-11-10 16:28:47 +00:00
|
|
|
client, err := commoncfg.NewClientFromConfig(*c.HTTPConfig, "slack", httpOpts...)
|
2019-06-18 13:34:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Notifier{
|
2023-04-28 13:05:56 +00:00
|
|
|
conf: c,
|
|
|
|
tmpl: t,
|
|
|
|
logger: l,
|
|
|
|
client: client,
|
|
|
|
retrier: ¬ify.Retrier{},
|
|
|
|
postJSONFunc: notify.PostJSON,
|
2019-06-18 13:34:46 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// request is the request for sending a slack notification.
|
|
|
|
type request struct {
|
|
|
|
Channel string `json:"channel,omitempty"`
|
|
|
|
Username string `json:"username,omitempty"`
|
|
|
|
IconEmoji string `json:"icon_emoji,omitempty"`
|
|
|
|
IconURL string `json:"icon_url,omitempty"`
|
|
|
|
LinkNames bool `json:"link_names,omitempty"`
|
|
|
|
Attachments []attachment `json:"attachments"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// attachment is used to display a richly-formatted message block.
|
|
|
|
type attachment struct {
|
|
|
|
Title string `json:"title,omitempty"`
|
|
|
|
TitleLink string `json:"title_link,omitempty"`
|
|
|
|
Pretext string `json:"pretext,omitempty"`
|
|
|
|
Text string `json:"text"`
|
|
|
|
Fallback string `json:"fallback"`
|
|
|
|
CallbackID string `json:"callback_id"`
|
|
|
|
Fields []config.SlackField `json:"fields,omitempty"`
|
|
|
|
Actions []config.SlackAction `json:"actions,omitempty"`
|
|
|
|
ImageURL string `json:"image_url,omitempty"`
|
|
|
|
ThumbURL string `json:"thumb_url,omitempty"`
|
|
|
|
Footer string `json:"footer"`
|
2019-07-31 10:04:59 +00:00
|
|
|
Color string `json:"color,omitempty"`
|
|
|
|
MrkdwnIn []string `json:"mrkdwn_in,omitempty"`
|
2019-06-18 13:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Notify implements the Notifier interface.
|
|
|
|
func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
|
|
|
|
var err error
|
|
|
|
var (
|
|
|
|
data = notify.GetTemplateData(ctx, n.tmpl, as, n.logger)
|
|
|
|
tmplText = notify.TmplText(n.tmpl, data, &err)
|
|
|
|
)
|
2019-07-31 10:04:59 +00:00
|
|
|
var markdownIn []string
|
2021-11-26 07:27:53 +00:00
|
|
|
|
2019-07-31 10:04:59 +00:00
|
|
|
if len(n.conf.MrkdwnIn) == 0 {
|
|
|
|
markdownIn = []string{"fallback", "pretext", "text"}
|
|
|
|
} else {
|
|
|
|
markdownIn = n.conf.MrkdwnIn
|
|
|
|
}
|
2022-11-25 19:47:20 +00:00
|
|
|
|
|
|
|
title, truncated := notify.TruncateInRunes(tmplText(n.conf.Title), maxTitleLenRunes)
|
2021-11-26 07:27:53 +00:00
|
|
|
if truncated {
|
|
|
|
key, err := notify.ExtractGroupKey(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2022-12-13 23:26:14 +00:00
|
|
|
level.Warn(n.logger).Log("msg", "Truncated title", "key", key, "max_runes", maxTitleLenRunes)
|
2021-11-26 07:27:53 +00:00
|
|
|
}
|
2019-06-18 13:34:46 +00:00
|
|
|
att := &attachment{
|
2021-11-26 07:27:53 +00:00
|
|
|
Title: title,
|
2019-06-18 13:34:46 +00:00
|
|
|
TitleLink: tmplText(n.conf.TitleLink),
|
|
|
|
Pretext: tmplText(n.conf.Pretext),
|
|
|
|
Text: tmplText(n.conf.Text),
|
|
|
|
Fallback: tmplText(n.conf.Fallback),
|
|
|
|
CallbackID: tmplText(n.conf.CallbackID),
|
|
|
|
ImageURL: tmplText(n.conf.ImageURL),
|
|
|
|
ThumbURL: tmplText(n.conf.ThumbURL),
|
|
|
|
Footer: tmplText(n.conf.Footer),
|
|
|
|
Color: tmplText(n.conf.Color),
|
2019-07-31 10:04:59 +00:00
|
|
|
MrkdwnIn: markdownIn,
|
2019-06-18 13:34:46 +00:00
|
|
|
}
|
|
|
|
|
2022-03-25 16:59:51 +00:00
|
|
|
numFields := len(n.conf.Fields)
|
2019-06-18 13:34:46 +00:00
|
|
|
if numFields > 0 {
|
2022-03-25 16:59:51 +00:00
|
|
|
fields := make([]config.SlackField, numFields)
|
2019-06-18 13:34:46 +00:00
|
|
|
for index, field := range n.conf.Fields {
|
|
|
|
// Check if short was defined for the field otherwise fallback to the global setting
|
|
|
|
var short bool
|
|
|
|
if field.Short != nil {
|
|
|
|
short = *field.Short
|
|
|
|
} else {
|
|
|
|
short = n.conf.ShortFields
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rebuild the field by executing any templates and setting the new value for short
|
|
|
|
fields[index] = config.SlackField{
|
|
|
|
Title: tmplText(field.Title),
|
|
|
|
Value: tmplText(field.Value),
|
|
|
|
Short: &short,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
att.Fields = fields
|
|
|
|
}
|
|
|
|
|
2022-03-25 16:59:51 +00:00
|
|
|
numActions := len(n.conf.Actions)
|
2019-06-18 13:34:46 +00:00
|
|
|
if numActions > 0 {
|
2022-03-25 16:59:51 +00:00
|
|
|
actions := make([]config.SlackAction, numActions)
|
2019-06-18 13:34:46 +00:00
|
|
|
for index, action := range n.conf.Actions {
|
|
|
|
slackAction := config.SlackAction{
|
|
|
|
Type: tmplText(action.Type),
|
|
|
|
Text: tmplText(action.Text),
|
|
|
|
URL: tmplText(action.URL),
|
|
|
|
Style: tmplText(action.Style),
|
|
|
|
Name: tmplText(action.Name),
|
|
|
|
Value: tmplText(action.Value),
|
|
|
|
}
|
|
|
|
|
|
|
|
if action.ConfirmField != nil {
|
|
|
|
slackAction.ConfirmField = &config.SlackConfirmationField{
|
|
|
|
Title: tmplText(action.ConfirmField.Title),
|
|
|
|
Text: tmplText(action.ConfirmField.Text),
|
|
|
|
OkText: tmplText(action.ConfirmField.OkText),
|
|
|
|
DismissText: tmplText(action.ConfirmField.DismissText),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
actions[index] = slackAction
|
|
|
|
}
|
|
|
|
att.Actions = actions
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &request{
|
|
|
|
Channel: tmplText(n.conf.Channel),
|
|
|
|
Username: tmplText(n.conf.Username),
|
|
|
|
IconEmoji: tmplText(n.conf.IconEmoji),
|
|
|
|
IconURL: tmplText(n.conf.IconURL),
|
|
|
|
LinkNames: n.conf.LinkNames,
|
|
|
|
Attachments: []attachment{*att},
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := json.NewEncoder(&buf).Encode(req); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2021-04-02 01:57:55 +00:00
|
|
|
var u string
|
|
|
|
if n.conf.APIURL != nil {
|
|
|
|
u = n.conf.APIURL.String()
|
|
|
|
} else {
|
2022-07-18 12:25:32 +00:00
|
|
|
content, err := os.ReadFile(n.conf.APIURLFile)
|
2021-04-02 01:57:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2022-09-09 14:37:31 +00:00
|
|
|
u = strings.TrimSpace(string(content))
|
2021-04-02 01:57:55 +00:00
|
|
|
}
|
|
|
|
|
2023-04-28 13:05:56 +00:00
|
|
|
resp, err := n.postJSONFunc(ctx, n.client, u, &buf)
|
2019-06-18 13:34:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return true, notify.RedactURL(err)
|
|
|
|
}
|
|
|
|
defer notify.Drain(resp)
|
|
|
|
|
|
|
|
// Only 5xx response codes are recoverable and 2xx codes are successful.
|
|
|
|
// https://api.slack.com/incoming-webhooks#handling_errors
|
|
|
|
// https://api.slack.com/changelog/2016-05-17-changes-to-errors-for-incoming-webhooks
|
2020-02-05 08:17:15 +00:00
|
|
|
retry, err := n.retrier.Check(resp.StatusCode, resp.Body)
|
2023-05-02 16:59:37 +00:00
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, fmt.Sprintf("channel %q", req.Channel))
|
|
|
|
return retry, notify.NewErrorWithReason(notify.GetFailureReasonFromStatusCode(resp.StatusCode), err)
|
|
|
|
}
|
2023-04-28 13:05:56 +00:00
|
|
|
|
2023-05-02 16:59:37 +00:00
|
|
|
return false, nil
|
2019-06-18 13:34:46 +00:00
|
|
|
}
|