Skip to content

Commit

Permalink
avoid log data cloning
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitb committed Apr 2, 2024
1 parent de7dc51 commit 0634469
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
35 changes: 26 additions & 9 deletions opentelemetry-sdk/src/logs/log_emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,16 +209,33 @@ impl opentelemetry::logs::Logger for Logger {
cx.has_active_span()
.then(|| TraceContext::from(cx.span().span_context()))
});
for p in processors {
let mut record = record.clone();
if let Some(ref trace_context) = trace_context {
record.trace_context = Some(trace_context.clone())
// Prioritize the scenario where there is likely to be only one processor.
if processors.len() == 1 {
// If there is exactly one processor, use the record directly.
if let Some(p) = processors.first() {
let mut record = record; // Move the record directly, avoiding clone.
if let Some(ref trace_context) = trace_context {
record.trace_context = Some(trace_context.clone());
}
let data = LogData {
record,
instrumentation: self.instrumentation_library().clone(),
};
p.emit(data);
}

Check warning on line 225 in opentelemetry-sdk/src/logs/log_emitter.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/logs/log_emitter.rs#L225

Added line #L225 was not covered by tests
} else {
// For multiple processors, clone the record for each.
for p in processors {
let mut cloned_record = record.clone();
if let Some(ref trace_context) = trace_context {
cloned_record.trace_context = Some(trace_context.clone());
}
let data = LogData {
record: cloned_record,
instrumentation: self.instrumentation_library().clone(),
};
p.emit(data);

Check warning on line 237 in opentelemetry-sdk/src/logs/log_emitter.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/logs/log_emitter.rs#L228-L237

Added lines #L228 - L237 were not covered by tests
}
let data = LogData {
record,
instrumentation: self.instrumentation_library().clone(),
};
p.emit(data);
}
}

Expand Down
4 changes: 2 additions & 2 deletions stress/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ doc = false
ctrlc = "3.2.5"
lazy_static = "1.4.0"
num_cpus = "1.15.0"
opentelemetry = { path = "../opentelemetry", features = ["metrics", "logs", "trace"] }
opentelemetry_sdk = { path = "../opentelemetry-sdk", features = ["metrics", "logs", "trace"] }
opentelemetry = { path = "../opentelemetry", features = ["metrics", "logs", "trace", "logs_level_enabled"] }
opentelemetry_sdk = { path = "../opentelemetry-sdk", features = ["metrics", "logs", "trace", "logs_level_enabled"] }
opentelemetry-appender-tracing = { path = "../opentelemetry-appender-tracing"}
rand = { version = "0.8.4", features = ["small_rng"] }
tracing = { workspace = true, features = ["std"]}
Expand Down

0 comments on commit 0634469

Please sign in to comment.