fix(ops): bound indexed log host length

Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
bestony
2026-07-14 01:29:47 +08:00
co-authored by multica-agent
parent 2c2e50ba58
commit 0f2ec134b5
2 changed files with 33 additions and 5 deletions
@@ -46,15 +46,14 @@ type OpsSystemLogSink struct {
lastError atomic.Value
}
const maxSystemLogHostLength = 255
func NewOpsSystemLogSink(opsRepo OpsRepository) *OpsSystemLogSink {
ctx, cancel := context.WithCancel(context.Background())
host, err := os.Hostname()
if err != nil || strings.TrimSpace(host) == "" {
host = "unknown"
}
rawHost, err := os.Hostname()
s := &OpsSystemLogSink{
opsRepo: opsRepo,
host: strings.TrimSpace(host),
host: normalizeSystemLogHost(rawHost, err),
queue: make(chan *logger.LogEvent, 5000),
batchSize: 200,
flushInterval: time.Second,
@@ -65,6 +64,18 @@ func NewOpsSystemLogSink(opsRepo OpsRepository) *OpsSystemLogSink {
return s
}
func normalizeSystemLogHost(host string, err error) string {
host = strings.TrimSpace(host)
if err != nil || host == "" {
return "unknown"
}
runes := []rune(host)
if len(runes) > maxSystemLogHostLength {
return string(runes[:maxSystemLogHostLength])
}
return host
}
func (s *OpsSystemLogSink) Start() {
if s == nil || s.opsRepo == nil {
return
@@ -328,3 +328,20 @@ func TestOpsSystemLogSink_HelperFunctions(t *testing.T) {
}
}
}
func TestNormalizeSystemLogHost(t *testing.T) {
if got := normalizeSystemLogHost(" api-node-1 ", nil); got != "api-node-1" {
t.Fatalf("trimmed host = %q, want api-node-1", got)
}
if got := normalizeSystemLogHost("", nil); got != "unknown" {
t.Fatalf("empty host = %q, want unknown", got)
}
if got := normalizeSystemLogHost("api-node-1", errors.New("hostname unavailable")); got != "unknown" {
t.Fatalf("errored host = %q, want unknown", got)
}
longHost := strings.Repeat("节", maxSystemLogHostLength+1)
got := normalizeSystemLogHost(longHost, nil)
if runeCount := len([]rune(got)); runeCount != maxSystemLogHostLength {
t.Fatalf("truncated host rune count = %d, want %d", runeCount, maxSystemLogHostLength)
}
}