Alerting: Add support for images in Telegram (#51433)

This commit is contained in:
George Robinson 2022-06-28 09:39:40 +01:00 committed by GitHub
parent a7f1ca133e
commit 5053468c65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 137 additions and 90 deletions

View File

@ -5,18 +5,21 @@ import (
"context"
"errors"
"fmt"
"io"
"mime/multipart"
"os"
"github.com/prometheus/alertmanager/template"
"github.com/prometheus/alertmanager/types"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models"
"github.com/grafana/grafana/pkg/services/notifications"
)
var (
TelegramAPIURL = "https://api.telegram.org/bot%s/sendMessage"
TelegramAPIURL = "https://api.telegram.org/bot%s/%s"
)
// TelegramNotifier is responsible for sending
@ -27,6 +30,7 @@ type TelegramNotifier struct {
ChatID string
Message string
log log.Logger
images ImageStore
ns notifications.WebhookSender
tmpl *template.Template
}
@ -46,7 +50,7 @@ func TelegramFactory(fc FactoryConfig) (NotificationChannel, error) {
Cfg: *fc.Config,
}
}
return NewTelegramNotifier(config, fc.NotificationService, fc.Template), nil
return NewTelegramNotifier(config, fc.ImageStore, fc.NotificationService, fc.Template), nil
}
func NewTelegramConfig(config *NotificationChannelConfig, fn GetDecryptedValueFn) (*TelegramConfig, error) {
@ -67,7 +71,7 @@ func NewTelegramConfig(config *NotificationChannelConfig, fn GetDecryptedValueFn
}
// NewTelegramNotifier is the constructor for the Telegram notifier
func NewTelegramNotifier(config *TelegramConfig, ns notifications.WebhookSender, t *template.Template) *TelegramNotifier {
func NewTelegramNotifier(config *TelegramConfig, images ImageStore, ns notifications.WebhookSender, t *template.Template) *TelegramNotifier {
return &TelegramNotifier{
Base: NewBase(&models.AlertNotification{
Uid: config.UID,
@ -81,89 +85,123 @@ func NewTelegramNotifier(config *TelegramConfig, ns notifications.WebhookSender,
Message: config.Message,
tmpl: t,
log: log.New("alerting.notifier.telegram"),
images: images,
ns: ns,
}
}
// Notify send an alert notification to Telegram.
func (tn *TelegramNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
msg, err := tn.buildTelegramMessage(ctx, as)
if err != nil {
return false, err
}
var body bytes.Buffer
w := multipart.NewWriter(&body)
defer func() {
if err := w.Close(); err != nil {
tn.log.Warn("failed to close writer", "err", err)
}
}()
boundary := GetBoundary()
if boundary != "" {
err = w.SetBoundary(boundary)
// Create the cmd for sendMessage
cmd, err := tn.newWebhookSyncCmd("sendMessage", func(w *multipart.Writer) error {
msg, err := tn.buildTelegramMessage(ctx, as)
if err != nil {
return false, err
return fmt.Errorf("failed to build message: %w", err)
}
}
for k, v := range msg {
if err := writeField(w, k, v); err != nil {
return false, err
for k, v := range msg {
fw, err := w.CreateFormField(k)
if err != nil {
return fmt.Errorf("failed to create form field: %w", err)
}
if _, err := fw.Write([]byte(v)); err != nil {
return fmt.Errorf("failed to write value: %w", err)
}
}
return nil
})
if err != nil {
return false, fmt.Errorf("failed to create telegram message: %w", err)
}
// We need to close it before using so that the last part
// is added to the writer along with the boundary.
if err := w.Close(); err != nil {
return false, err
}
tn.log.Info("sending telegram notification", "chat_id", msg["chat_id"])
cmd := &models.SendWebhookSync{
Url: fmt.Sprintf(TelegramAPIURL, tn.BotToken),
Body: body.String(),
HttpMethod: "POST",
HttpHeader: map[string]string{
"Content-Type": w.FormDataContentType(),
},
}
if err := tn.ns.SendWebhookSync(ctx, cmd); err != nil {
tn.log.Error("failed to send webhook", "err", err, "webhook", tn.Name)
return false, err
return false, fmt.Errorf("failed to send telegram message: %w", err)
}
// Create the cmd to upload each image
_ = withStoredImages(ctx, tn.log, tn.images, func(index int, image *ngmodels.Image) error {
if image != nil {
cmd, err = tn.newWebhookSyncCmd("sendPhoto", func(w *multipart.Writer) error {
f, err := os.Open(image.Path)
if err != nil {
return fmt.Errorf("failed to open image: %w", err)
}
defer func() {
if err := f.Close(); err != nil {
tn.log.Warn("failed to close image", "err", err)
}
}()
fw, err := w.CreateFormFile("photo", image.Path)
if err != nil {
return fmt.Errorf("failed to create form file: %w", err)
}
if _, err := io.Copy(fw, f); err != nil {
return fmt.Errorf("failed to write to form file: %w", err)
}
return nil
})
if err != nil {
return fmt.Errorf("failed to create image: %w", err)
}
if err := tn.ns.SendWebhookSync(ctx, cmd); err != nil {
return fmt.Errorf("failed to upload image to telegram: %w", err)
}
}
return nil
}, as...)
return true, nil
}
func (tn *TelegramNotifier) buildTelegramMessage(ctx context.Context, as []*types.Alert) (map[string]string, error) {
var tmplErr error
defer func() {
if tmplErr != nil {
tn.log.Warn("failed to template Telegram message", "err", tmplErr)
}
}()
tmpl, _ := TmplText(ctx, tn.tmpl, as, tn.log, &tmplErr)
msg := map[string]string{}
msg["chat_id"] = tmpl(tn.ChatID)
msg["parse_mode"] = "html"
message := tmpl(tn.Message)
if tmplErr != nil {
tn.log.Warn("failed to template Telegram message", "err", tmplErr.Error())
}
msg["text"] = message
return msg, nil
m := make(map[string]string)
m["text"] = tmpl(tn.Message)
m["parse_mode"] = "html"
return m, nil
}
func writeField(w *multipart.Writer, name, value string) error {
fw, err := w.CreateFormField(name)
func (tn *TelegramNotifier) newWebhookSyncCmd(action string, fn func(writer *multipart.Writer) error) (*models.SendWebhookSync, error) {
b := bytes.Buffer{}
w := multipart.NewWriter(&b)
boundary := GetBoundary()
if boundary != "" {
if err := w.SetBoundary(boundary); err != nil {
return nil, err
}
}
fw, err := w.CreateFormField("chat_id")
if err != nil {
return err
return nil, err
}
if _, err := fw.Write([]byte(value)); err != nil {
return err
if _, err := fw.Write([]byte(tn.ChatID)); err != nil {
return nil, err
}
return nil
if err := fn(w); err != nil {
return nil, err
}
if err := w.Close(); err != nil {
return nil, fmt.Errorf("failed to close multipart: %w", err)
}
cmd := &models.SendWebhookSync{
Url: fmt.Sprintf(TelegramAPIURL, tn.BotToken, action),
Body: b.String(),
HttpMethod: "POST",
HttpHeader: map[string]string{
"Content-Type": w.FormDataContentType(),
},
}
return cmd, nil
}
func (tn *TelegramNotifier) SendResolved() bool {

View File

@ -17,7 +17,7 @@ import (
func TestTelegramNotifier(t *testing.T) {
tmpl := templateForTests(t)
images := newFakeImageStoreWithFile(t, 2)
externalURL, err := url.Parse("http://localhost")
require.NoError(t, err)
tmpl.ExternalURL = externalURL
@ -31,7 +31,7 @@ func TestTelegramNotifier(t *testing.T) {
expMsgError error
}{
{
name: "Default template with one alert",
name: "A single alert with default template",
settings: `{
"bottoken": "abcdefgh0123456789",
"chatid": "someid"
@ -40,19 +40,18 @@ func TestTelegramNotifier(t *testing.T) {
{
Alert: model.Alert{
Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val1"},
Annotations: model.LabelSet{"ann1": "annv1", "__dashboardUid__": "abcd", "__panelId__": "efgh"},
Annotations: model.LabelSet{"ann1": "annv1", "__dashboardUid__": "abcd", "__panelId__": "efgh", "__alertScreenshotToken__": "test-image-1"},
GeneratorURL: "a URL",
},
},
},
expMsg: map[string]string{
"chat_id": "someid",
"parse_mode": "html",
"text": "**Firing**\n\nValue: [no value]\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: a URL\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matcher=alertname%3Dalert1&matcher=lbl1%3Dval1\nDashboard: http://localhost/d/abcd\nPanel: http://localhost/d/abcd?viewPanel=efgh\n",
},
expMsgError: nil,
}, {
name: "Custom template with multiple alerts",
name: "Multiple alerts with custom template",
settings: `{
"bottoken": "abcdefgh0123456789",
"chatid": "someid",
@ -62,18 +61,17 @@ func TestTelegramNotifier(t *testing.T) {
{
Alert: model.Alert{
Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val1"},
Annotations: model.LabelSet{"ann1": "annv1"},
Annotations: model.LabelSet{"ann1": "annv1", "__alertScreenshotToken__": "test-image-1"},
GeneratorURL: "a URL",
},
}, {
Alert: model.Alert{
Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val2"},
Annotations: model.LabelSet{"ann1": "annv2"},
Annotations: model.LabelSet{"ann1": "annv2", "__alertScreenshotToken__": "test-image-2"},
},
},
},
expMsg: map[string]string{
"chat_id": "someid",
"parse_mode": "html",
"text": "__Custom Firing__\n2 Firing\n\nValue: [no value]\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: a URL\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matcher=alertname%3Dalert1&matcher=lbl1%3Dval1\n\nValue: [no value]\nLabels:\n - alertname = alert1\n - lbl1 = val2\nAnnotations:\n - ann1 = annv2\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matcher=alertname%3Dalert1&matcher=lbl1%3Dval2\n",
},
@ -91,34 +89,45 @@ func TestTelegramNotifier(t *testing.T) {
require.NoError(t, err)
secureSettings := make(map[string][]byte)
m := &NotificationChannelConfig{
Name: "telegram_testing",
Type: "telegram",
Settings: settingsJSON,
SecureSettings: secureSettings,
}
webhookSender := mockNotificationService()
secretsService := secretsManager.SetupTestService(t, fakes.NewFakeSecretsStore())
decryptFn := secretsService.GetDecryptedValue
cfg, err := NewTelegramConfig(m, decryptFn)
notificationService := mockNotificationService()
fc := FactoryConfig{
Config: &NotificationChannelConfig{
Name: "telegram_tests",
Type: "telegram",
Settings: settingsJSON,
SecureSettings: secureSettings,
},
ImageStore: images,
NotificationService: notificationService,
DecryptFunc: decryptFn,
}
cfg, err := NewTelegramConfig(fc.Config, decryptFn)
if c.expInitError != "" {
require.Error(t, err)
require.Equal(t, c.expInitError, err.Error())
return
}
require.NoError(t, err)
n := NewTelegramNotifier(cfg, images, notificationService, tmpl)
ctx := notify.WithGroupKey(context.Background(), "alertname")
ctx = notify.WithGroupLabels(ctx, model.LabelSet{"alertname": ""})
pn := NewTelegramNotifier(cfg, webhookSender, tmpl)
msg, err := pn.buildTelegramMessage(ctx, c.alerts)
ok, err := n.Notify(ctx, c.alerts...)
require.NoError(t, err)
require.True(t, ok)
msg, err := n.buildTelegramMessage(ctx, c.alerts)
if c.expMsgError != nil {
require.Error(t, err)
require.Equal(t, c.expMsgError.Error(), err.Error())
return
}
require.NoError(t, err)
require.Equal(t, c.expMsg, msg)
})
}

View File

@ -721,7 +721,7 @@ func TestNotificationChannels(t *testing.T) {
channels.DefaultTemplateString = channels.TemplateForTestsString
channels.SlackAPIEndpoint = fmt.Sprintf("http://%s/slack_recvX/slack_testX", mockChannel.server.Addr)
channels.PagerdutyEventAPIURL = fmt.Sprintf("http://%s/pagerduty_recvX/pagerduty_testX", mockChannel.server.Addr)
channels.TelegramAPIURL = fmt.Sprintf("http://%s/telegram_recv/bot%%s", mockChannel.server.Addr)
channels.TelegramAPIURL = fmt.Sprintf("http://%s/telegram_recv/bot%%s/%%s", mockChannel.server.Addr)
channels.PushoverEndpoint = fmt.Sprintf("http://%s/pushover_recv/pushover_test", mockChannel.server.Addr)
channels.LineNotifyURL = fmt.Sprintf("http://%s/line_recv/line_test", mockChannel.server.Addr)
channels.ThreemaGwBaseURL = fmt.Sprintf("http://%s/threema_recv/threema_test", mockChannel.server.Addr)
@ -932,8 +932,8 @@ func (nc *mockNotificationChannel) ServeHTTP(res http.ResponseWriter, req *http.
nc.receivedNotificationsMtx.Lock()
defer nc.receivedNotificationsMtx.Unlock()
urlParts := strings.Split(req.URL.String(), "/")
key := fmt.Sprintf("%s/%s", urlParts[len(urlParts)-2], urlParts[len(urlParts)-1])
paths := strings.Split(req.URL.Path[1:], "/")
key := strings.Join(paths[0:2], "/")
body := getBody(nc.t, req.Body)
nc.receivedNotifications[key] = append(nc.receivedNotifications[key], body)
@ -973,7 +973,7 @@ func (nc *mockNotificationChannel) matchesExpNotifications(t *testing.T, exp map
case "slack_recv1/slack_test_without_token":
// It has a time component "ts".
r1 = regexp.MustCompile(`.*"ts"\s*:\s*([0-9]+)`)
case "sensugo/events":
case "sensugo_recv/sensugo_test":
// It has a time component "ts".
r1 = regexp.MustCompile(`.*"issued"\s*:\s*([0-9]+)`)
case "pagerduty_recvX/pagerduty_testX":
@ -985,7 +985,7 @@ func (nc *mockNotificationChannel) matchesExpNotifications(t *testing.T, exp map
case "victorops_recv/victorops_test":
// It has a time component "timestamp".
r1 = regexp.MustCompile(`.*"timestamp"\s*:\s*([0-9]+)`)
case "v1/alerts":
case "alertmanager_recv/alertmanager_test":
// It has a changing time fields.
r1 = regexp.MustCompile(`.*"startsAt"\s*:\s*"([^"]+)"`)
r2 = regexp.MustCompile(`.*"UpdatedAt"\s*:\s*"([^"]+)"`)
@ -993,7 +993,7 @@ func (nc *mockNotificationChannel) matchesExpNotifications(t *testing.T, exp map
if r1 != nil {
parts := r1.FindStringSubmatch(actVals[i])
require.Len(t, parts, 2)
if expKey == "v1/alerts" {
if expKey == "alertmanager_recv/alertmanager_test" {
// 2 fields for Prometheus Alertmanager.
parts2 := r2.FindStringSubmatch(actVals[i])
require.Len(t, parts2, 2)
@ -2277,7 +2277,7 @@ var expNonEmailNotifications = map[string][]string{
"username": "Grafana"
}`,
},
"sensugo/events": {
"sensugo_recv/sensugo_test": {
`{
"check": {
"handlers": null,
@ -2396,7 +2396,7 @@ var expNonEmailNotifications = map[string][]string{
}`,
},
// Prometheus Alertmanager.
"v1/alerts": {
"alertmanager_recv/alertmanager_test": {
`[
{
"labels": {