-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: logBody() filter to log request or response body while strea…
…ming (#2827) doc: add logBody() test: add 100% coverage Signed-off-by: Sandor Szücs <[email protected]>
- Loading branch information
Showing
6 changed files
with
750 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package diag | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/zalando/skipper/filters" | ||
"github.com/zalando/skipper/filters/flowid" | ||
) | ||
|
||
type logBody struct { | ||
limit int | ||
request bool | ||
response bool | ||
} | ||
|
||
// NewLogBody creates a filter specification for the 'logBody()' filter. | ||
func NewLogBody() filters.Spec { return logBody{} } | ||
|
||
// Name returns the logBody filtern name. | ||
func (logBody) Name() string { | ||
return filters.LogBodyName | ||
} | ||
|
||
func (logBody) CreateFilter(args []interface{}) (filters.Filter, error) { | ||
var ( | ||
request = false | ||
response = false | ||
) | ||
|
||
if len(args) != 2 { | ||
return nil, filters.ErrInvalidFilterParameters | ||
} | ||
|
||
opt, ok := args[0].(string) | ||
if !ok { | ||
return nil, filters.ErrInvalidFilterParameters | ||
} | ||
switch opt { | ||
case "response": | ||
response = true | ||
case "request": | ||
request = true | ||
default: | ||
return nil, fmt.Errorf("failed to match %q: %w", opt, filters.ErrInvalidFilterParameters) | ||
} | ||
|
||
limit, ok := args[1].(float64) | ||
if !ok || float64(int(limit)) != limit { | ||
return nil, fmt.Errorf("failed to convert to int: %w", filters.ErrInvalidFilterParameters) | ||
} | ||
|
||
return &logBody{ | ||
limit: int(limit), | ||
request: request, | ||
response: response, | ||
}, nil | ||
} | ||
|
||
func (lb *logBody) Request(ctx filters.FilterContext) { | ||
if !lb.request { | ||
return | ||
} | ||
|
||
req := ctx.Request() | ||
if req.Body != nil { | ||
req.Body = newLogBodyStream( | ||
lb.limit, | ||
func(chunk []byte) { | ||
ctx.Logger().Infof( | ||
`logBody("request") %s: %s`, | ||
req.Header.Get(flowid.HeaderName), | ||
chunk) | ||
}, | ||
req.Body, | ||
) | ||
} | ||
} | ||
|
||
func (lb *logBody) Response(ctx filters.FilterContext) { | ||
if !lb.response { | ||
return | ||
} | ||
|
||
rsp := ctx.Response() | ||
if rsp.Body != nil { | ||
rsp.Body = newLogBodyStream( | ||
lb.limit, | ||
func(chunk []byte) { | ||
ctx.Logger().Infof( | ||
`logBody("response") %s: %s`, | ||
ctx.Request().Header.Get(flowid.HeaderName), | ||
chunk) | ||
}, | ||
rsp.Body, | ||
) | ||
} | ||
} | ||
|
||
type logBodyStream struct { | ||
left int | ||
f func([]byte) | ||
input io.ReadCloser | ||
} | ||
|
||
func newLogBodyStream(left int, f func([]byte), rc io.ReadCloser) io.ReadCloser { | ||
return &logBodyStream{ | ||
left: left, | ||
f: f, | ||
input: rc, | ||
} | ||
} | ||
|
||
func (lb *logBodyStream) Read(p []byte) (n int, err error) { | ||
if lb.left <= 0 { | ||
return lb.input.Read(p) | ||
} | ||
|
||
n, err = lb.input.Read(p) | ||
if n > 0 { | ||
lb.f(p[:min(n, lb.left)]) | ||
} | ||
lb.left -= n | ||
|
||
return n, err | ||
} | ||
|
||
func (lb *logBodyStream) Close() error { | ||
return lb.input.Close() | ||
} |
Oops, something went wrong.