Skip to content

Commit 2b51756

Browse files
committed
boxing and capacity allocation
1 parent ab81e0f commit 2b51756

File tree

1 file changed

+13
-4
lines changed
  • opentelemetry-appender-tracing/src

1 file changed

+13
-4
lines changed

opentelemetry-appender-tracing/src/layer.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use opentelemetry::{
33
Key,
44
};
55
use std::borrow::Cow;
6+
use std::fmt::Write;
67
use tracing_core::{Level, Metadata};
78
#[cfg(feature = "experimental_metadata_attributes")]
89
use tracing_log::NormalizeEvent;
@@ -86,10 +87,16 @@ impl<'a, LR: LogRecord> tracing::field::Visit for EventVisitor<'a, LR> {
8687
return;
8788
}
8889
if field.name() == "message" {
89-
self.log_record.set_body(format!("{:?}", value).into());
90+
let mut body = String::new();
91+
write!(&mut body, "{:?}", value).unwrap();
92+
self.log_record.set_body(body.into());
9093
} else {
91-
self.log_record
92-
.add_attribute(Key::new(field.name()), AnyValue::from(format!("{value:?}")));
94+
let mut value_string = String::new();
95+
write!(&mut value_string, "{:?}", value).unwrap();
96+
self.log_record.add_attribute(
97+
Key::new(field.name()),
98+
AnyValue::from(format!("{value_string:?}")),
99+
);
93100
}
94101
}
95102

@@ -98,8 +105,10 @@ impl<'a, LR: LogRecord> tracing::field::Visit for EventVisitor<'a, LR> {
98105
if is_duplicated_metadata(field.name()) {
99106
return;
100107
}
108+
// Create a String with the exact capacity required
109+
let owned_string = String::from(value);
101110
self.log_record
102-
.add_attribute(Key::new(field.name()), AnyValue::from(value.to_owned()));
111+
.add_attribute(Key::new(field.name()), AnyValue::from(owned_string));
103112
}
104113

105114
fn record_bool(&mut self, field: &tracing_core::Field, value: bool) {

0 commit comments

Comments
 (0)