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

🐛 bug: Use Content-Length for bytesReceived and bytesSent tags in Logger Middleware #3066

Merged
merged 4 commits into from
Jul 23, 2024

Conversation

gaby
Copy link
Member

@gaby gaby commented Jul 9, 2024

Description

  • Use Content-Length header when calculating the bytesSent and bytesReceived tags in the Logger Middleware.
    • The current implementation will load big files to memory to calculate the len causing Fiber to use a high amount of RAM.
  • Added new test case: Test_Request_Body for testing the bytesReceived tag.
  • Added test case using SetContentLength

Fixes #3060

Type of change

  • Performance improvement (non-breaking change which improves efficiency)
  • Code consistency (non-breaking change which improves code reliability and robustness)

@gaby gaby requested a review from a team as a code owner July 9, 2024 03:11
@gaby gaby requested review from sixcolors, ReneWerner87 and efectn and removed request for a team July 9, 2024 03:11
Copy link
Contributor

coderabbitai bot commented Jul 9, 2024

Walkthrough

The changes primarily focus on resolving an Out-Of-Memory (OOM) issue in the Go-Fiber logger middleware. The updates include enhancements to the test suite in middleware/logger/logger_test.go for better validation of logging behavior, while middleware/logger/tags.go now accurately manages byte logging during large file transfers, ensuring more efficient memory usage.

Changes

Files Change Summary
middleware/logger/logger_test.go Enhanced test coverage by adding and modifying assertions in the logger test functions.
middleware/logger/tags.go Updated byte logging logic to use ContentLength headers for better performance and accuracy.
middleware/logger/default_logger.go Replaced fasthttp.AppendUint with strconv.AppendInt for integer appending to streamline dependencies.

Assessment against linked issues

Objective Addressed Explanation
Fix OOM issue when downloading large files (#3060)
Ensure memory is reclaimed after file download (#3060)

Poem

In the code where bytes do play,
No more the logs shall stray,
From download deep and streaming wide,
The memory now shall bide.
Bugs we’ve fixed, tests anew,
The logger shines, oh how it grew! 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

codecov bot commented Jul 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.12%. Comparing base (8e1470f) to head (fde554a).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3066      +/-   ##
==========================================
- Coverage   83.12%   83.12%   -0.01%     
==========================================
  Files         115      115              
  Lines        8332     8331       -1     
==========================================
- Hits         6926     6925       -1     
  Misses       1076     1076              
  Partials      330      330              
Flag Coverage Δ
unittests 83.12% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c579a1a and 292a7d1.

Files selected for processing (2)
  • middleware/logger/logger_test.go (3 hunks)
  • middleware/logger/tags.go (2 hunks)
Additional comments not posted (6)
middleware/logger/tags.go (2)

91-91: Approved: Correctly uses strconv.Itoa for conversion

The TagBytesReceived function correctly retrieves the content length from the request header and uses strconv.Itoa for conversion.


94-94: Approved: Correctly uses strconv.Itoa for conversion

The TagBytesSent function correctly retrieves the content length from the response header and uses strconv.Itoa for conversion.

middleware/logger/logger_test.go (4)

413-416: Approved: Correctly tests response body logging

The Test_Response_Body function correctly tests both GET and POST requests and verifies the response body logging.


418-442: Approved: Correctly tests bytes received and sent logging

The Test_Request_Body function correctly sets up a POST request with a body and verifies the logging of bytes received and sent.


461-475: Approved: Correctly tests bytes received and sent logging with different response content lengths

The Test_Logger_AppendUint function correctly sets up GET requests and verifies the logging of bytes received and sent with different response content lengths.


658-660: Approved: Correctly tests bytes sent logging with streaming responses

The Test_Logger_ByteSent_Streaming function correctly sets up a GET request with a streaming response and verifies the logging of bytes sent.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 292a7d1 and fde554a.

Files selected for processing (2)
  • middleware/logger/default_logger.go (2 hunks)
  • middleware/logger/tags.go (1 hunks)
Additional comments not posted (3)
middleware/logger/default_logger.go (1)

153-154: LGTM! Verify the function usage in the codebase.

The change to use strconv.AppendInt instead of fasthttp.AppendUint is approved.

However, ensure that all function calls to appendInt are consistent with the new implementation.

Verification successful

Verify the context around the appendInt calls in middleware/logger/tags.go to ensure they are consistent with the new implementation.

The appendInt function is used in three places in middleware/logger/tags.go. We need to confirm that these usages are consistent with the new implementation using strconv.AppendInt.


The function calls to appendInt are consistent with the new implementation.

The usage of appendInt in middleware/logger/tags.go is correct and consistent with the new implementation using strconv.AppendInt.

  • TagBytesReceived: appendInt(output, c.Request().Header.ContentLength())
  • TagBytesSent: appendInt(output, c.Response().Header.ContentLength())
  • TagStatus: appendInt(output, c.Response().StatusCode())
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `appendInt` are consistent with the new implementation.

# Test: Search for the function usage. Expect: Only occurances of the new implementation.
rg --type go -A 5 $'appendInt'

Length of output: 1562


Script:

#!/bin/bash
# Description: Verify the context around the `appendInt` calls in `middleware/logger/tags.go`.

# Test: Search for the function usage and include additional context lines.
rg --type go -A 10 -B 10 $'appendInt' middleware/logger/tags.go

Length of output: 2067

middleware/logger/tags.go (2)

93-93: LGTM! Verify the function usage in the codebase.

The change to use the ContentLength header for calculating the number of bytes sent is approved.

However, ensure that all function calls to TagBytesSent are consistent with the new implementation.


90-90: LGTM! Verify the function usage in the codebase.

The change to use the ContentLength header for calculating the number of bytes received is approved.

However, ensure that all function calls to TagBytesReceived are consistent with the new implementation.

Verification successful

No issues found with the updated implementation of TagBytesReceived.

The function TagBytesReceived is not used elsewhere in the codebase, so there are no inconsistencies to verify with the new implementation.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `TagBytesReceived` are consistent with the new implementation.

# Test: Search for the function usage. Expect: Only occurances of the new implementation.
rg --type go -A 5 $'TagBytesReceived'

Length of output: 873


Script:

#!/bin/bash
# Description: Verify all function calls to `TagBytesReceived` are consistent with the new implementation.

# Test: Search for the function usage across the entire codebase.
rg 'TagBytesReceived' --type go

Length of output: 210

@ReneWerner87 ReneWerner87 added this to the v3 milestone Jul 23, 2024
@ReneWerner87 ReneWerner87 merged commit a57b3c0 into main Jul 23, 2024
15 of 16 checks passed
gaby added a commit that referenced this pull request Jul 25, 2024
…ger Middleware (#3066)

* logger: Use Content-Length header for BytesReceived and BytesSent tags

* Use strconv.AppendInt instead of fasthttp.AppendUint
@efectn efectn deleted the fix-3060 branch September 1, 2024 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

🐛 [Bug]: Logger module causes OOM with ${bytesSent} and ${bytesReceived}
2 participants