Skip to content

Commit

Permalink
Log outgoing status code
Browse files Browse the repository at this point in the history
Acconut committed Dec 20, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 1df3a70 commit ff0631a
Showing 2 changed files with 22 additions and 2 deletions.
23 changes: 22 additions & 1 deletion pkg/handler/unrouted_handler.go
Original file line number Diff line number Diff line change
@@ -1069,7 +1069,11 @@ func (handler *UnroutedHandler) GetFile(w http.ResponseWriter, r *http.Request)
w.Header().Set("Content-Type", resp.Header["Content-Type"])
w.Header().Set("Content-Disposition", resp.Header["Content-Disposition"])

err = servableUpload.ServeContent(c, w, r)
// Use loggingResponseWriter to get the ResponseOutgoing log entry that
// normally handler.sendResp would produce.
loggingW := &loggingResponseWriter{ResponseWriter: w, logger: c.log}

err = servableUpload.ServeContent(c, loggingW, r)
if err != nil {
handler.sendError(c, err)
}
@@ -1698,3 +1702,20 @@ func validateUploadId(newId string) error {

return nil
}

// loggingResponseWriter is a wrapper around http.ResponseWriter that logs the
// final status code similar to UnroutedHandler.sendResp.
type loggingResponseWriter struct {
http.ResponseWriter
logger *slog.Logger
}

func (w *loggingResponseWriter) WriteHeader(statusCode int) {
if statusCode >= 200 {
w.logger.Info("ResponseOutgoing", "status", statusCode)
}
w.ResponseWriter.WriteHeader(statusCode)
}

// Unwrap provides access to the underlying http.ResponseWriter.
func (w *loggingResponseWriter) Unwrap() http.ResponseWriter { return w.ResponseWriter }
1 change: 0 additions & 1 deletion pkg/s3store/serve_content.go
Original file line number Diff line number Diff line change
@@ -72,7 +72,6 @@ func (upload *s3Upload) ServeContent(ctx context.Context, w http.ResponseWriter,
}
}

// TODO: Return HTTPErrors, so that tusd can log them.
w.WriteHeader(http.StatusNotModified)
return nil
}

0 comments on commit ff0631a

Please sign in to comment.