From 35d51435f42fa897e9724656df06100ad8c61a5e Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Tue, 6 Sep 2022 19:29:18 +0200 Subject: [PATCH] Alerting: Telegram: truncate long messages (#54339) (#54796) Truncate messages longer than 4096 characters (cherry picked from commit b593d371effec967358bf092481d0509ab02e5b3) Co-authored-by: Ilya Galimyanov --- .../ngalert/notifier/channels/telegram.go | 9 ++++++++- .../notifier/channels/telegram_test.go | 20 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/pkg/services/ngalert/notifier/channels/telegram.go b/pkg/services/ngalert/notifier/channels/telegram.go index 650a390c0da..b28c621f009 100644 --- a/pkg/services/ngalert/notifier/channels/telegram.go +++ b/pkg/services/ngalert/notifier/channels/telegram.go @@ -9,6 +9,7 @@ import ( "mime/multipart" "os" + "github.com/prometheus/alertmanager/notify" "github.com/prometheus/alertmanager/template" "github.com/prometheus/alertmanager/types" @@ -158,8 +159,14 @@ func (tn *TelegramNotifier) buildTelegramMessage(ctx context.Context, as []*type }() tmpl, _ := TmplText(ctx, tn.tmpl, as, tn.log, &tmplErr) + // Telegram supports 4096 chars max + messageText, truncated := notify.Truncate(tmpl(tn.Message), 4096) + if truncated { + tn.log.Warn("Telegram message too long, truncate message", "original_message", tn.Message) + } + m := make(map[string]string) - m["text"] = tmpl(tn.Message) + m["text"] = messageText m["parse_mode"] = "html" return m, nil } diff --git a/pkg/services/ngalert/notifier/channels/telegram_test.go b/pkg/services/ngalert/notifier/channels/telegram_test.go index 7d92125dcf2..f0a61692958 100644 --- a/pkg/services/ngalert/notifier/channels/telegram_test.go +++ b/pkg/services/ngalert/notifier/channels/telegram_test.go @@ -3,6 +3,7 @@ package channels import ( "context" "net/url" + "strings" "testing" "github.com/grafana/grafana/pkg/components/simplejson" @@ -76,6 +77,25 @@ func TestTelegramNotifier(t *testing.T) { "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", }, expMsgError: nil, + }, { + name: "Truncate long message", + settings: `{ + "bottoken": "abcdefgh0123456789", + "chatid": "someid", + "message": "{{ .CommonLabels.alertname }}" + }`, + alerts: []*types.Alert{ + { + Alert: model.Alert{ + Labels: model.LabelSet{"alertname": model.LabelValue(strings.Repeat("1", 4097))}, + }, + }, + }, + expMsg: map[string]string{ + "parse_mode": "html", + "text": strings.Repeat("1", 4096-3) + "...", + }, + expMsgError: nil, }, { name: "Error in initing", settings: `{}`,