@@ -191,18 +191,21 @@ impl TraceProcessor for ServerlessTraceProcessor {
191
191
#[ cfg( test) ]
192
192
mod tests {
193
193
use datadog_trace_obfuscation:: obfuscation_config:: ObfuscationConfig ;
194
+ use datadog_trace_utils:: tracer_payload:: TraceChunkProcessor ;
194
195
use std:: {
195
196
collections:: HashMap ,
196
197
sync:: Arc ,
197
198
time:: { SystemTime , UNIX_EPOCH } ,
198
199
} ;
199
200
200
201
use crate :: config:: Config ;
202
+ use crate :: span_pointers:: SpanPointer ;
201
203
use crate :: tags:: provider:: Provider ;
202
- use crate :: traces:: trace_processor:: { self , TraceProcessor } ;
204
+ use crate :: traces:: trace_processor:: { self , ChunkProcessor , TraceProcessor } ;
203
205
use crate :: LAMBDA_RUNTIME_SLUG ;
204
206
use datadog_trace_protobuf:: pb;
205
207
use datadog_trace_utils:: { tracer_header_tags, tracer_payload:: TracerPayloadCollection } ;
208
+ use serde_json:: json;
206
209
207
210
fn get_current_timestamp_nanos ( ) -> i64 {
208
211
i64:: try_from (
@@ -342,4 +345,135 @@ mod tests {
342
345
received_payload. expect( "no payload received" )
343
346
) ;
344
347
}
348
+
349
+ struct SpanPointerTestCase {
350
+ test_name : & ' static str ,
351
+ span_name : & ' static str ,
352
+ span_pointers : Option < Vec < SpanPointer > > ,
353
+ expected_links : Option < serde_json:: Value > ,
354
+ }
355
+
356
+ #[ test]
357
+ fn test_span_pointer_processing ( ) {
358
+ let test_cases = vec ! [
359
+ SpanPointerTestCase {
360
+ test_name: "adds span links to lambda span" ,
361
+ span_name: "aws.lambda" ,
362
+ span_pointers: Some ( vec![
363
+ SpanPointer {
364
+ hash: "hash1" . to_string( ) ,
365
+ kind: "test.kind1" . to_string( ) ,
366
+ } ,
367
+ SpanPointer {
368
+ hash: "hash2" . to_string( ) ,
369
+ kind: "test.kind2" . to_string( ) ,
370
+ } ,
371
+ ] ) ,
372
+ expected_links: Some ( json!( [
373
+ {
374
+ "attributes" : {
375
+ "link.kind" : "span-pointer" ,
376
+ "ptr.dir" : "u" ,
377
+ "ptr.hash" : "hash1" ,
378
+ "ptr.kind" : "test.kind1"
379
+ } ,
380
+ "span_id" : "0" ,
381
+ "trace_id" : "0"
382
+ } ,
383
+ {
384
+ "attributes" : {
385
+ "link.kind" : "span-pointer" ,
386
+ "ptr.dir" : "u" ,
387
+ "ptr.hash" : "hash2" ,
388
+ "ptr.kind" : "test.kind2"
389
+ } ,
390
+ "span_id" : "0" ,
391
+ "trace_id" : "0"
392
+ }
393
+ ] ) ) ,
394
+ } ,
395
+ SpanPointerTestCase {
396
+ test_name: "ignores non-lambda span" ,
397
+ span_name: "not.lambda" ,
398
+ span_pointers: Some ( vec![ SpanPointer {
399
+ hash: "hash1" . to_string( ) ,
400
+ kind: "test.kind1" . to_string( ) ,
401
+ } ] ) ,
402
+ expected_links: None ,
403
+ } ,
404
+ SpanPointerTestCase {
405
+ test_name: "handles empty span pointers" ,
406
+ span_name: "aws.lambda" ,
407
+ span_pointers: Some ( vec![ ] ) ,
408
+ expected_links: None ,
409
+ } ,
410
+ SpanPointerTestCase {
411
+ test_name: "handles none span pointers" ,
412
+ span_name: "aws.lambda" ,
413
+ span_pointers: None ,
414
+ expected_links: None ,
415
+ } ,
416
+ ] ;
417
+
418
+ for case in test_cases {
419
+ let tags_provider = create_tags_provider ( create_test_config ( ) ) ;
420
+
421
+ let span = pb:: Span {
422
+ name : case. span_name . to_string ( ) ,
423
+ meta : HashMap :: new ( ) ,
424
+ ..create_test_span (
425
+ 11 ,
426
+ 222 ,
427
+ 333 ,
428
+ get_current_timestamp_nanos ( ) ,
429
+ true ,
430
+ tags_provider. clone ( ) ,
431
+ )
432
+ } ;
433
+
434
+ let mut processor = ChunkProcessor {
435
+ obfuscation_config : Arc :: new (
436
+ ObfuscationConfig :: new ( ) . expect ( "Failed to create ObfuscationConfig for test" ) ,
437
+ ) ,
438
+ tags_provider,
439
+ span_pointers : case. span_pointers ,
440
+ } ;
441
+
442
+ let mut chunk = pb:: TraceChunk {
443
+ priority : 0 ,
444
+ origin : String :: new ( ) ,
445
+ spans : vec ! [ span] ,
446
+ tags : HashMap :: new ( ) ,
447
+ dropped_trace : false ,
448
+ } ;
449
+
450
+ processor. process ( & mut chunk, 0 ) ;
451
+
452
+ match case. expected_links {
453
+ Some ( expected) => {
454
+ let span_links =
455
+ chunk. spans [ 0 ]
456
+ . meta
457
+ . get ( "_dd.span_links" )
458
+ . unwrap_or_else ( || {
459
+ panic ! ( "[{}] Span links should be present" , case. test_name)
460
+ } ) ;
461
+ let actual_links: serde_json:: Value =
462
+ serde_json:: from_str ( span_links) . expect ( "Should be valid JSON" ) ;
463
+ assert_eq ! (
464
+ actual_links, expected,
465
+ "Failed test case: {}" ,
466
+ case. test_name
467
+ ) ;
468
+ }
469
+ None => {
470
+ assert ! (
471
+ !chunk. spans[ 0 ] . meta. contains_key( "_dd.span_links" ) ,
472
+ "Failed test case: {}" ,
473
+ case. test_name
474
+ ) ;
475
+ }
476
+ }
477
+ }
478
+ }
345
479
}
0 commit comments