Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

caddyhttp: Log request body bytes read #5461

Merged
merged 1 commit into from
Mar 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions modules/caddyhttp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/netip"
Expand Down Expand Up @@ -259,6 +260,14 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
wrec := NewResponseRecorder(w, nil, nil)
w = wrec

// wrap the request body in a LengthReader
// so we can track the number of bytes read from it
var bodyReader *lengthReader
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be a long shot, but does this have to be a pointer? Just wondering if we can avoid an allocation.

Copy link
Member Author

@francislavoie francislavoie Mar 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so because we need to use a pointer receiver for Read

cannot use bodyReader (variable of type lengthReader) as io.ReadCloser value in assignment: lengthReader does not implement io.ReadCloser (method Close has pointer receiver) compiler(InvalidIfaceAssign)

Copy link
Member Author

@francislavoie francislavoie Mar 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well maybe if I make Read and Close not use a pointer receiver, but does that even work?
Edit: No it wouldn't because Read unfortunately needs to write the Length back to the struct

Copy link
Member

@mholt mholt Mar 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah, that was my guess/concern. Makes sense. Oh well!

Copy link
Member Author

@francislavoie francislavoie Aug 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You were kinda right though - we should probably use sync.Pool for lengthReader. Opened #5756

if r.Body != nil {
bodyReader = &lengthReader{Source: r.Body}
r.Body = bodyReader
}

// capture the original version of the request
accLog := s.accessLogger.With(loggableReq)

Expand All @@ -285,7 +294,13 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {

userID, _ := repl.GetString("http.auth.user.id")

reqBodyLength := 0
if bodyReader != nil {
reqBodyLength = bodyReader.Length
}

log("handled request",
zap.Int("bytes_read", reqBodyLength),
zap.String("user_id", userID),
zap.Duration("duration", duration),
zap.Int("size", wrec.Size()),
Expand Down Expand Up @@ -826,6 +841,23 @@ func cloneURL(from, to *url.URL) {
}
}

// lengthReader is an io.ReadCloser that keeps track of the
// number of bytes read from the request body.
type lengthReader struct {
Source io.ReadCloser
Length int
}

func (r *lengthReader) Read(b []byte) (int, error) {
n, err := r.Source.Read(b)
r.Length += n
return n, err
}

func (r *lengthReader) Close() error {
return r.Source.Close()
}

// Context keys for HTTP request context values.
const (
// For referencing the server instance
Expand Down