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

Predefine Vector Capacity for LogRecord Attributes #1908

Merged
merged 7 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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: 1 addition & 1 deletion opentelemetry-appender-tracing/benches/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
| noop_layer_disabled | 12 ns |
| noop_layer_enabled | 25 ns |
| ot_layer_disabled | 19 ns |
| ot_layer_enabled | 371 ns |
| ot_layer_enabled | 270 ns |
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 you update stress tests too?

Copy link
Member Author

Choose a reason for hiding this comment

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

updated stress. The benchmark shows improvement from 391 ns -> 305 ns. Updated that too.

*/

use async_trait::async_trait;
Expand Down
20 changes: 19 additions & 1 deletion opentelemetry-sdk/src/logs/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use opentelemetry::{
};
use std::{borrow::Cow, time::SystemTime};

#[derive(Debug, Default, Clone)]
const PREALLOCATED_ATTRIBUTE_CAPACITY: usize = 8;

#[derive(Debug, Clone)]
#[non_exhaustive]
/// LogRecord represents all data carried by a log record, and
/// is provided to `LogExporter`s as input.
Expand Down Expand Up @@ -37,6 +39,22 @@ pub struct LogRecord {
pub attributes: Option<Vec<(Key, AnyValue)>>,
}

impl Default for LogRecord {
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
fn default() -> Self {
LogRecord {
event_name: None,
target: None,
timestamp: None,
observed_timestamp: None,
trace_context: None,
severity_text: None,
severity_number: None,
body: None,
attributes: Some(Vec::with_capacity(PREALLOCATED_ATTRIBUTE_CAPACITY)), // Pre-allocate for perf optimization. This may change in future.
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

impl opentelemetry::logs::LogRecord for LogRecord {
fn set_event_name<T>(&mut self, name: T)
where
Expand Down
Loading