Skip to content

Commit 07b3b40

Browse files
authored
perf: Small perf optimization for logs (#2799)
1 parent 6b221e4 commit 07b3b40

File tree

4 files changed

+9
-11
lines changed

4 files changed

+9
-11
lines changed

opentelemetry-appender-tracing/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ when conversion is feasible. Otherwise stored as
4747
`opentelemetry::logs::AnyValue::String`. This avoids unnecessary string
4848
allocation when values can be represented in their original types.
4949

50+
- perf - small perf improvement by avoiding string allocation of `target`
51+
5052
## 0.28.1
5153

5254
Released 2025-Feb-12

opentelemetry-appender-tracing/benches/logs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
| noop_layer_disabled | 12 ns |
1111
| noop_layer_enabled | 25 ns |
1212
| ot_layer_disabled | 19 ns |
13-
| ot_layer_enabled | 167 ns |
13+
| ot_layer_enabled | 155 ns |
1414
1515
Hardware: Apple M4 Pro
1616
Total Number of Cores: 14 (10 performance and 4 efficiency)
@@ -20,7 +20,7 @@
2020
| noop_layer_disabled | 8 ns |
2121
| noop_layer_enabled | 14 ns |
2222
| ot_layer_disabled | 12 ns |
23-
| ot_layer_enabled | 186 ns |
23+
| ot_layer_enabled | 130 ns |
2424
*/
2525

2626
use criterion::{criterion_group, criterion_main, Criterion};

opentelemetry-appender-tracing/src/layer.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,7 @@ where
189189

190190
let mut log_record = self.logger.create_log_record();
191191

192-
// TODO: Fix heap allocation
193-
log_record.set_target(target.to_string());
192+
log_record.set_target(target);
194193
log_record.set_event_name(name);
195194
log_record.set_severity_number(severity);
196195
log_record.set_severity_text(metadata.level().as_str());

opentelemetry-sdk/src/logs/logger.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,11 @@ impl opentelemetry::logs::Logger for SdkLogger {
3434

3535
//let mut log_record = record;
3636
if record.trace_context.is_none() {
37-
let trace_context = Context::map_current(|cx| {
38-
cx.has_active_span()
39-
.then(|| TraceContext::from(cx.span().span_context()))
37+
Context::map_current(|cx| {
38+
cx.has_active_span().then(|| {
39+
record.trace_context = Some(TraceContext::from(cx.span().span_context()))
40+
})
4041
});
41-
42-
if let Some(ref trace_context) = trace_context {
43-
record.trace_context = Some(trace_context.clone());
44-
}
4542
}
4643
if record.observed_timestamp.is_none() {
4744
record.observed_timestamp = Some(now());

0 commit comments

Comments
 (0)