mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-09-21 14:19:18 +08:00
fix(ops): bound indexed log host length
Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user