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

Avoid vec allocation during each export for BatchLogProcessor - Part 2 #2488

Merged
merged 6 commits into from
Dec 31, 2024
Merged
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
67 changes: 60 additions & 7 deletions opentelemetry-sdk/src/export/logs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@ use std::fmt::Debug;
///
#[derive(Debug)]
pub struct LogBatch<'a> {
/// The data field contains a slice of tuples, where each tuple consists of a reference to
/// a `LogRecord` and a reference to an `InstrumentationScope`.
data: &'a [(&'a LogRecord, &'a InstrumentationScope)],
data: LogBatchData<'a>,
}

/// The `LogBatchData` enum represents the data field of a `LogBatch`.
/// It can either be:
/// - A shared reference to a vector of tuples, where each tuple consists of a `LogRecord` and an `InstrumentationScope`.
/// - Or it can be a slice of tuples, where each tuple consists of a reference to a `LogRecord` and a reference to an `InstrumentationScope`.
#[derive(Debug)]
#[allow(clippy::vec_box)] // Clippy complains about using Box in a Vec, but it's done here for performant moves of the data between channel and the vec.
utpilla marked this conversation as resolved.
Show resolved Hide resolved
enum LogBatchData<'a> {
BorrowedVec(&'a Vec<Box<(LogRecord, InstrumentationScope)>>), // Used by BatchProcessor which clones the LogRecords for its own use.
BorrowedSlice(&'a [(&'a LogRecord, &'a InstrumentationScope)]),
}

impl<'a> LogBatch<'a> {
Expand All @@ -39,7 +48,18 @@ impl<'a> LogBatch<'a> {
/// Note - this is not a public function, and should not be used directly. This would be
/// made private in the future.
pub fn new(data: &'a [(&'a LogRecord, &'a InstrumentationScope)]) -> LogBatch<'a> {
LogBatch { data }
LogBatch {
data: LogBatchData::BorrowedSlice(data),
}
}

#[allow(clippy::vec_box)] // Clippy complains about using Box in a Vec, but it's done here for performant moves of the data between channel and the vec.
pub(crate) fn new_with_owned_data(
data: &'a Vec<Box<(LogRecord, InstrumentationScope)>>,
) -> LogBatch<'a> {
LogBatch {
data: LogBatchData::BorrowedVec(data),
}
}
}

Expand All @@ -54,9 +74,42 @@ impl LogBatch<'_> {
/// An iterator that yields references to the `LogRecord` and `InstrumentationScope` in the batch.
///
pub fn iter(&self) -> impl Iterator<Item = (&LogRecord, &InstrumentationScope)> {
self.data
.iter()
.map(|(record, library)| (*record, *library))
LogBatchDataIter {
data: &self.data,
index: 0,
}
}
}

struct LogBatchDataIter<'a> {
data: &'a LogBatchData<'a>,
index: usize,
}

impl<'a> Iterator for LogBatchDataIter<'a> {
type Item = (&'a LogRecord, &'a InstrumentationScope);

fn next(&mut self) -> Option<Self::Item> {
match self.data {
LogBatchData::BorrowedVec(data) => {
if self.index < data.len() {
let record = &*data[self.index];
self.index += 1;
Some((&record.0, &record.1))
} else {
None
}
}
LogBatchData::BorrowedSlice(data) => {
if self.index < data.len() {
let record = &data[self.index];
self.index += 1;
Some((record.0, record.1))
} else {
None
}
}
}
}
}

Expand Down
7 changes: 1 addition & 6 deletions opentelemetry-sdk/src/logs/log_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,12 +524,7 @@ where
return LogResult::Ok(());
}

let log_vec: Vec<(&LogRecord, &InstrumentationScope)> = batch
.iter()
.map(|log_data| (&log_data.0, &log_data.1))
.collect();

let export = exporter.export(LogBatch::new(log_vec.as_slice()));
let export = exporter.export(LogBatch::new_with_owned_data(batch));
let export_result = futures_executor::block_on(export);

// Clear the batch vec after exporting
Expand Down
Loading