@@ -168,7 +168,7 @@ where
168
168
log_record. set_target ( meta. target ( ) . to_string ( ) ) ;
169
169
log_record. set_event_name ( meta. name ( ) ) ;
170
170
log_record. set_severity_number ( severity_of_level ( meta. level ( ) ) ) ;
171
- log_record. set_severity_text ( meta. level ( ) . to_string ( ) . into ( ) ) ;
171
+ log_record. set_severity_text ( meta. level ( ) . as_str ( ) . into ( ) ) ;
172
172
let mut visitor = EventVisitor :: new ( & mut log_record) ;
173
173
#[ cfg( feature = "experimental_metadata_attributes" ) ]
174
174
visitor. visit_experimental_metadata ( meta) ;
@@ -208,13 +208,19 @@ mod tests {
208
208
use opentelemetry:: trace:: TracerProvider as _;
209
209
use opentelemetry:: trace:: { TraceContextExt , TraceFlags , Tracer } ;
210
210
use opentelemetry:: { logs:: AnyValue , Key } ;
211
- use opentelemetry_sdk:: logs:: LoggerProvider ;
211
+ use opentelemetry_sdk:: logs:: { LogRecord , LoggerProvider } ;
212
212
use opentelemetry_sdk:: testing:: logs:: InMemoryLogsExporter ;
213
213
use opentelemetry_sdk:: trace;
214
214
use opentelemetry_sdk:: trace:: { Sampler , TracerProvider } ;
215
215
use tracing:: error;
216
216
use tracing_subscriber:: layer:: SubscriberExt ;
217
217
218
+ pub fn attributes_contains ( log_record : & LogRecord , key : & Key , value : & AnyValue ) -> bool {
219
+ log_record
220
+ . attributes_iter ( )
221
+ . any ( |( k, v) | k == key && v == value)
222
+ }
223
+
218
224
// cargo test --features=testing
219
225
#[ test]
220
226
fn tracing_appender_standalone ( ) {
@@ -252,29 +258,45 @@ mod tests {
252
258
assert ! ( log. record. trace_context. is_none( ) ) ;
253
259
254
260
// Validate attributes
255
- let attributes: Vec < ( Key , AnyValue ) > = log
256
- . record
257
- . attributes
258
- . clone ( )
259
- . expect ( "Attributes are expected" ) ;
260
261
#[ cfg( not( feature = "experimental_metadata_attributes" ) ) ]
261
- assert_eq ! ( attributes . len ( ) , 3 ) ;
262
+ assert_eq ! ( log . record . attributes_iter ( ) . count ( ) , 3 ) ;
262
263
#[ cfg( feature = "experimental_metadata_attributes" ) ]
263
- assert_eq ! ( attributes. len( ) , 8 ) ;
264
- assert ! ( attributes. contains( & ( Key :: new( "event_id" ) , 20 . into( ) ) ) ) ;
265
- assert ! ( attributes. contains( & ( Key :: new( "user_name" ) , "otel" . into( ) ) ) ) ;
266
- assert ! ( attributes. contains( & ( Key :: new( "user_email" ) , "otel@opentelemetry.io" . into( ) ) ) ) ;
264
+ assert_eq ! ( log. record. attributes_iter( ) . count( ) , 8 ) ;
265
+ assert ! ( attributes_contains(
266
+ & log. record,
267
+ & Key :: new( "event_id" ) ,
268
+ & AnyValue :: Int ( 20 )
269
+ ) ) ;
270
+ assert ! ( attributes_contains(
271
+ & log. record,
272
+ & Key :: new( "user_name" ) ,
273
+ & AnyValue :: String ( "otel" . into( ) )
274
+ ) ) ;
275
+ assert ! ( attributes_contains(
276
+ & log. record,
277
+ & Key :: new( "user_email" ) ,
278
+ & AnyValue :: String ( "otel@opentelemetry.io" . into( ) )
279
+ ) ) ;
267
280
#[ cfg( feature = "experimental_metadata_attributes" ) ]
268
281
{
269
- assert ! ( attributes. contains( & ( Key :: new( "code.filename" ) , "layer.rs" . into( ) ) ) ) ;
270
- assert ! ( attributes. contains( & (
271
- Key :: new( "code.namespace" ) ,
272
- "opentelemetry_appender_tracing::layer::tests" . into( )
273
- ) ) ) ;
282
+ assert ! ( attributes_contains(
283
+ & log. record,
284
+ & Key :: new( "code.filename" ) ,
285
+ & AnyValue :: String ( "layer.rs" . into( ) )
286
+ ) ) ;
287
+ assert ! ( attributes_contains(
288
+ & log. record,
289
+ & Key :: new( "code.namespace" ) ,
290
+ & AnyValue :: String ( "opentelemetry_appender_tracing::layer::tests" . into( ) )
291
+ ) ) ;
274
292
// The other 3 experimental_metadata_attributes are too unstable to check their value.
275
293
// Ex.: The path will be different on a Windows and Linux machine.
276
294
// Ex.: The line can change easily if someone makes changes in this source file.
277
- let attributes_key: Vec < Key > = attributes. iter ( ) . map ( |( key, _) | key. clone ( ) ) . collect ( ) ;
295
+ let attributes_key: Vec < Key > = log
296
+ . record
297
+ . attributes_iter ( )
298
+ . map ( |( key, _) | key. clone ( ) )
299
+ . collect ( ) ;
278
300
assert ! ( attributes_key. contains( & Key :: new( "code.filepath" ) ) ) ;
279
301
assert ! ( attributes_key. contains( & Key :: new( "code.lineno" ) ) ) ;
280
302
assert ! ( attributes_key. contains( & Key :: new( "log.target" ) ) ) ;
@@ -348,29 +370,45 @@ mod tests {
348
370
) ;
349
371
350
372
// validate attributes.
351
- let attributes: Vec < ( Key , AnyValue ) > = log
352
- . record
353
- . attributes
354
- . clone ( )
355
- . expect ( "Attributes are expected" ) ;
356
373
#[ cfg( not( feature = "experimental_metadata_attributes" ) ) ]
357
- assert_eq ! ( attributes . len ( ) , 3 ) ;
374
+ assert_eq ! ( log . record . attributes_iter ( ) . count ( ) , 3 ) ;
358
375
#[ cfg( feature = "experimental_metadata_attributes" ) ]
359
- assert_eq ! ( attributes. len( ) , 8 ) ;
360
- assert ! ( attributes. contains( & ( Key :: new( "event_id" ) , 20 . into( ) ) ) ) ;
361
- assert ! ( attributes. contains( & ( Key :: new( "user_name" ) , "otel" . into( ) ) ) ) ;
362
- assert ! ( attributes. contains( & ( Key :: new( "user_email" ) , "otel@opentelemetry.io" . into( ) ) ) ) ;
376
+ assert_eq ! ( log. record. attributes_iter( ) . count( ) , 8 ) ;
377
+ assert ! ( attributes_contains(
378
+ & log. record,
379
+ & Key :: new( "event_id" ) ,
380
+ & AnyValue :: Int ( 20 . into( ) )
381
+ ) ) ;
382
+ assert ! ( attributes_contains(
383
+ & log. record,
384
+ & Key :: new( "user_name" ) ,
385
+ & AnyValue :: String ( "otel" . into( ) )
386
+ ) ) ;
387
+ assert ! ( attributes_contains(
388
+ & log. record,
389
+ & Key :: new( "user_email" ) ,
390
+ & AnyValue :: String ( "otel@opentelemetry.io" . into( ) )
391
+ ) ) ;
363
392
#[ cfg( feature = "experimental_metadata_attributes" ) ]
364
393
{
365
- assert ! ( attributes. contains( & ( Key :: new( "code.filename" ) , "layer.rs" . into( ) ) ) ) ;
366
- assert ! ( attributes. contains( & (
367
- Key :: new( "code.namespace" ) ,
368
- "opentelemetry_appender_tracing::layer::tests" . into( )
369
- ) ) ) ;
394
+ assert ! ( attributes_contains(
395
+ & log. record,
396
+ & Key :: new( "code.filename" ) ,
397
+ & AnyValue :: String ( "layer.rs" . into( ) )
398
+ ) ) ;
399
+ assert ! ( attributes_contains(
400
+ & log. record,
401
+ & Key :: new( "code.namespace" ) ,
402
+ & AnyValue :: String ( "opentelemetry_appender_tracing::layer::tests" . into( ) )
403
+ ) ) ;
370
404
// The other 3 experimental_metadata_attributes are too unstable to check their value.
371
405
// Ex.: The path will be different on a Windows and Linux machine.
372
406
// Ex.: The line can change easily if someone makes changes in this source file.
373
- let attributes_key: Vec < Key > = attributes. iter ( ) . map ( |( key, _) | key. clone ( ) ) . collect ( ) ;
407
+ let attributes_key: Vec < Key > = log
408
+ . record
409
+ . attributes_iter ( )
410
+ . map ( |( key, _) | key. clone ( ) )
411
+ . collect ( ) ;
374
412
assert ! ( attributes_key. contains( & Key :: new( "code.filepath" ) ) ) ;
375
413
assert ! ( attributes_key. contains( & Key :: new( "code.lineno" ) ) ) ;
376
414
assert ! ( attributes_key. contains( & Key :: new( "log.target" ) ) ) ;
@@ -413,29 +451,30 @@ mod tests {
413
451
// Validate trace context is none.
414
452
assert ! ( log. record. trace_context. is_none( ) ) ;
415
453
416
- // Validate attributes
417
- #[ cfg( feature = "experimental_metadata_attributes" ) ]
418
- let attributes: Vec < ( Key , AnyValue ) > = log
419
- . record
420
- . attributes
421
- . clone ( )
422
- . expect ( "Attributes are expected" ) ;
423
-
424
454
// Attributes can be polluted when we don't use this feature.
425
455
#[ cfg( feature = "experimental_metadata_attributes" ) ]
426
- assert_eq ! ( attributes . len ( ) , 5 ) ;
456
+ assert_eq ! ( log . record . attributes_iter ( ) . count ( ) , 5 ) ;
427
457
428
458
#[ cfg( feature = "experimental_metadata_attributes" ) ]
429
459
{
430
- assert ! ( attributes. contains( & ( Key :: new( "code.filename" ) , "layer.rs" . into( ) ) ) ) ;
431
- assert ! ( attributes. contains( & (
432
- Key :: new( "code.namespace" ) ,
433
- "opentelemetry_appender_tracing::layer::tests" . into( )
434
- ) ) ) ;
460
+ assert ! ( attributes_contains(
461
+ & log. record,
462
+ & Key :: new( "code.filename" ) ,
463
+ & AnyValue :: String ( "layer.rs" . into( ) )
464
+ ) ) ;
465
+ assert ! ( attributes_contains(
466
+ & log. record,
467
+ & Key :: new( "code.namespace" ) ,
468
+ & AnyValue :: String ( "opentelemetry_appender_tracing::layer::tests" . into( ) )
469
+ ) ) ;
435
470
// The other 3 experimental_metadata_attributes are too unstable to check their value.
436
471
// Ex.: The path will be different on a Windows and Linux machine.
437
472
// Ex.: The line can change easily if someone makes changes in this source file.
438
- let attributes_key: Vec < Key > = attributes. iter ( ) . map ( |( key, _) | key. clone ( ) ) . collect ( ) ;
473
+ let attributes_key: Vec < Key > = log
474
+ . record
475
+ . attributes_iter ( )
476
+ . map ( |( key, _) | key. clone ( ) )
477
+ . collect ( ) ;
439
478
assert ! ( attributes_key. contains( & Key :: new( "code.filepath" ) ) ) ;
440
479
assert ! ( attributes_key. contains( & Key :: new( "code.lineno" ) ) ) ;
441
480
assert ! ( attributes_key. contains( & Key :: new( "log.target" ) ) ) ;
@@ -509,29 +548,30 @@ mod tests {
509
548
TraceFlags :: SAMPLED
510
549
) ;
511
550
512
- // validate attributes.
513
- #[ cfg( feature = "experimental_metadata_attributes" ) ]
514
- let attributes: Vec < ( Key , AnyValue ) > = log
515
- . record
516
- . attributes
517
- . clone ( )
518
- . expect ( "Attributes are expected" ) ;
519
-
520
551
// Attributes can be polluted when we don't use this feature.
521
552
#[ cfg( feature = "experimental_metadata_attributes" ) ]
522
- assert_eq ! ( attributes . len ( ) , 5 ) ;
553
+ assert_eq ! ( log . record . attributes_iter ( ) . count ( ) , 5 ) ;
523
554
524
555
#[ cfg( feature = "experimental_metadata_attributes" ) ]
525
556
{
526
- assert ! ( attributes. contains( & ( Key :: new( "code.filename" ) , "layer.rs" . into( ) ) ) ) ;
527
- assert ! ( attributes. contains( & (
528
- Key :: new( "code.namespace" ) ,
529
- "opentelemetry_appender_tracing::layer::tests" . into( )
530
- ) ) ) ;
557
+ assert ! ( attributes_contains(
558
+ & log. record,
559
+ & Key :: new( "code.filename" ) ,
560
+ & AnyValue :: String ( "layer.rs" . into( ) )
561
+ ) ) ;
562
+ assert ! ( attributes_contains(
563
+ & log. record,
564
+ & Key :: new( "code.namespace" ) ,
565
+ & AnyValue :: String ( "opentelemetry_appender_tracing::layer::tests" . into( ) )
566
+ ) ) ;
531
567
// The other 3 experimental_metadata_attributes are too unstable to check their value.
532
568
// Ex.: The path will be different on a Windows and Linux machine.
533
569
// Ex.: The line can change easily if someone makes changes in this source file.
534
- let attributes_key: Vec < Key > = attributes. iter ( ) . map ( |( key, _) | key. clone ( ) ) . collect ( ) ;
570
+ let attributes_key: Vec < Key > = log
571
+ . record
572
+ . attributes_iter ( )
573
+ . map ( |( key, _) | key. clone ( ) )
574
+ . collect ( ) ;
535
575
assert ! ( attributes_key. contains( & Key :: new( "code.filepath" ) ) ) ;
536
576
assert ! ( attributes_key. contains( & Key :: new( "code.lineno" ) ) ) ;
537
577
assert ! ( attributes_key. contains( & Key :: new( "log.target" ) ) ) ;
0 commit comments