Skip to content

Commit

Permalink
optimize logBody to not buffer and pass x-flow-id to the log, such th…
Browse files Browse the repository at this point in the history
…at it can be searched in log analysis

Signed-off-by: Sandor Szücs <[email protected]>
  • Loading branch information
szuecs committed Nov 20, 2023
1 parent 6adac28 commit 6d94e05
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 16 deletions.
34 changes: 18 additions & 16 deletions filters/diag/logbody.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package diag

import (
"context"
"fmt"
"strings"

"github.com/zalando/skipper/filters"
"github.com/zalando/skipper/filters/flowid"
"github.com/zalando/skipper/net"
)

Expand Down Expand Up @@ -59,13 +61,13 @@ func (lb logBody) Request(ctx filters.FilterContext) {

req := ctx.Request()
if req.Body != nil {
req.Body = net.WrapBody(
req.Context(),
func(p []byte) (int, error) {
ctx.Logger().Infof(`logBody("request"): %q`, p)
return len(p), nil
},
req.Body)
req.Body = net.LogBody(
context.Background(),
fmt.Sprintf(`logBody("request") %s: `, req.Header.Get(flowid.HeaderName)),
ctx.Logger().Infof,
req.Body,
)

}
}

Expand All @@ -78,14 +80,14 @@ func (lb logBody) Response(ctx filters.FilterContext) {
if rsp.Body != nil {
// if this is not set we get from curl
// Error while processing content unencoding: invalid stored block lengths
rsp.Header.Del("Content-Length")
rsp.ContentLength = -1

rsp.Body = net.WrapBody(
context.Background(), // not sure if it makes sense to be cancellable here
func(p []byte) (int, error) {
ctx.Logger().Infof(`logBody("response"): %q`, p)
return len(p), nil
}, rsp.Body)
// rsp.Header.Del("Content-Length")
// rsp.ContentLength = -1

rsp.Body = net.LogBody(
context.Background(),
fmt.Sprintf(`logBody("response") %s: `, ctx.Request().Header.Get(flowid.HeaderName)),
ctx.Logger().Infof,
rsp.Body,
)
}
}
32 changes: 32 additions & 0 deletions net/httpbody.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,38 @@ const (
MaxBufferAbort
)

type logBody struct {
ctx context.Context
fmtstr string
log func(format string, args ...interface{})
input io.ReadCloser
}

func newLogBody(ctx context.Context, fmtstr string, log func(format string, args ...interface{}), rc io.ReadCloser) io.ReadCloser {
return &logBody{
ctx: ctx,
fmtstr: fmtstr,
input: rc,
log: log,
}
}

func (lb *logBody) Read(p []byte) (int, error) {
n, err := lb.input.Read(p)
if n > 0 {
lb.log("%s%s", lb.fmtstr, p)
}
return n, err
}

func (lb *logBody) Close() error {
return lb.input.Close()
}

func LogBody(ctx context.Context, fmtstr string, log func(format string, args ...interface{}), rc io.ReadCloser) io.ReadCloser {
return newLogBody(ctx, fmtstr, log, rc)
}

type matcher struct {
ctx context.Context
once sync.Once
Expand Down

0 comments on commit 6d94e05

Please sign in to comment.