Reduce API log noise
This commit is contained in:
+28
-2
@@ -332,7 +332,33 @@ func writeError(w http.ResponseWriter, err error) {
|
||||
func requestLog(log *slog.Logger, next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
started := time.Now()
|
||||
next.ServeHTTP(w, r)
|
||||
log.Info("api request", "method", r.Method, "path", r.URL.Path, "durationMs", time.Since(started).Milliseconds())
|
||||
recorder := &statusRecorder{ResponseWriter: w, status: http.StatusOK}
|
||||
next.ServeHTTP(recorder, r)
|
||||
duration := time.Since(started)
|
||||
if shouldLogRequest(r, recorder.status, duration) {
|
||||
log.Info("api request", "method", r.Method, "path", r.URL.Path, "status", recorder.status, "durationMs", duration.Milliseconds())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type statusRecorder struct {
|
||||
http.ResponseWriter
|
||||
status int
|
||||
}
|
||||
|
||||
func (r *statusRecorder) WriteHeader(status int) {
|
||||
r.status = status
|
||||
r.ResponseWriter.WriteHeader(status)
|
||||
}
|
||||
|
||||
func shouldLogRequest(r *http.Request, status int, duration time.Duration) bool {
|
||||
if status >= 400 || duration >= time.Second {
|
||||
return true
|
||||
}
|
||||
switch r.URL.Path {
|
||||
case "/v1/runs", "/v1/health", "/v1/logs":
|
||||
return false
|
||||
default:
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ func (s *Service) Config() model.Config { s.mu.RLock(); defer s.mu.RUnlo
|
||||
func (s *Service) Runs() []model.Run { return s.queue.Snapshot() }
|
||||
func (s *Service) Logs() map[string]any {
|
||||
return map[string]any{
|
||||
"daemon": tailFile("/var/log/urbm.log", 512*1024, 300),
|
||||
"daemon": tailFile("/var/log/urbm.log", 1024*1024, 1000),
|
||||
"runs": runsWithLogs(s.queue.Snapshot(), 25),
|
||||
}
|
||||
}
|
||||
@@ -386,6 +386,7 @@ func (s *Service) RepositoryStats(ctx context.Context) []model.RepositoryStats {
|
||||
|
||||
func (s *Service) SnapshotFiles(ctx context.Context, repoID, snapshot, path string) ([]map[string]any, error) {
|
||||
started := time.Now()
|
||||
s.log.Info("snapshot file listing started", "repoID", repoID, "snapshot", snapshot, "path", path)
|
||||
repo, ok := s.repository(repoID)
|
||||
if !ok {
|
||||
return nil, errors.New("validation: unknown repository")
|
||||
|
||||
Reference in New Issue
Block a user