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

Documenting memory usage/management by BatchLogProcessor #2469

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

lalitb
Copy link
Member

@lalitb lalitb commented Dec 24, 2024

Changes

Felt it would be good to document it for better understanding of the memory management by BatchLogProcessor. This is not the doc-comment so won't go the docs.rs. If there is better place to add this, please suggest.

Merge requirement checklist

  • CONTRIBUTING guidelines followed
  • Unit tests added/updated (if applicable)
  • Appropriate CHANGELOG.md files updated for non-trivial, user-facing changes
  • Changes in public API reviewed (if applicable)

@lalitb lalitb requested a review from a team as a code owner December 24, 2024 14:20
Copy link

codecov bot commented Dec 24, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 77.1%. Comparing base (8d5f222) to head (76973f4).
Report is 1 commits behind head on main.

Additional details and impacted files
@@          Coverage Diff          @@
##            main   #2469   +/-   ##
=====================================
  Coverage   77.1%   77.1%           
=====================================
  Files        124     124           
  Lines      23021   23021           
=====================================
  Hits       17771   17771           
  Misses      5250    5250           

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

@@ -167,6 +167,72 @@ enum BatchMessage {

/// A [`LogProcessor`] that buffers log records and reports
/// them at a pre-configured interval from a dedicated background thread.
// **Memory Management in BatchLogProcessor**
Copy link
Member

Choose a reason for hiding this comment

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

a lot of these can be moved off to docs.rs itself, as they are good for users to be aware of.

Copy link
Member

@cijothomas cijothomas left a comment

Choose a reason for hiding this comment

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

The contents are very good. Lets plan to move them to user-facing docs before stable release.

// 1. **Record Ingestion**:
// - Each `LogRecord` is **cloned** upon entering the processor.
// - `LogRecordAttributes` utilize a hybrid memory model:
// - Attributes up to `PREALLOCATED_ATTRIBUTE_CAPACITY` are **stack-allocated**.
Copy link
Contributor

Choose a reason for hiding this comment

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

Since PREALLOCATED_ATTRIBUTE_CAPACITY is not a public const, lets write the exact number of attributes here. I would suggest something like this:

- The first 5 attributes are allocated on the stack.
- Any additional attributes are allocated on the heap in a dynamically growing vector.

// 4. **Export Process**:
// - A temporary `Vec` is created during the export process to hold ownership of the boxed records:
// - The contents of the worker thread's buffer (`Vec<Box<(LogRecord, InstrumentationScope)>>`)
// are moved into this temporary vector using `logs.split_off(0)`.
Copy link
Contributor

Choose a reason for hiding this comment

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

@lalitb Thanks for writing up the documentation. It's very insightful.

I was wondering if we could avoid the allocation caused by split_off(). We should be able to reuse the existing logs vec when calling export_with_timeout_sync method. I created #2483 as a proof of concept.

Copy link
Member Author

Choose a reason for hiding this comment

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

@utpilla, yes that's a good point - the original buffer is not getting used/updated during export process, so it's good idea to reuse it for export, and then clear it up.

//
/// 5. **Memory Limits**:
/// - **Worst-Case Memory Usage**:
/// - **Queue Memory** = `max_queue_size * size of boxed (LogRecord + InstrumentationScope)`.
Copy link
Contributor

Choose a reason for hiding this comment

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

It'd be nice to mention the actual size of LogRecord and InstrumentationScope in bytes to get a better idea of the memory consumption.

// are moved into this temporary vector using `logs.split_off(0)`.
// - Ownership of the boxed records is transferred to the new vector, ensuring efficient
// memory usage without additional cloning.
// - The temporary vector is then used to construct references passed to the exporter via `LogBatch`.
Copy link
Contributor

Choose a reason for hiding this comment

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

It'd be good to mention that there two temporary vectors created during each export:

  1. One vector is created by split_off(0) (we could avoid this if we pursue Avoid vec allocation during each export for BatchLogProcessor #2483)
  2. The second vector is created by collecting references from the first vector mentioned above.

I think we should be able to avoid the second vector creation as well by updating LogBatch constructors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants