@@ -80,6 +80,11 @@ impl<LR: LogRecord> tracing::field::Visit for EventVisitor<'_, LR> {
8080 }
8181 }
8282
83+ fn record_bytes ( & mut self , field : & tracing_core:: Field , value : & [ u8 ] ) {
84+ self . log_record
85+ . add_attribute ( Key :: new ( field. name ( ) ) , AnyValue :: from ( value) ) ;
86+ }
87+
8388 fn record_str ( & mut self , field : & tracing_core:: Field , value : & str ) {
8489 #[ cfg( feature = "experimental_metadata_attributes" ) ]
8590 if is_duplicated_metadata ( field. name ( ) ) {
@@ -114,6 +119,22 @@ impl<LR: LogRecord> tracing::field::Visit for EventVisitor<'_, LR> {
114119 . add_attribute ( Key :: new ( field. name ( ) ) , AnyValue :: from ( value) ) ;
115120 }
116121
122+ // TODO: We might need to do similar for record_i128,record_u128 too
123+ // to avoid stringification, unless needed.
124+ fn record_u64 ( & mut self , field : & tracing:: field:: Field , value : u64 ) {
125+ #[ cfg( feature = "experimental_metadata_attributes" ) ]
126+ if is_duplicated_metadata ( field. name ( ) ) {
127+ return ;
128+ }
129+ if let Ok ( signed) = i64:: try_from ( value) {
130+ self . log_record
131+ . add_attribute ( Key :: new ( field. name ( ) ) , AnyValue :: from ( signed) ) ;
132+ } else {
133+ self . log_record
134+ . add_attribute ( Key :: new ( field. name ( ) ) , AnyValue :: from ( format ! ( "{value:?}" ) ) ) ;
135+ }
136+ }
137+
117138 // TODO: Remaining field types from AnyValue : Bytes, ListAny, Boolean
118139}
119140
@@ -173,8 +194,7 @@ where
173194
174195 let mut log_record = self . logger . create_log_record ( ) ;
175196
176- // TODO: Fix heap allocation
177- log_record. set_target ( target. to_string ( ) ) ;
197+ log_record. set_target ( target) ;
178198 log_record. set_event_name ( name) ;
179199 log_record. set_severity_number ( severity) ;
180200 log_record. set_severity_text ( metadata. level ( ) . as_str ( ) ) ;
@@ -331,7 +351,11 @@ mod tests {
331351 let _guard = tracing:: subscriber:: set_default ( subscriber) ;
332352
333353 // Act
334- error ! ( name
: "my-event-name" , target
: "my-system" , event_id =
20 , user_name =
"otel" , user_email =
"[email protected] " ) ; 354+ let small_u64value: u64 = 42 ;
355+ let big_u64value: u64 = u64:: MAX ;
356+ let small_usizevalue: usize = 42 ;
357+ let big_usizevalue: usize = usize:: MAX ;
358+ error ! ( name
: "my-event-name" , target
: "my-system" , event_id =
20 , bytes =
& b"abc" [ ..
] , small_u64value
, big_u64value
, small_usizevalue
, big_usizevalue
, user_name =
"otel" , user_email =
"[email protected] " ) ; 335359 assert ! ( logger_provider. force_flush( ) . is_ok( ) ) ;
336360
337361 // Assert TODO: move to helper methods
@@ -362,9 +386,9 @@ mod tests {
362386
363387 // Validate attributes
364388 #[ cfg( not( feature = "experimental_metadata_attributes" ) ) ]
365- assert_eq ! ( log. record. attributes_iter( ) . count( ) , 3 ) ;
389+ assert_eq ! ( log. record. attributes_iter( ) . count( ) , 8 ) ;
366390 #[ cfg( feature = "experimental_metadata_attributes" ) ]
367- assert_eq ! ( log. record. attributes_iter( ) . count( ) , 7 ) ;
391+ assert_eq ! ( log. record. attributes_iter( ) . count( ) , 12 ) ;
368392 assert ! ( attributes_contains(
369393 & log. record,
370394 & Key :: new( "event_id" ) ,
@@ -380,6 +404,31 @@ mod tests {
380404 & Key :: new( "user_email" ) ,
381405 & AnyValue :: String ( "[email protected] " . into
( ) ) 382406 ) ) ;
407+ assert ! ( attributes_contains(
408+ & log. record,
409+ & Key :: new( "small_u64value" ) ,
410+ & AnyValue :: Int ( 42 . into( ) )
411+ ) ) ;
412+ assert ! ( attributes_contains(
413+ & log. record,
414+ & Key :: new( "big_u64value" ) ,
415+ & AnyValue :: String ( format!( "{}" , u64 :: MAX ) . into( ) )
416+ ) ) ;
417+ assert ! ( attributes_contains(
418+ & log. record,
419+ & Key :: new( "small_usizevalue" ) ,
420+ & AnyValue :: Int ( 42 . into( ) )
421+ ) ) ;
422+ assert ! ( attributes_contains(
423+ & log. record,
424+ & Key :: new( "big_usizevalue" ) ,
425+ & AnyValue :: String ( format!( "{}" , u64 :: MAX ) . into( ) )
426+ ) ) ;
427+ assert ! ( attributes_contains(
428+ & log. record,
429+ & Key :: new( "bytes" ) ,
430+ & AnyValue :: Bytes ( Box :: new( b"abc" . to_vec( ) ) )
431+ ) ) ;
383432 #[ cfg( feature = "experimental_metadata_attributes" ) ]
384433 {
385434 assert ! ( attributes_contains(
@@ -753,6 +802,10 @@ mod tests {
753802 TraceFlags :: SAMPLED
754803 ) ;
755804
805+ for attribute in log. record . attributes_iter ( ) {
806+ println ! ( "key: {:?}, value: {:?}" , attribute. 0 , attribute. 1 ) ;
807+ }
808+
756809 // Attributes can be polluted when we don't use this feature.
757810 #[ cfg( feature = "experimental_metadata_attributes" ) ]
758811 assert_eq ! ( log. record. attributes_iter( ) . count( ) , 4 ) ;
0 commit comments