@@ -3,6 +3,7 @@ use opentelemetry::{
3
3
Key ,
4
4
} ;
5
5
use std:: borrow:: Cow ;
6
+ use std:: fmt:: Write ;
6
7
use tracing_core:: { Level , Metadata } ;
7
8
#[ cfg( feature = "experimental_metadata_attributes" ) ]
8
9
use tracing_log:: NormalizeEvent ;
@@ -86,10 +87,16 @@ impl<'a, LR: LogRecord> tracing::field::Visit for EventVisitor<'a, LR> {
86
87
return ;
87
88
}
88
89
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 ( ) ) ;
90
93
} 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
+ ) ;
93
100
}
94
101
}
95
102
@@ -98,8 +105,10 @@ impl<'a, LR: LogRecord> tracing::field::Visit for EventVisitor<'a, LR> {
98
105
if is_duplicated_metadata ( field. name ( ) ) {
99
106
return ;
100
107
}
108
+ // Create a String with the exact capacity required
109
+ let owned_string = String :: from ( value) ;
101
110
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 ) ) ;
103
112
}
104
113
105
114
fn record_bool ( & mut self , field : & tracing_core:: Field , value : bool ) {
0 commit comments