@@ -114,6 +114,22 @@ impl<LR: LogRecord> tracing::field::Visit for EventVisitor<'_, LR> {
114114 . add_attribute ( Key :: new ( field. name ( ) ) , AnyValue :: from ( value) ) ;
115115 }
116116
117+ // TODO: We might need to do similar for record_i128,record_u128 too
118+ // to avoid stringification, unless needed.
119+ fn record_u64 ( & mut self , field : & tracing:: field:: Field , value : u64 ) {
120+ #[ cfg( feature = "experimental_metadata_attributes" ) ]
121+ if is_duplicated_metadata ( field. name ( ) ) {
122+ return ;
123+ }
124+ if let Ok ( signed) = i64:: try_from ( value) {
125+ self . log_record
126+ . add_attribute ( Key :: new ( field. name ( ) ) , AnyValue :: from ( signed) ) ;
127+ } else {
128+ self . log_record
129+ . add_attribute ( Key :: new ( field. name ( ) ) , AnyValue :: from ( format ! ( "{value:?}" ) ) ) ;
130+ }
131+ }
132+
117133 // TODO: Remaining field types from AnyValue : Bytes, ListAny, Boolean
118134}
119135
@@ -173,8 +189,7 @@ where
173189
174190 let mut log_record = self . logger . create_log_record ( ) ;
175191
176- // TODO: Fix heap allocation
177- log_record. set_target ( target. to_string ( ) ) ;
192+ log_record. set_target ( target) ;
178193 log_record. set_event_name ( name) ;
179194 log_record. set_severity_number ( severity) ;
180195 log_record. set_severity_text ( metadata. level ( ) . as_str ( ) ) ;
@@ -331,7 +346,11 @@ mod tests {
331346 let _guard = tracing:: subscriber:: set_default ( subscriber) ;
332347
333348 // Act
334- error ! ( name
: "my-event-name" , target
: "my-system" , event_id =
20 , user_name =
"otel" , user_email =
"[email protected] " ) ; 349+ let small_u64value: u64 = 42 ;
350+ let big_u64value: u64 = u64:: MAX ;
351+ let small_usizevalue: usize = 42 ;
352+ let big_usizevalue: usize = usize:: MAX ;
353+ error ! ( name
: "my-event-name" , target
: "my-system" , event_id =
20 , small_u64value
, big_u64value
, small_usizevalue
, big_usizevalue
, user_name =
"otel" , user_email =
"[email protected] " ) ; 335354 assert ! ( logger_provider. force_flush( ) . is_ok( ) ) ;
336355
337356 // Assert TODO: move to helper methods
@@ -362,9 +381,9 @@ mod tests {
362381
363382 // Validate attributes
364383 #[ cfg( not( feature = "experimental_metadata_attributes" ) ) ]
365- assert_eq ! ( log. record. attributes_iter( ) . count( ) , 3 ) ;
366- #[ cfg( feature = "experimental_metadata_attributes" ) ]
367384 assert_eq ! ( log. record. attributes_iter( ) . count( ) , 7 ) ;
385+ #[ cfg( feature = "experimental_metadata_attributes" ) ]
386+ assert_eq ! ( log. record. attributes_iter( ) . count( ) , 11 ) ;
368387 assert ! ( attributes_contains(
369388 & log. record,
370389 & Key :: new( "event_id" ) ,
@@ -380,6 +399,26 @@ mod tests {
380399 & Key :: new( "user_email" ) ,
381400 & AnyValue :: String ( "[email protected] " . into
( ) ) 382401 ) ) ;
402+ assert ! ( attributes_contains(
403+ & log. record,
404+ & Key :: new( "small_u64value" ) ,
405+ & AnyValue :: Int ( 42 . into( ) )
406+ ) ) ;
407+ assert ! ( attributes_contains(
408+ & log. record,
409+ & Key :: new( "big_u64value" ) ,
410+ & AnyValue :: String ( format!( "{}" , u64 :: MAX ) . into( ) )
411+ ) ) ;
412+ assert ! ( attributes_contains(
413+ & log. record,
414+ & Key :: new( "small_usizevalue" ) ,
415+ & AnyValue :: Int ( 42 . into( ) )
416+ ) ) ;
417+ assert ! ( attributes_contains(
418+ & log. record,
419+ & Key :: new( "big_usizevalue" ) ,
420+ & AnyValue :: String ( format!( "{}" , u64 :: MAX ) . into( ) )
421+ ) ) ;
383422 #[ cfg( feature = "experimental_metadata_attributes" ) ]
384423 {
385424 assert ! ( attributes_contains(
@@ -753,6 +792,10 @@ mod tests {
753792 TraceFlags :: SAMPLED
754793 ) ;
755794
795+ for attribute in log. record . attributes_iter ( ) {
796+ println ! ( "key: {:?}, value: {:?}" , attribute. 0 , attribute. 1 ) ;
797+ }
798+
756799 // Attributes can be polluted when we don't use this feature.
757800 #[ cfg( feature = "experimental_metadata_attributes" ) ]
758801 assert_eq ! ( log. record. attributes_iter( ) . count( ) , 4 ) ;
0 commit comments