Skip to content

Fix LoggingHandler reporting 0 response size. - #207

Closed
nrvalliere wants to merge 1 commit into
gorilla:masterfrom
nrvalliere:logging-handler-read-from
Closed

nrvalliere wants to merge 1 commit into
gorilla:masterfrom
nrvalliere:logging-handler-read-from

Conversation

@nrvalliere

Copy link
Copy Markdown

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

  1. Add a ReadFrom hook in responseLogger that increments Size.

@stale

stale Bot commented Jun 4, 2021

Copy link
Copy Markdown

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.

@stale stale Bot added the stale label Jun 4, 2021
@stale stale Bot closed this Jun 16, 2021
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant