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

Add Feature Flag to Disable Array Preallocation for Log Record Attributes in Limited Stack Environments #2055

Closed
Closed
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions opentelemetry-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ testing = ["opentelemetry/testing", "trace", "metrics", "logs", "rt-async-std",
rt-tokio = ["tokio", "tokio-stream"]
rt-tokio-current-thread = ["tokio", "tokio-stream"]
rt-async-std = ["async-std"]
no-preallocate-attributes-array = []
Copy link
Member

Choose a reason for hiding this comment

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

Alternate suggestion:
disable_stack_log_attributes

"Preallocate" is potentially misleading, we can pre-allocate in heap as well.

Copy link
Member

Choose a reason for hiding this comment

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

please also add a changelog entry for this. There is a general lacking of documentaion for each of the feature flags, but that is something we need to work on independent of this PR.


[[bench]]
name = "context"
Expand Down
14 changes: 14 additions & 0 deletions opentelemetry-sdk/src/logs/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,29 @@ use std::{borrow::Cow, time::SystemTime};

// According to a Go-specific study mentioned on https://go.dev/blog/slog,
// up to 5 attributes is the most common case.
#[cfg(not(feature = "no-preallocate-attributes-array"))]
const PREALLOCATED_ATTRIBUTE_CAPACITY: usize = 5;
#[cfg(feature = "no-preallocate-attributes-array")]
const PREALLOCATED_ATTRIBUTE_CAPACITY: usize = 0;
#[cfg(feature = "no-preallocate-attributes-array")]
const OVERFLOW_ATTRIBUTE_CAPACITY: usize = 5;

/// Represents a collection of log record attributes with a predefined capacity.
///
/// This type uses `GrowableArray` to store key-value pairs of log attributes, where each attribute is an `Option<(Key, AnyValue)>`.
/// The initial attributes are allocated in a fixed-size array of capacity `PREALLOCATED_ATTRIBUTE_CAPACITY`.
/// If more attributes are added beyond this capacity, additional storage is handled by dynamically growing a vector.
#[cfg(not(feature = "no-preallocate-attributes-array"))]
pub(crate) type LogRecordAttributes =
GrowableArray<Option<(Key, AnyValue)>, PREALLOCATED_ATTRIBUTE_CAPACITY>;
/// When the `no-preallocate-attributes-array` feature flag is enabled, we don't use array allocation.
/// Instead, the vector part of `GrowableArray` is used, and OVERFLOW_ATTRIBUTE_CAPACITY allocation occurs when the first entry is added.
#[cfg(feature = "no-preallocate-attributes-array")]
pub(crate) type LogRecordAttributes = GrowableArray<
Option<(Key, AnyValue)>,
PREALLOCATED_ATTRIBUTE_CAPACITY,
OVERFLOW_ATTRIBUTE_CAPACITY,
>;

#[derive(Debug, Default, Clone, PartialEq)]
#[non_exhaustive]
Expand Down
Loading