Skip to content

Commit

Permalink
🪵 http: improve debug log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
database64128 committed Jun 6, 2024
1 parent 06839f4 commit 0bf3ce1
Showing 1 changed file with 36 additions and 15 deletions.
51 changes: 36 additions & 15 deletions http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ func NewHttpStreamServerReadWriter(rw zerocopy.DirectReadWriteCloser, logger *za
return nil, conn.Addr{}, err
}

if ce := logger.Check(zap.DebugLevel, "Received initial HTTP request"); ce != nil {
ce.Write(
zap.String("proto", req.Proto),
zap.String("method", req.Method),
zap.String("url", req.RequestURI),
zap.String("host", req.Host),
zap.Int64("contentLength", req.ContentLength),
zap.Bool("close", req.Close),
)
}

// Host -> targetAddr
targetAddr, err := hostHeaderToAddr(req.Host)
if err != nil {
Expand Down Expand Up @@ -65,14 +76,6 @@ func NewHttpStreamServerReadWriter(rw zerocopy.DirectReadWriteCloser, logger *za

delete(req.Header, "Proxy-Connection")

if ce := logger.Check(zap.DebugLevel, "Writing HTTP request"); ce != nil {
ce.Write(
zap.String("proto", req.Proto),
zap.String("method", req.Method),
zap.String("url", req.RequestURI),
)
}

// Write request.
if werr = req.Write(plbw); werr != nil {
werr = fmt.Errorf("failed to write HTTP request: %w", werr)
Expand All @@ -97,6 +100,17 @@ func NewHttpStreamServerReadWriter(rw zerocopy.DirectReadWriteCloser, logger *za
break
}

if ce := logger.Check(zap.DebugLevel, "Received HTTP response"); ce != nil {
ce.Write(
zap.String("url", req.RequestURI),
zap.String("host", req.Host),
zap.String("proto", resp.Proto),
zap.String("status", resp.Status),
zap.Int64("contentLength", resp.ContentLength),
zap.Bool("close", resp.Close),
)
}

// Add Connection: close if response is 301, 302, or 307,
// and Location points to a different host.
switch resp.StatusCode {
Expand All @@ -105,6 +119,8 @@ func NewHttpStreamServerReadWriter(rw zerocopy.DirectReadWriteCloser, logger *za

if ce := logger.Check(zap.DebugLevel, "Checking HTTP 3xx response Location header"); ce != nil {
ce.Write(
zap.String("url", req.RequestURI),
zap.String("host", req.Host),
zap.String("proto", resp.Proto),
zap.String("status", resp.Status),
zap.Strings("location", location),
Expand All @@ -127,13 +143,6 @@ func NewHttpStreamServerReadWriter(rw zerocopy.DirectReadWriteCloser, logger *za
}
}

if ce := logger.Check(zap.DebugLevel, "Writing HTTP response"); ce != nil {
ce.Write(
zap.String("proto", resp.Proto),
zap.String("status", resp.Status),
)
}

// Write response.
if rerr = resp.Write(rwbw); rerr != nil {
rerr = fmt.Errorf("failed to write HTTP response: %w", rerr)
Expand Down Expand Up @@ -165,6 +174,18 @@ func NewHttpStreamServerReadWriter(rw zerocopy.DirectReadWriteCloser, logger *za
break
}

if ce := logger.Check(zap.DebugLevel, "Received subsequent HTTP request"); ce != nil {
ce.Write(
zap.String("proto", req.Proto),
zap.String("method", req.Method),
zap.String("url", req.RequestURI),
zap.String("host", req.Host),
zap.String("fixedHost", fixedHost),
zap.Int64("contentLength", req.ContentLength),
zap.Bool("close", req.Close),
)
}

// Close the proxy connection if the destination host changes.
// The client should seamlessly open a new connection.
if req.Host != fixedHost {
Expand Down

0 comments on commit 0bf3ce1

Please sign in to comment.