@@ -114,6 +114,22 @@ impl<LR: LogRecord> tracing::field::Visit for EventVisitor<'_, LR> {
114
114
. add_attribute ( Key :: new ( field. name ( ) ) , AnyValue :: from ( value) ) ;
115
115
}
116
116
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
+
117
133
// TODO: Remaining field types from AnyValue : Bytes, ListAny, Boolean
118
134
}
119
135
@@ -331,7 +347,11 @@ mod tests {
331
347
let _guard = tracing:: subscriber:: set_default ( subscriber) ;
332
348
333
349
// Act
334
- error ! ( name: "my-event-name" , target: "my-system" , event_id = 20 , user_name = "otel" , user_email = "otel@opentelemetry.io" ) ;
350
+ let small_u64value: u64 = 42 ;
351
+ let big_u64value: u64 = u64:: MAX ;
352
+ let small_usizevalue: usize = 42 ;
353
+ let big_usizevalue: usize = usize:: MAX ;
354
+ error ! ( name: "my-event-name" , target: "my-system" , event_id = 20 , small_u64value, big_u64value, small_usizevalue, big_usizevalue, user_name = "otel" , user_email = "otel@opentelemetry.io" ) ;
335
355
assert ! ( logger_provider. force_flush( ) . is_ok( ) ) ;
336
356
337
357
// Assert TODO: move to helper methods
@@ -362,9 +382,9 @@ mod tests {
362
382
363
383
// Validate attributes
364
384
#[ cfg( not( feature = "experimental_metadata_attributes" ) ) ]
365
- assert_eq ! ( log. record. attributes_iter( ) . count( ) , 3 ) ;
366
- #[ cfg( feature = "experimental_metadata_attributes" ) ]
367
385
assert_eq ! ( log. record. attributes_iter( ) . count( ) , 7 ) ;
386
+ #[ cfg( feature = "experimental_metadata_attributes" ) ]
387
+ assert_eq ! ( log. record. attributes_iter( ) . count( ) , 11 ) ;
368
388
assert ! ( attributes_contains(
369
389
& log. record,
370
390
& Key :: new( "event_id" ) ,
@@ -380,6 +400,26 @@ mod tests {
380
400
& Key :: new( "user_email" ) ,
381
401
& AnyValue :: String ( "otel@opentelemetry.io" . into( ) )
382
402
) ) ;
403
+ assert ! ( attributes_contains(
404
+ & log. record,
405
+ & Key :: new( "small_u64value" ) ,
406
+ & AnyValue :: Int ( 42 . into( ) )
407
+ ) ) ;
408
+ assert ! ( attributes_contains(
409
+ & log. record,
410
+ & Key :: new( "big_u64value" ) ,
411
+ & AnyValue :: String ( format!( "{}" , u64 :: MAX ) . into( ) )
412
+ ) ) ;
413
+ assert ! ( attributes_contains(
414
+ & log. record,
415
+ & Key :: new( "small_usizevalue" ) ,
416
+ & AnyValue :: Int ( 42 . into( ) )
417
+ ) ) ;
418
+ assert ! ( attributes_contains(
419
+ & log. record,
420
+ & Key :: new( "big_usizevalue" ) ,
421
+ & AnyValue :: String ( format!( "{}" , u64 :: MAX ) . into( ) )
422
+ ) ) ;
383
423
#[ cfg( feature = "experimental_metadata_attributes" ) ]
384
424
{
385
425
assert ! ( attributes_contains(
@@ -753,6 +793,10 @@ mod tests {
753
793
TraceFlags :: SAMPLED
754
794
) ;
755
795
796
+ for attribute in log. record . attributes_iter ( ) {
797
+ println ! ( "key: {:?}, value: {:?}" , attribute. 0 , attribute. 1 ) ;
798
+ }
799
+
756
800
// Attributes can be polluted when we don't use this feature.
757
801
#[ cfg( feature = "experimental_metadata_attributes" ) ]
758
802
assert_eq ! ( log. record. attributes_iter( ) . count( ) , 4 ) ;
0 commit comments