@@ -80,6 +80,18 @@ impl<LR: LogRecord> tracing::field::Visit for EventVisitor<'_, LR> {
80
80
}
81
81
}
82
82
83
+ fn record_error (
84
+ & mut self ,
85
+ _field : & tracing_core:: Field ,
86
+ value : & ( dyn std:: error:: Error + ' static ) ,
87
+ ) {
88
+ self . log_record . add_attribute (
89
+ Key :: new ( "exception.message" ) ,
90
+ AnyValue :: from ( value. to_string ( ) ) ,
91
+ ) ;
92
+ // No ability to get exception.stacktrace or exception.type from the error today.
93
+ }
94
+
83
95
fn record_bytes ( & mut self , field : & tracing_core:: Field , value : & [ u8 ] ) {
84
96
self . log_record
85
97
. add_attribute ( Key :: new ( field. name ( ) ) , AnyValue :: from ( value) ) ;
@@ -135,6 +147,34 @@ impl<LR: LogRecord> tracing::field::Visit for EventVisitor<'_, LR> {
135
147
}
136
148
}
137
149
150
+ fn record_i128 ( & mut self , field : & tracing:: field:: Field , value : i128 ) {
151
+ #[ cfg( feature = "experimental_metadata_attributes" ) ]
152
+ if is_duplicated_metadata ( field. name ( ) ) {
153
+ return ;
154
+ }
155
+ if let Ok ( signed) = i64:: try_from ( value) {
156
+ self . log_record
157
+ . add_attribute ( Key :: new ( field. name ( ) ) , AnyValue :: from ( signed) ) ;
158
+ } else {
159
+ self . log_record
160
+ . add_attribute ( Key :: new ( field. name ( ) ) , AnyValue :: from ( format ! ( "{value:?}" ) ) ) ;
161
+ }
162
+ }
163
+
164
+ fn record_u128 ( & mut self , field : & tracing:: field:: Field , value : u128 ) {
165
+ #[ cfg( feature = "experimental_metadata_attributes" ) ]
166
+ if is_duplicated_metadata ( field. name ( ) ) {
167
+ return ;
168
+ }
169
+ if let Ok ( signed) = i64:: try_from ( value) {
170
+ self . log_record
171
+ . add_attribute ( Key :: new ( field. name ( ) ) , AnyValue :: from ( signed) ) ;
172
+ } else {
173
+ self . log_record
174
+ . add_attribute ( Key :: new ( field. name ( ) ) , AnyValue :: from ( format ! ( "{value:?}" ) ) ) ;
175
+ }
176
+ }
177
+
138
178
// TODO: Remaining field types from AnyValue : Bytes, ListAny, Boolean
139
179
}
140
180
@@ -247,7 +287,7 @@ mod tests {
247
287
use opentelemetry:: trace:: { TraceContextExt , TraceFlags , Tracer } ;
248
288
use opentelemetry:: InstrumentationScope ;
249
289
use opentelemetry:: { logs:: AnyValue , Key } ;
250
- use opentelemetry_sdk:: error:: OTelSdkResult ;
290
+ use opentelemetry_sdk:: error:: { OTelSdkError , OTelSdkResult } ;
251
291
use opentelemetry_sdk:: logs:: { InMemoryLogExporter , LogProcessor } ;
252
292
use opentelemetry_sdk:: logs:: { LogBatch , LogExporter } ;
253
293
use opentelemetry_sdk:: logs:: { SdkLogRecord , SdkLoggerProvider } ;
@@ -355,7 +395,11 @@ mod tests {
355
395
let big_u64value: u64 = u64:: MAX ;
356
396
let small_usizevalue: usize = 42 ;
357
397
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] " ) ;
398
+ let small_u128value: u128 = 42 ;
399
+ let big_u128value: u128 = u128:: MAX ;
400
+ let small_i128value: i128 = 42 ;
401
+ let big_i128value: i128 = i128:: MAX ;
402
+ error ! ( name
: "my-event-name" , target
: "my-system" , event_id =
20 , bytes =
& b"abc" [ ..
] , error =
& OTelSdkError :: AlreadyShutdown as & dyn std
:: error
:: Error , small_u64value
, big_u64value
, small_usizevalue
, big_usizevalue
, small_u128value
, big_u128value
, small_i128value
, big_i128value
, user_name =
"otel" , user_email =
"[email protected] " ) ;
359
403
assert ! ( logger_provider. force_flush( ) . is_ok( ) ) ;
360
404
361
405
// Assert TODO: move to helper methods
@@ -386,9 +430,9 @@ mod tests {
386
430
387
431
// Validate attributes
388
432
#[ cfg( not( feature = "experimental_metadata_attributes" ) ) ]
389
- assert_eq ! ( log. record. attributes_iter( ) . count( ) , 8 ) ;
433
+ assert_eq ! ( log. record. attributes_iter( ) . count( ) , 13 ) ;
390
434
#[ cfg( feature = "experimental_metadata_attributes" ) ]
391
- assert_eq ! ( log. record. attributes_iter( ) . count( ) , 12 ) ;
435
+ assert_eq ! ( log. record. attributes_iter( ) . count( ) , 17 ) ;
392
436
assert ! ( attributes_contains(
393
437
& log. record,
394
438
& Key :: new( "event_id" ) ,
@@ -404,6 +448,11 @@ mod tests {
404
448
& Key :: new( "user_email" ) ,
405
449
& AnyValue :: String ( "[email protected] " . into
( ) )
406
450
) ) ;
451
+ assert ! ( attributes_contains(
452
+ & log. record,
453
+ & Key :: new( "exception.message" ) ,
454
+ & AnyValue :: String ( OTelSdkError :: AlreadyShutdown . to_string( ) . into( ) )
455
+ ) ) ;
407
456
assert ! ( attributes_contains(
408
457
& log. record,
409
458
& Key :: new( "small_u64value" ) ,
@@ -424,6 +473,26 @@ mod tests {
424
473
& Key :: new( "big_usizevalue" ) ,
425
474
& AnyValue :: String ( format!( "{}" , u64 :: MAX ) . into( ) )
426
475
) ) ;
476
+ assert ! ( attributes_contains(
477
+ & log. record,
478
+ & Key :: new( "small_u128value" ) ,
479
+ & AnyValue :: Int ( 42 . into( ) )
480
+ ) ) ;
481
+ assert ! ( attributes_contains(
482
+ & log. record,
483
+ & Key :: new( "big_u128value" ) ,
484
+ & AnyValue :: String ( format!( "{}" , u128 :: MAX ) . into( ) )
485
+ ) ) ;
486
+ assert ! ( attributes_contains(
487
+ & log. record,
488
+ & Key :: new( "small_i128value" ) ,
489
+ & AnyValue :: Int ( 42 . into( ) )
490
+ ) ) ;
491
+ assert ! ( attributes_contains(
492
+ & log. record,
493
+ & Key :: new( "big_i128value" ) ,
494
+ & AnyValue :: String ( format!( "{}" , i128 :: MAX ) . into( ) )
495
+ ) ) ;
427
496
assert ! ( attributes_contains(
428
497
& log. record,
429
498
& Key :: new( "bytes" ) ,
0 commit comments