Repository navigation
Fix LoggingHandler reporting 0 response size. - #207
Closed
nrvalliere wants to merge 1 commit into
Closed
nrvalliere wants to merge 1 commit into
nrvalliere wants to merge 1 commit into
Conversation
|
This issue has been automatically marked as stale because it hasn't seen a recent update. It'll be automatically closed in a few days. |
jgehrcke
added a commit
to jgehrcke/distribution
that referenced
this pull request
Oct 6, 2026
Gorilla's CombinedLoggingHandler counts response bytes only in Write. When net/http copies a response through io.ReaderFrom, which it does to use sendfile, the access log reports zero bytes. gorilla/handlers#207 fixes this by adding a ReadFrom hook that counts the copied bytes and still delegates to the wrapped writer. The fix is sound, but the PR was closed by a stale bot in 2021 without review, and v1.5.2, the latest release, does not include it. Patching vendor/ is not an option: CI regenerates vendor/ from go.mod and fails on any difference, and modules that import Distribution do not use its vendor/ directory. Without changing Gorilla, there is no way to keep its formatter and correct the count. CustomLoggingHandler accepts a custom formatter, but the combined-format formatter is unexported and cannot be reused. A fix outside Gorilla therefore has to format the log line itself. Replace the handler with a local one built on httpsnoop.CaptureMetrics, which counts both Write and ReadFrom and still delegates ReadFrom to the wrapped writer. httpsnoop is already vendored; it becomes a direct dependency and the vendor tree is unchanged. The log line uses the same format as Gorilla's. Test that log lines match CombinedLoggingHandler on the Write path, and that bytes copied through ReadFrom are logged while ReadFrom still reaches the underlying writer. Signed-off-by: Dr. Jan-Philip Gehrcke <[email protected]> Assisted-by: Claude <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
If you convert your ResponseWriter to an io.ReaderFrom and call ReadFrom to send the response then LoggingHandler reports the size as 0. The cause is a missing ReadFrom hook in responseLogger, similar to the issue fixed in #197.
Summary of Changes