Skip to content

Commit

Permalink
refactor: ensure proper UTF-8 character boundaries when truncating sc…
Browse files Browse the repository at this point in the history
…ript output
  • Loading branch information
710leo committed Feb 25, 2025
1 parent ab49b13 commit ef04300
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions alert/sender/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"os/exec"
"time"
"unicode/utf8"

"github.com/ccfos/nightingale/v6/alert/astats"
"github.com/ccfos/nightingale/v6/models"
Expand Down Expand Up @@ -87,10 +88,20 @@ func alertingCallScript(ctx *ctx.Context, stdinBytes []byte, notifyScript models
err, isTimeout := sys.WrapTimeout(cmd, time.Duration(config.Timeout)*time.Second)

res := buf.String()

// 截断超出长度的输出
if len(res) > 512 {
res = res[:512] + "..."
// 确保在有效的UTF-8字符边界处截断
validLen := 0
for i := 0; i < 512 && i < len(res); {
_, size := utf8.DecodeRuneInString(res[i:])
if i+size > 512 {
break
}
i += size
validLen = i
}
res = res[:validLen] + "..."
}

NotifyRecord(ctx, []*models.AlertCurEvent{event}, channel, cmd.String(), res, buildErr(err, isTimeout))
Expand Down

0 comments on commit ef04300

Please sign in to comment.