feat(log): wire log db, metrics, and body toggle

This commit is contained in:
zenfun
2025-12-21 16:18:22 +08:00
parent 4c1e03f83d
commit c2c65e774b
9 changed files with 305 additions and 40 deletions

View File

@@ -2,6 +2,7 @@ package service
import (
"context"
"expvar"
"log/slog"
"time"
@@ -9,6 +10,11 @@ import (
"gorm.io/gorm"
)
var (
logBatchWriteTotal = expvar.NewInt("log_write_batch_total")
logQueueDropped = expvar.NewInt("log_queue_dropped_total")
)
// LogWriter batches log records to reduce IO overhead.
type LogWriter struct {
ch chan model.LogRecord
@@ -48,6 +54,8 @@ func (w *LogWriter) Start(ctx context.Context) {
}
if err := w.db.Create(&buf).Error; err != nil {
slog.Default().Error("log batch insert failed", "err", err)
} else {
logBatchWriteTotal.Add(1)
}
buf = buf[:0]
}
@@ -75,5 +83,6 @@ func (w *LogWriter) Write(rec model.LogRecord) {
case w.ch <- rec:
default:
// drop to avoid blocking hot path
logQueueDropped.Add(1)
}
}