@@ -363,14 +363,14 @@ func TestEmptyAttributes(t *testing.T) {
363
363
}{
364
364
{
365
365
name : "Empty Attributes Map" ,
366
- payload : `{ "ClientIP": "89.163.253.200", "ClientRequestHost": "www.theburritobot0.com", "ClientRequestMethod": "GET", "ClientRequestURI": "/static/img/testimonial-hipster.png", "EdgeEndTimestamp": "2023-03-03T05:30:05Z", "EdgeResponseBytes": "69045", "EdgeResponseStatus": "200", "EdgeStartTimestamp": "2023-03-03T05:29:05Z", "RayID": "3a6050bcbe121a87" }
367
- { "ClientIP" : "89.163.253.201", "ClientRequestHost": "www.theburritobot1.com", "ClientRequestMethod": "GET", "ClientRequestURI": "/static/img/testimonial-hipster.png", "EdgeEndTimestamp": "2023-03-03T05:30:05Z", "EdgeResponseBytes": "69045", "EdgeResponseStatus": "200", "EdgeStartTimestamp": "2023-03-03T05:29:05Z", "RayID": "3a6050bcbe121a87" }` ,
366
+ payload : `{ "ClientIP": "89.163.253.200", "ClientRequestHost": "www.theburritobot0.com", "ClientRequestMethod": "GET", "ClientRequestURI": "/static/img/testimonial-hipster.png", "EdgeEndTimestamp": "2023-03-03T05:30:05Z", "EdgeResponseBytes": "69045", "EdgeResponseStatus": "200", "EdgeStartTimestamp": "2023-03-03T05:29:05Z", "RayID": "3a6050bcbe121a87", "RequestHeaders": { "Content-Type": "application/json" } }
367
+ { "ClientIP" : "89.163.253.201", "ClientRequestHost": "www.theburritobot1.com", "ClientRequestMethod": "GET", "ClientRequestURI": "/static/img/testimonial-hipster.png", "EdgeEndTimestamp": "2023-03-03T05:30:05Z", "EdgeResponseBytes": "69045", "EdgeResponseStatus": "200", "EdgeStartTimestamp": "2023-03-03T05:29:05Z", "RayID": "3a6050bcbe121a87", "RequestHeaders": { "Content-Type": "application/json" } }` ,
368
368
attributes : map [string ]string {},
369
369
},
370
370
{
371
371
name : "nil Attributes" ,
372
- payload : `{ "ClientIP": "89.163.253.200", "ClientRequestHost": "www.theburritobot0.com", "ClientRequestMethod": "GET", "ClientRequestURI": "/static/img/testimonial-hipster.png", "EdgeEndTimestamp": "2023-03-03T05:30:05Z", "EdgeResponseBytes": "69045", "EdgeResponseStatus": "200", "EdgeStartTimestamp": "2023-03-03T05:29:05Z", "RayID": "3a6050bcbe121a87" }
373
- { "ClientIP" : "89.163.253.201", "ClientRequestHost": "www.theburritobot1.com", "ClientRequestMethod": "GET", "ClientRequestURI": "/static/img/testimonial-hipster.png", "EdgeEndTimestamp": "2023-03-03T05:30:05Z", "EdgeResponseBytes": "69045", "EdgeResponseStatus": "200", "EdgeStartTimestamp": "2023-03-03T05:29:05Z", "RayID": "3a6050bcbe121a87" }` ,
372
+ payload : `{ "ClientIP": "89.163.253.200", "ClientRequestHost": "www.theburritobot0.com", "ClientRequestMethod": "GET", "ClientRequestURI": "/static/img/testimonial-hipster.png", "EdgeEndTimestamp": "2023-03-03T05:30:05Z", "EdgeResponseBytes": "69045", "EdgeResponseStatus": "200", "EdgeStartTimestamp": "2023-03-03T05:29:05Z", "RayID": "3a6050bcbe121a87", "RequestHeaders": { "Content-Type": "application/json" } }
373
+ { "ClientIP" : "89.163.253.201", "ClientRequestHost": "www.theburritobot1.com", "ClientRequestMethod": "GET", "ClientRequestURI": "/static/img/testimonial-hipster.png", "EdgeEndTimestamp": "2023-03-03T05:30:05Z", "EdgeResponseBytes": "69045", "EdgeResponseStatus": "200", "EdgeStartTimestamp": "2023-03-03T05:29:05Z", "RayID": "3a6050bcbe121a87", "RequestHeaders": { "Content-Type": "application/json" } }` ,
374
374
attributes : nil ,
375
375
},
376
376
}
@@ -381,6 +381,94 @@ func TestEmptyAttributes(t *testing.T) {
381
381
sl := rl .ScopeLogs ().AppendEmpty ()
382
382
sl .Scope ().SetName ("github.com/open-telemetry/opentelemetry-collector-contrib/receiver/cloudflarereceiver" )
383
383
384
+ for idx , line := range strings .Split (payload , "\n " ) {
385
+ lr := sl .LogRecords ().AppendEmpty ()
386
+
387
+ require .NoError (t , lr .Attributes ().FromRaw (map [string ]any {
388
+ "ClientIP" : fmt .Sprintf ("89.163.253.%d" , 200 + idx ),
389
+ "ClientRequestHost" : fmt .Sprintf ("www.theburritobot%d.com" , idx ),
390
+ "ClientRequestMethod" : "GET" ,
391
+ "ClientRequestURI" : "/static/img/testimonial-hipster.png" ,
392
+ "EdgeEndTimestamp" : "2023-03-03T05:30:05Z" ,
393
+ "EdgeResponseBytes" : "69045" ,
394
+ "EdgeResponseStatus" : "200" ,
395
+ "EdgeStartTimestamp" : "2023-03-03T05:29:05Z" ,
396
+ "RayID" : "3a6050bcbe121a87" ,
397
+ "RequestHeaders.Content_Type" : "application/json" ,
398
+ }))
399
+
400
+ lr .SetObservedTimestamp (pcommon .NewTimestampFromTime (now ))
401
+ ts , err := time .Parse (time .RFC3339 , "2023-03-03T05:29:05Z" )
402
+ require .NoError (t , err )
403
+ lr .SetTimestamp (pcommon .NewTimestampFromTime (ts ))
404
+ lr .SetSeverityNumber (plog .SeverityNumberInfo )
405
+ lr .SetSeverityText (plog .SeverityNumberInfo .String ())
406
+
407
+ var log map [string ]any
408
+ err = json .Unmarshal ([]byte (line ), & log )
409
+ require .NoError (t , err )
410
+
411
+ payloadToExpectedBody (t , line , lr )
412
+ }
413
+ return logs
414
+ }
415
+
416
+ for _ , tc := range testCases {
417
+ t .Run (tc .name , func (t * testing.T ) {
418
+ recv := newReceiver (t , & Config {
419
+ Logs : LogsConfig {
420
+ Endpoint : "localhost:0" ,
421
+ TLS : & configtls.ServerConfig {},
422
+ TimestampField : "EdgeStartTimestamp" ,
423
+ Attributes : tc .attributes ,
424
+ Separator : "." ,
425
+ },
426
+ },
427
+ & consumertest.LogsSink {},
428
+ )
429
+ var logs plog.Logs
430
+ rawLogs , err := parsePayload ([]byte (tc .payload ))
431
+ if err == nil {
432
+ logs = recv .processLogs (pcommon .NewTimestampFromTime (time .Now ()), rawLogs )
433
+ }
434
+ require .NoError (t , err )
435
+ require .NotNil (t , logs )
436
+ require .NoError (t , plogtest .CompareLogs (expectedLogs (t , tc .payload ), logs , plogtest .IgnoreObservedTimestamp ()))
437
+ })
438
+ }
439
+ }
440
+
441
+ func TestAttributesWithSeparator (t * testing.T ) {
442
+ now := time.Time {}
443
+
444
+ testCases := []struct {
445
+ name string
446
+ payload string
447
+ attributes map [string ]string
448
+ separator string
449
+ }{
450
+ {
451
+ name : "Empty Attributes Map" ,
452
+ payload : `{ "ClientIP": "89.163.253.200", "ClientRequestHost": "www.theburritobot0.com", "ClientRequestMethod": "GET", "ClientRequestURI": "/static/img/testimonial-hipster.png", "EdgeEndTimestamp": "2023-03-03T05:30:05Z", "EdgeResponseBytes": "69045", "EdgeResponseStatus": "200", "EdgeStartTimestamp": "2023-03-03T05:29:05Z", "RayID": "3a6050bcbe121a87", "RequestHeaders": { "Content-Type": "application/json" } }
453
+ { "ClientIP" : "89.163.253.201", "ClientRequestHost": "www.theburritobot1.com", "ClientRequestMethod": "GET", "ClientRequestURI": "/static/img/testimonial-hipster.png", "EdgeEndTimestamp": "2023-03-03T05:30:05Z", "EdgeResponseBytes": "69045", "EdgeResponseStatus": "200", "EdgeStartTimestamp": "2023-03-03T05:29:05Z", "RayID": "3a6050bcbe121a87", "RequestHeaders": { "Content-Type": "application/json" } }` ,
454
+ attributes : map [string ]string {},
455
+ separator : "." ,
456
+ },
457
+ {
458
+ name : "nil Attributes" ,
459
+ payload : `{ "ClientIP": "89.163.253.200", "ClientRequestHost": "www.theburritobot0.com", "ClientRequestMethod": "GET", "ClientRequestURI": "/static/img/testimonial-hipster.png", "EdgeEndTimestamp": "2023-03-03T05:30:05Z", "EdgeResponseBytes": "69045", "EdgeResponseStatus": "200", "EdgeStartTimestamp": "2023-03-03T05:29:05Z", "RayID": "3a6050bcbe121a87", "RequestHeaders": { "Content-Type": "application/json" } }
460
+ { "ClientIP" : "89.163.253.201", "ClientRequestHost": "www.theburritobot1.com", "ClientRequestMethod": "GET", "ClientRequestURI": "/static/img/testimonial-hipster.png", "EdgeEndTimestamp": "2023-03-03T05:30:05Z", "EdgeResponseBytes": "69045", "EdgeResponseStatus": "200", "EdgeStartTimestamp": "2023-03-03T05:29:05Z", "RayID": "3a6050bcbe121a87", "RequestHeaders": { "Content-Type": "application/json" } }` ,
461
+ attributes : nil ,
462
+ separator : "_test_" ,
463
+ },
464
+ }
465
+
466
+ expectedLogs := func (t * testing.T , payload string , separator string ) plog.Logs {
467
+ logs := plog .NewLogs ()
468
+ rl := logs .ResourceLogs ().AppendEmpty ()
469
+ sl := rl .ScopeLogs ().AppendEmpty ()
470
+ sl .Scope ().SetName ("github.com/open-telemetry/opentelemetry-collector-contrib/receiver/cloudflarereceiver" )
471
+
384
472
for idx , line := range strings .Split (payload , "\n " ) {
385
473
lr := sl .LogRecords ().AppendEmpty ()
386
474
@@ -394,6 +482,7 @@ func TestEmptyAttributes(t *testing.T) {
394
482
"EdgeResponseStatus" : "200" ,
395
483
"EdgeStartTimestamp" : "2023-03-03T05:29:05Z" ,
396
484
"RayID" : "3a6050bcbe121a87" ,
485
+ fmt .Sprintf ("RequestHeaders%sContent_Type" , separator ): "application/json" ,
397
486
}))
398
487
399
488
lr .SetObservedTimestamp (pcommon .NewTimestampFromTime (now ))
@@ -409,7 +498,85 @@ func TestEmptyAttributes(t *testing.T) {
409
498
410
499
payloadToExpectedBody (t , line , lr )
411
500
}
501
+ return logs
502
+ }
412
503
504
+ for _ , tc := range testCases {
505
+ t .Run (tc .name , func (t * testing.T ) {
506
+ recv := newReceiver (t , & Config {
507
+ Logs : LogsConfig {
508
+ Endpoint : "localhost:0" ,
509
+ TLS : & configtls.ServerConfig {},
510
+ TimestampField : "EdgeStartTimestamp" ,
511
+ Attributes : tc .attributes ,
512
+ Separator : tc .separator ,
513
+ },
514
+ },
515
+ & consumertest.LogsSink {},
516
+ )
517
+ var logs plog.Logs
518
+ rawLogs , err := parsePayload ([]byte (tc .payload ))
519
+ if err == nil {
520
+ logs = recv .processLogs (pcommon .NewTimestampFromTime (time .Now ()), rawLogs )
521
+ }
522
+ require .NoError (t , err )
523
+ require .NotNil (t , logs )
524
+ require .NoError (t , plogtest .CompareLogs (expectedLogs (t , tc .payload , tc .separator ), logs , plogtest .IgnoreObservedTimestamp ()))
525
+ })
526
+ }
527
+ }
528
+
529
+ func TestMultipleMapAttributes (t * testing.T ) {
530
+ now := time.Time {}
531
+
532
+ testCases := []struct {
533
+ name string
534
+ payload string
535
+ attributes map [string ]string
536
+ }{
537
+ {
538
+ name : "Multi Map Attributes" ,
539
+ payload : `{ "ClientIP": "89.163.253.200", "ClientRequestHost": "www.theburritobot0.com", "ClientRequestMethod": "GET", "ClientRequestURI": "/static/img/testimonial-hipster.png", "EdgeEndTimestamp": "2023-03-03T05:30:05Z", "EdgeResponseBytes": "69045", "EdgeResponseStatus": "200", "EdgeStartTimestamp": "2023-03-03T05:29:05Z", "RayID": "3a6050bcbe121a87", "RequestHeaders": { "Cookie-new": { "x-m2-new": "sensitive" } } }
540
+ { "ClientIP" : "89.163.253.201", "ClientRequestHost": "www.theburritobot1.com", "ClientRequestMethod": "GET", "ClientRequestURI": "/static/img/testimonial-hipster.png", "EdgeEndTimestamp": "2023-03-03T05:30:05Z", "EdgeResponseBytes": "69045", "EdgeResponseStatus": "200", "EdgeStartTimestamp": "2023-03-03T05:29:05Z", "RayID": "3a6050bcbe121a87", "RequestHeaders": { "Cookie-new": { "x-m2-new": "sensitive" } } }` ,
541
+ attributes : map [string ]string {},
542
+ },
543
+ }
544
+
545
+ expectedLogs := func (t * testing.T , payload string ) plog.Logs {
546
+ logs := plog .NewLogs ()
547
+ rl := logs .ResourceLogs ().AppendEmpty ()
548
+ sl := rl .ScopeLogs ().AppendEmpty ()
549
+ sl .Scope ().SetName ("github.com/open-telemetry/opentelemetry-collector-contrib/receiver/cloudflarereceiver" )
550
+
551
+ for idx , line := range strings .Split (payload , "\n " ) {
552
+ lr := sl .LogRecords ().AppendEmpty ()
553
+
554
+ require .NoError (t , lr .Attributes ().FromRaw (map [string ]any {
555
+ "ClientIP" : fmt .Sprintf ("89.163.253.%d" , 200 + idx ),
556
+ "ClientRequestHost" : fmt .Sprintf ("www.theburritobot%d.com" , idx ),
557
+ "ClientRequestMethod" : "GET" ,
558
+ "ClientRequestURI" : "/static/img/testimonial-hipster.png" ,
559
+ "EdgeEndTimestamp" : "2023-03-03T05:30:05Z" ,
560
+ "EdgeResponseBytes" : "69045" ,
561
+ "EdgeResponseStatus" : "200" ,
562
+ "EdgeStartTimestamp" : "2023-03-03T05:29:05Z" ,
563
+ "RayID" : "3a6050bcbe121a87" ,
564
+ "RequestHeaders.Cookie_new.x_m2_new" : "sensitive" ,
565
+ }))
566
+
567
+ lr .SetObservedTimestamp (pcommon .NewTimestampFromTime (now ))
568
+ ts , err := time .Parse (time .RFC3339 , "2023-03-03T05:29:05Z" )
569
+ require .NoError (t , err )
570
+ lr .SetTimestamp (pcommon .NewTimestampFromTime (ts ))
571
+ lr .SetSeverityNumber (plog .SeverityNumberInfo )
572
+ lr .SetSeverityText (plog .SeverityNumberInfo .String ())
573
+
574
+ var log map [string ]any
575
+ err = json .Unmarshal ([]byte (line ), & log )
576
+ require .NoError (t , err )
577
+
578
+ payloadToExpectedBody (t , line , lr )
579
+ }
413
580
return logs
414
581
}
415
582
@@ -421,6 +588,7 @@ func TestEmptyAttributes(t *testing.T) {
421
588
TLS : & configtls.ServerConfig {},
422
589
TimestampField : "EdgeStartTimestamp" ,
423
590
Attributes : tc .attributes ,
591
+ Separator : "." ,
424
592
},
425
593
},
426
594
& consumertest.LogsSink {},
0 commit comments