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

check response size for debug logs #99

Merged
merged 3 commits into from
Oct 18, 2017

Conversation

sudo-suhas
Copy link
Contributor

In my case, for some endpoints, the response was too large(ex: length - 1616599). This made it very difficult to use debug mode, which btw, is really awesome. This PR checks the size of the response and if it exceeds 1000000, ***** RESPONSE TOO LARGE ***** is logged instead.

@codecov
Copy link

codecov bot commented Oct 17, 2017

Codecov Report

Merging #99 into master will decrease coverage by 0.08%.
The diff coverage is 96.42%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #99      +/-   ##
==========================================
- Coverage   97.01%   96.93%   -0.09%     
==========================================
  Files          10       10              
  Lines         973      979       +6     
==========================================
+ Hits          944      949       +5     
- Misses         15       16       +1     
  Partials       14       14
Impacted Files Coverage Δ
default.go 100% <100%> (ø) ⬆️
client.go 97.6% <100%> (+0.02%) ⬆️
middleware.go 92.12% <100%> (ø) ⬆️
response.go 98% <83.33%> (-2%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b081b82...b3a95f2. Read the comment docs.

If response size exceeds 1000000, do not log response.
Copy link
Member

@jeevatkm jeevatkm left a comment

Choose a reason for hiding this comment

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

@sudo-suhas Please have a look on review comments.

response.go Outdated
if r.body != nil {
if len(r.body) > 1000000 {
Copy link
Member

Choose a reason for hiding this comment

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

What is this 1000000 value? are you picking random max value to limit it? and why?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yea.. It is arbitrary. What do you suggest? Should I add a var so that the user can override if required?

Copy link
Member

Choose a reason for hiding this comment

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

Making this value configurable at client level would be best. Since some user wants to get complete body value.

  • Add an un-exported field debugBodySizeLimit int64 to Client and method in the client SetDebugBodyLimit(sl int64)
  • Initializing debugBodySizeLimit default value as math.MaxInt32 (this is good enough to begin with) at client instance creation
  • Add corresponding method for default resty client in default.go

This way it will satisfy possibilities.

response.go Outdated
ct := r.Header().Get(hdrContentTypeKey)
if IsJSONType(ct) {
out := acquireBuffer()
defer releaseBuffer(out)
if err := json.Indent(out, r.body, "", " "); err == nil {
bodyStr = string(out.Bytes())
err := json.Indent(out, r.body, "", " ")
Copy link
Member

Choose a reason for hiding this comment

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

This could be better and concise.

if err := json.Indent(out, r.body, "", "   "); err == nil {
    return out.String()
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That results in a warning from golint - if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) (golint).

Copy link
Member

Choose a reason for hiding this comment

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

Actually remove this line return "unexpected error: " + err.Error() with my above code snippet then golint warning goaway. Because if any errors in formatting debug log, return the original body value which is r.String().

@jeevatkm
Copy link
Member

Also test coverage is decreasing, spend some time on it. Thank you.

@sudo-suhas
Copy link
Contributor Author

Could you tell me where I would need to add tests? I did not see response_test.go nor are there any tests for fmtBodyString.

@jeevatkm
Copy link
Member

To improve test you need create a scenario for response body limit. With configurable value you can decrease the value to create a scenario.

You can add the test in client_test.go or resty_test.go. Just focus on your changes/implementation and compare with master branch code coverage number.

Use following commands to know coverage lines info:

go test -coverprofile=coverage.out -race
go tool cover -html=coverage.out

- client.go
  - Add private field `Client.debugBodySizeLimit` for setting response size
    limit used for debug logs.
  - Add method `Client.SetDebugBodyLimit` for setting
    `Client.debugBodySizeLimit`.
- default.go
  - Set `debugBodySizeLimit` to `math.MaxInt32` while creating new resty
    client.
  - Add mothod `SetDebugBodyLimit` for setting the limit for `DefaultClient`.
- response.go>fmtBodyString
  - Add method parameter for debug response size limit
  - Include response body size in debug logs when it exceeds limit.
  - Fallback to `r.String()` if `json.Indent` errors.
- middleware.go - Pass `debugBodySizeLimit` to `fmtBodyString`.
- client_test.go
  - Add test for `SetDebugBodyLimit`.
  - Add tests for debug body size limit.
- resty_test.go>createGetServer
  - Refactor `if-else-if-else` chain to use switch(recommended by Effective Go).
  - Add cases `/json`, `/json-invalid`, `/long-text`, `/long-json`
@jeevatkm jeevatkm added this to the v1.1 Milestone milestone Oct 18, 2017
@jeevatkm
Copy link
Member

jeevatkm commented Oct 18, 2017

@sudo-suhas Thanks for your contribution and improving tests.

@jeevatkm jeevatkm merged commit 8b5e3f9 into go-resty:master Oct 18, 2017
@sudo-suhas sudo-suhas deleted the debug-log-res-size branch October 19, 2017 03:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

2 participants