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

aws/client: Fix logging to allow it to be enabled per operation #3778

Merged
merged 3 commits into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
### SDK Enhancements

### SDK Bugs
* `aws/client`: Fix logging to allow it to be enabled per operation
* Allow logging of operation and request to be enabled per operation, not only per client or session.
4 changes: 0 additions & 4 deletions aws/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@ func (c *Client) NewRequest(operation *request.Operation, params interface{}, da
// AddDebugHandlers injects debug logging handlers into the service to log request
// debug information.
func (c *Client) AddDebugHandlers() {
if !c.Config.LogLevel.AtLeast(aws.LogDebug) {
return
}

c.Handlers.Send.PushFrontNamed(LogHTTPRequestHandler)
c.Handlers.Send.PushBackNamed(LogHTTPResponseHandler)
}
8 changes: 5 additions & 3 deletions aws/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ func TestNewClient_CopyHandlers(t *testing.T) {
if e, a := 2, handlers.Send.Len(); e != a {
t.Errorf("expect %d original handlers, got %d", e, a)
}
if e, a := 3, c.Handlers.Send.Len(); e != a {
if e, a := 5, c.Handlers.Send.Len(); e != a {
t.Errorf("expect %d client handlers, got %d", e, a)
}

handlers.Send.Run(nil)
req := c.NewRequest(&request.Operation{}, struct{}{}, struct{}{})

handlers.Send.Run(req)
if !*firstCalled {
t.Errorf("expect first handler to of been called")
}
Expand All @@ -64,7 +66,7 @@ func TestNewClient_CopyHandlers(t *testing.T) {
t.Errorf("expect client handler to not of been called, but was")
}

c.Handlers.Send.Run(nil)
c.Handlers.Send.Run(req)
if !*firstCalled {
t.Errorf("expect client's first handler to of been called")
}
Expand Down
8 changes: 8 additions & 0 deletions aws/client/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ var LogHTTPRequestHandler = request.NamedHandler{
}

func logRequest(r *request.Request) {
if !r.Config.LogLevel.AtLeast(aws.LogDebug) {
return
}

logBody := r.Config.LogLevel.Matches(aws.LogDebugWithHTTPBody)
bodySeekable := aws.IsReaderSeekable(r.Body)

Expand Down Expand Up @@ -120,6 +124,10 @@ var LogHTTPResponseHandler = request.NamedHandler{
}

func logResponse(r *request.Request) {
if !r.Config.LogLevel.AtLeast(aws.LogDebug) {
return
}

lw := &logWriter{r.Config.Logger, bytes.NewBuffer(nil)}

if r.HTTPResponse == nil {
Expand Down
7 changes: 6 additions & 1 deletion aws/client/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func TestLogResponse(t *testing.T) {
ExpectBody []byte
ReadBody bool
LogLevel aws.LogLevelType
ExpectLog bool
}{
{
Body: bytes.NewBuffer([]byte("body content")),
Expand All @@ -142,11 +143,13 @@ func TestLogResponse(t *testing.T) {
{
Body: bytes.NewBuffer([]byte("body content")),
LogLevel: aws.LogDebug,
ExpectLog: true,
ExpectBody: []byte("body content"),
},
{
Body: bytes.NewBuffer([]byte("body content")),
LogLevel: aws.LogDebugWithHTTPBody,
ExpectLog: true,
ReadBody: true,
ExpectBody: []byte("body content"),
},
Expand Down Expand Up @@ -190,8 +193,10 @@ func TestLogResponse(t *testing.T) {
}
}

if logW.Len() == 0 {
if c.ExpectLog && logW.Len() == 0 {
t.Errorf("%d, expect HTTP Response headers to be logged", i)
} else if !c.ExpectLog && logW.Len() != 0 {
t.Errorf("%d, expect no log, got,\n%v", i, logW.String())
}

b, err := ioutil.ReadAll(req.HTTPResponse.Body)
Expand Down