@@ -3,7 +3,6 @@ use opentelemetry::{
3
3
Key ,
4
4
} ;
5
5
use std:: borrow:: Cow ;
6
- use std:: fmt:: Write ;
7
6
use tracing_core:: { Level , Metadata } ;
8
7
#[ cfg( feature = "experimental_metadata_attributes" ) ]
9
8
use tracing_log:: NormalizeEvent ;
@@ -40,12 +39,9 @@ impl<'a, LR: LogRecord> EventVisitor<'a, LR> {
40
39
fn new ( log_record : & ' a mut LR ) -> Self {
41
40
EventVisitor { log_record }
42
41
}
43
- fn visit_metadata ( & mut self , meta : & Metadata ) {
44
- self . log_record
45
- . add_attribute ( Key :: new ( "name" ) , AnyValue :: from ( meta. name ( ) ) ) ;
46
-
42
+ fn visit_metadata ( & mut self , _meta : & Metadata ) {
47
43
#[ cfg( feature = "experimental_metadata_attributes" ) ]
48
- self . visit_experimental_metadata ( meta ) ;
44
+ self . visit_experimental_metadata ( _meta ) ;
49
45
}
50
46
51
47
#[ cfg( feature = "experimental_metadata_attributes" ) ]
@@ -87,16 +83,10 @@ impl<'a, LR: LogRecord> tracing::field::Visit for EventVisitor<'a, LR> {
87
83
return ;
88
84
}
89
85
if field. name ( ) == "message" {
90
- let mut body = String :: new ( ) ;
91
- write ! ( & mut body, "{:?}" , value) . unwrap ( ) ;
92
- self . log_record . set_body ( body. into ( ) ) ;
86
+ self . log_record . set_body ( format ! ( "{:?}" , value) . into ( ) ) ;
93
87
} else {
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
- ) ;
88
+ self . log_record
89
+ . add_attribute ( Key :: new ( field. name ( ) ) , AnyValue :: from ( format ! ( "{value:?}" ) ) ) ;
100
90
}
101
91
}
102
92
@@ -105,10 +95,8 @@ impl<'a, LR: LogRecord> tracing::field::Visit for EventVisitor<'a, LR> {
105
95
if is_duplicated_metadata ( field. name ( ) ) {
106
96
return ;
107
97
}
108
- // Create a String with the exact capacity required
109
- let owned_string = String :: from ( value) ;
110
98
self . log_record
111
- . add_attribute ( Key :: new ( field. name ( ) ) , AnyValue :: from ( owned_string ) ) ;
99
+ . add_attribute ( Key :: new ( field. name ( ) ) , AnyValue :: from ( value . to_owned ( ) ) ) ;
112
100
}
113
101
114
102
fn record_bool ( & mut self , field : & tracing_core:: Field , value : bool ) {
@@ -177,11 +165,11 @@ where
177
165
#[ cfg( not( feature = "experimental_metadata_attributes" ) ) ]
178
166
let meta = event. metadata ( ) ;
179
167
180
- //let mut log_record: LogRecord = LogRecord::default();
181
168
let mut log_record = self . logger . create_log_record ( ) ;
182
169
log_record. set_severity_number ( severity_of_level ( meta. level ( ) ) ) ;
183
170
log_record. set_severity_text ( meta. level ( ) . to_string ( ) . into ( ) ) ;
184
171
log_record. set_target ( meta. target ( ) . to_string ( ) ) ;
172
+ log_record. set_event_name ( meta. name ( ) ) ;
185
173
186
174
let mut visitor = EventVisitor :: new ( & mut log_record) ;
187
175
visitor. visit_metadata ( meta) ;
@@ -224,9 +212,12 @@ mod tests {
224
212
use opentelemetry_sdk:: testing:: logs:: InMemoryLogsExporter ;
225
213
use opentelemetry_sdk:: trace;
226
214
use opentelemetry_sdk:: trace:: { Sampler , TracerProvider } ;
215
+ use smallvec:: SmallVec ;
227
216
use tracing:: error;
228
217
use tracing_subscriber:: layer:: SubscriberExt ;
229
218
219
+ const PREALLOCATED_ATTRIBUTE_CAPACITY : usize = 8 ;
220
+
230
221
// cargo test --features=testing
231
222
#[ test]
232
223
fn tracing_appender_standalone ( ) {
@@ -264,7 +255,7 @@ mod tests {
264
255
assert ! ( log. record. trace_context. is_none( ) ) ;
265
256
266
257
// Validate attributes
267
- let attributes: Vec < ( Key , AnyValue ) > = log
258
+ let attributes: SmallVec < [ ( Key , AnyValue ) ; PREALLOCATED_ATTRIBUTE_CAPACITY ] > = log
268
259
. record
269
260
. attributes
270
261
. clone ( )
@@ -361,7 +352,7 @@ mod tests {
361
352
) ;
362
353
363
354
// validate attributes.
364
- let attributes: Vec < ( Key , AnyValue ) > = log
355
+ let attributes: SmallVec < [ ( Key , AnyValue ) ; PREALLOCATED_ATTRIBUTE_CAPACITY ] > = log
365
356
. record
366
357
. attributes
367
358
. clone ( )
@@ -428,7 +419,7 @@ mod tests {
428
419
assert ! ( log. record. trace_context. is_none( ) ) ;
429
420
430
421
// Validate attributes
431
- let attributes: Vec < ( Key , AnyValue ) > = log
422
+ let attributes: SmallVec < [ ( Key , AnyValue ) ; PREALLOCATED_ATTRIBUTE_CAPACITY ] > = log
432
423
. record
433
424
. attributes
434
425
. clone ( )
@@ -525,7 +516,7 @@ mod tests {
525
516
) ;
526
517
527
518
// validate attributes.
528
- let attributes: Vec < ( Key , AnyValue ) > = log
519
+ let attributes: SmallVec < [ ( Key , AnyValue ) ; PREALLOCATED_ATTRIBUTE_CAPACITY ] > = log
529
520
. record
530
521
. attributes
531
522
. clone ( )
0 commit comments