mirror of
https://github.com/grafana/grafana.git
synced 2025-02-12 00:25:46 -06:00
Alerting: Telegram: truncate long messages (#54339)
Truncate messages longer than 4096 characters
This commit is contained in:
parent
a4566eaf32
commit
b593d371ef
@ -9,6 +9,7 @@ import (
|
|||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/prometheus/alertmanager/notify"
|
||||||
"github.com/prometheus/alertmanager/template"
|
"github.com/prometheus/alertmanager/template"
|
||||||
"github.com/prometheus/alertmanager/types"
|
"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)
|
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 := make(map[string]string)
|
||||||
m["text"] = tmpl(tn.Message)
|
m["text"] = messageText
|
||||||
m["parse_mode"] = "html"
|
m["parse_mode"] = "html"
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package channels
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"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",
|
"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,
|
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",
|
name: "Error in initing",
|
||||||
settings: `{}`,
|
settings: `{}`,
|
||||||
|
Loading…
Reference in New Issue
Block a user