mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
feat(log): wire log db, metrics, and body toggle
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user