@@ -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 ( ) {
@@ -253,27 +259,33 @@ mod tests {
253
259
254
260
// Validate attributes
255
261
#[ cfg( not( feature = "experimental_metadata_attributes" ) ) ]
256
- assert_eq ! ( log. record. attributes_len ( ) , 3 ) ;
262
+ assert_eq ! ( log. record. attributes_iter ( ) . count ( ) , 3 ) ;
257
263
#[ cfg( feature = "experimental_metadata_attributes" ) ]
258
- assert_eq ! ( log. record. attributes_len( ) , 8 ) ;
259
- assert ! ( log
260
- . record
261
- . attributes_contains( & Key :: new( "event_id" ) , & AnyValue :: Int ( 20 ) ) ) ;
262
- assert ! ( log
263
- . record
264
- . attributes_contains( & Key :: new( "user_name" ) , & AnyValue :: String ( "otel" . into( ) ) ) ) ;
265
- assert ! ( log. record. attributes_contains(
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,
266
277
& Key :: new( "user_email" ) ,
267
278
& AnyValue :: String ( "otel@opentelemetry.io" . into( ) )
268
279
) ) ;
269
-
270
280
#[ cfg( feature = "experimental_metadata_attributes" ) ]
271
281
{
272
- assert ! ( log. record. attributes_contains(
282
+ assert ! ( attributes_contains(
283
+ & log. record,
273
284
& Key :: new( "code.filename" ) ,
274
285
& AnyValue :: String ( "layer.rs" . into( ) )
275
286
) ) ;
276
- assert ! ( log. record. attributes_contains(
287
+ assert ! ( attributes_contains(
288
+ & log. record,
277
289
& Key :: new( "code.namespace" ) ,
278
290
& AnyValue :: String ( "opentelemetry_appender_tracing::layer::tests" . into( ) )
279
291
) ) ;
@@ -285,7 +297,6 @@ mod tests {
285
297
. attributes_iter ( )
286
298
. map ( |( key, _) | key. clone ( ) )
287
299
. collect ( ) ;
288
-
289
300
assert ! ( attributes_key. contains( & Key :: new( "code.filepath" ) ) ) ;
290
301
assert ! ( attributes_key. contains( & Key :: new( "code.lineno" ) ) ) ;
291
302
assert ! ( attributes_key. contains( & Key :: new( "log.target" ) ) ) ;
@@ -360,33 +371,39 @@ mod tests {
360
371
361
372
// validate attributes.
362
373
#[ cfg( not( feature = "experimental_metadata_attributes" ) ) ]
363
- assert_eq ! ( log. record. attributes_len ( ) , 3 ) ;
374
+ assert_eq ! ( log. record. attributes_iter ( ) . count ( ) , 3 ) ;
364
375
#[ cfg( feature = "experimental_metadata_attributes" ) ]
365
- assert_eq ! ( log. record. attributes_len( ) , 8 ) ;
366
- assert ! ( log
367
- . record
368
- . attributes_contains( & Key :: new( "event_id" ) , & AnyValue :: Int ( 20 . into( ) ) ) ) ;
369
- assert ! ( log
370
- . record
371
- . attributes_contains( & Key :: new( "user_name" ) , & AnyValue :: String ( "otel" . into( ) ) ) ) ;
372
- assert ! ( log. record. attributes_contains(
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,
373
389
& Key :: new( "user_email" ) ,
374
390
& AnyValue :: String ( "otel@opentelemetry.io" . into( ) )
375
391
) ) ;
376
392
#[ cfg( feature = "experimental_metadata_attributes" ) ]
377
393
{
378
- assert ! ( log. record. attributes_contains(
394
+ assert ! ( attributes_contains(
395
+ & log. record,
379
396
& Key :: new( "code.filename" ) ,
380
397
& AnyValue :: String ( "layer.rs" . into( ) )
381
398
) ) ;
382
- assert ! ( log. record. attributes_contains(
399
+ assert ! ( attributes_contains(
400
+ & log. record,
383
401
& Key :: new( "code.namespace" ) ,
384
402
& AnyValue :: String ( "opentelemetry_appender_tracing::layer::tests" . into( ) )
385
403
) ) ;
386
404
// The other 3 experimental_metadata_attributes are too unstable to check their value.
387
405
// Ex.: The path will be different on a Windows and Linux machine.
388
406
// Ex.: The line can change easily if someone makes changes in this source file.
389
-
390
407
let attributes_key: Vec < Key > = log
391
408
. record
392
409
. attributes_iter ( )
@@ -436,15 +453,17 @@ mod tests {
436
453
437
454
// Attributes can be polluted when we don't use this feature.
438
455
#[ cfg( feature = "experimental_metadata_attributes" ) ]
439
- assert_eq ! ( log. record. attributes_len ( ) , 5 ) ;
456
+ assert_eq ! ( log. record. attributes_iter ( ) . count ( ) , 5 ) ;
440
457
441
458
#[ cfg( feature = "experimental_metadata_attributes" ) ]
442
459
{
443
- assert ! ( log. record. attributes_contains(
460
+ assert ! ( attributes_contains(
461
+ & log. record,
444
462
& Key :: new( "code.filename" ) ,
445
463
& AnyValue :: String ( "layer.rs" . into( ) )
446
464
) ) ;
447
- assert ! ( log. record. attributes_contains(
465
+ assert ! ( attributes_contains(
466
+ & log. record,
448
467
& Key :: new( "code.namespace" ) ,
449
468
& AnyValue :: String ( "opentelemetry_appender_tracing::layer::tests" . into( ) )
450
469
) ) ;
@@ -529,18 +548,19 @@ mod tests {
529
548
TraceFlags :: SAMPLED
530
549
) ;
531
550
532
- // validate attributes.
533
551
// Attributes can be polluted when we don't use this feature.
534
552
#[ cfg( feature = "experimental_metadata_attributes" ) ]
535
- assert_eq ! ( log. record. attributes_len ( ) , 5 ) ;
553
+ assert_eq ! ( log. record. attributes_iter ( ) . count ( ) , 5 ) ;
536
554
537
555
#[ cfg( feature = "experimental_metadata_attributes" ) ]
538
556
{
539
- assert ! ( log. record. attributes_contains(
557
+ assert ! ( attributes_contains(
558
+ & log. record,
540
559
& Key :: new( "code.filename" ) ,
541
560
& AnyValue :: String ( "layer.rs" . into( ) )
542
561
) ) ;
543
- assert ! ( log. record. attributes_contains(
562
+ assert ! ( attributes_contains(
563
+ & log. record,
544
564
& Key :: new( "code.namespace" ) ,
545
565
& AnyValue :: String ( "opentelemetry_appender_tracing::layer::tests" . into( ) )
546
566
) ) ;
0 commit comments