Use 1024 rather than 1000 for KB size, fix target arn, handle large SMS messages correctly

Signed-off-by: Tyler Reid <tyler.reid@grafana.com>
This commit is contained in:
Tyler Reid 2021-06-14 10:21:33 -05:00
parent 74d15273c0
commit 009f8b17e9
1 changed files with 7 additions and 7 deletions

View File

@ -84,16 +84,16 @@ func (n Notifier) Notify(ctx context.Context, alert ...*types.Alert) (bool, erro
} }
if n.conf.PhoneNumber != "" { if n.conf.PhoneNumber != "" {
publishInput.SetPhoneNumber(n.conf.PhoneNumber) publishInput.SetPhoneNumber(n.conf.PhoneNumber)
// Truncate for SMS // If SMS message is over 1600 chars, SNS will reject the message.
trunc, isTruncated := notify.Truncate(message, 140) _, isTruncated := notify.Truncate(message, 1600)
if isTruncated { if isTruncated {
publishInput.SetMessage(trunc) return false, fmt.Errorf("SMS message exeeds length of 1600 charactors")
} else { } else {
publishInput.SetMessage(message) publishInput.SetMessage(message)
} }
} }
if n.conf.TopicARN != "" { if n.conf.TargetARN != "" {
publishInput.SetTopicArn(n.conf.TopicARN) publishInput.SetTargetArn(n.conf.TargetARN)
messageToSend, isTrunc, err := validateAndTruncateMessage(message) messageToSend, isTrunc, err := validateAndTruncateMessage(message)
if err != nil { if err != nil {
return false, err return false, err
@ -143,8 +143,8 @@ func (n Notifier) Notify(ctx context.Context, alert ...*types.Alert) (bool, erro
func validateAndTruncateMessage(message string) (string, bool, error) { func validateAndTruncateMessage(message string) (string, bool, error) {
if utf8.ValidString(message) { if utf8.ValidString(message) {
// if the message is larger than 256KB we have to truncate. // if the message is larger than 256KB we have to truncate.
if len(message) > 256*1000 { if len(message) > 256*1024 {
truncated := make([]byte, 256*1000, 256*1000) truncated := make([]byte, 256*1024, 256*1024)
copy(truncated, message) copy(truncated, message)
return string(truncated), true, nil return string(truncated), true, nil
} }