@@ -434,6 +434,206 @@ defmodule Sentry.OpenTelemetry.SpanStorageTest do
434434 end
435435 end
436436
437+ describe "nested span hierarchies" do
438+ @ tag span_storage: true
439+ test "retrieves grand-children spans correctly" , % { table_name: table_name } do
440+ root_span = % SpanRecord {
441+ span_id: "root123" ,
442+ parent_span_id: nil ,
443+ trace_id: "trace123" ,
444+ name: "root_span" ,
445+ start_time: 1000 ,
446+ end_time: 2000
447+ }
448+
449+ child1_span = % SpanRecord {
450+ span_id: "child1" ,
451+ parent_span_id: "root123" ,
452+ trace_id: "trace123" ,
453+ name: "child_span_1" ,
454+ start_time: 1100 ,
455+ end_time: 1900
456+ }
457+
458+ child2_span = % SpanRecord {
459+ span_id: "child2" ,
460+ parent_span_id: "root123" ,
461+ trace_id: "trace123" ,
462+ name: "child_span_2" ,
463+ start_time: 1200 ,
464+ end_time: 1800
465+ }
466+
467+ grandchild1_span = % SpanRecord {
468+ span_id: "grandchild1" ,
469+ parent_span_id: "child1" ,
470+ trace_id: "trace123" ,
471+ name: "grandchild_span_1" ,
472+ start_time: 1150 ,
473+ end_time: 1250
474+ }
475+
476+ grandchild2_span = % SpanRecord {
477+ span_id: "grandchild2" ,
478+ parent_span_id: "child1" ,
479+ trace_id: "trace123" ,
480+ name: "grandchild_span_2" ,
481+ start_time: 1300 ,
482+ end_time: 1400
483+ }
484+
485+ grandchild3_span = % SpanRecord {
486+ span_id: "grandchild3" ,
487+ parent_span_id: "child2" ,
488+ trace_id: "trace123" ,
489+ name: "grandchild_span_3" ,
490+ start_time: 1250 ,
491+ end_time: 1350
492+ }
493+
494+ SpanStorage . store_span ( root_span , table_name: table_name )
495+ SpanStorage . store_span ( child1_span , table_name: table_name )
496+ SpanStorage . store_span ( child2_span , table_name: table_name )
497+ SpanStorage . store_span ( grandchild1_span , table_name: table_name )
498+ SpanStorage . store_span ( grandchild2_span , table_name: table_name )
499+ SpanStorage . store_span ( grandchild3_span , table_name: table_name )
500+
501+ all_descendants = SpanStorage . get_child_spans ( "root123" , table_name: table_name )
502+
503+ assert length ( all_descendants ) == 5
504+
505+ span_ids = Enum . map ( all_descendants , & & 1 . span_id )
506+ assert "child1" in span_ids
507+ assert "child2" in span_ids
508+ assert "grandchild1" in span_ids
509+ assert "grandchild2" in span_ids
510+ assert "grandchild3" in span_ids
511+
512+ start_times = Enum . map ( all_descendants , & & 1 . start_time )
513+ assert start_times == Enum . sort ( start_times )
514+ end
515+
516+ @ tag span_storage: true
517+ test "retrieves deep nested hierarchies correctly" , % { table_name: table_name } do
518+ spans = [
519+ % SpanRecord {
520+ span_id: "root" ,
521+ parent_span_id: nil ,
522+ trace_id: "trace123" ,
523+ name: "root_span" ,
524+ start_time: 1000 ,
525+ end_time: 2000
526+ } ,
527+ % SpanRecord {
528+ span_id: "child" ,
529+ parent_span_id: "root" ,
530+ trace_id: "trace123" ,
531+ name: "child_span" ,
532+ start_time: 1100 ,
533+ end_time: 1900
534+ } ,
535+ % SpanRecord {
536+ span_id: "grandchild" ,
537+ parent_span_id: "child" ,
538+ trace_id: "trace123" ,
539+ name: "grandchild_span" ,
540+ start_time: 1200 ,
541+ end_time: 1800
542+ } ,
543+ % SpanRecord {
544+ span_id: "great_grandchild" ,
545+ parent_span_id: "grandchild" ,
546+ trace_id: "trace123" ,
547+ name: "great_grandchild_span" ,
548+ start_time: 1300 ,
549+ end_time: 1700
550+ }
551+ ]
552+
553+ Enum . each ( spans , & SpanStorage . store_span ( & 1 , table_name: table_name ) )
554+
555+ all_descendants = SpanStorage . get_child_spans ( "root" , table_name: table_name )
556+ assert length ( all_descendants ) == 3
557+
558+ span_ids = Enum . map ( all_descendants , & & 1 . span_id )
559+ assert "child" in span_ids
560+ assert "grandchild" in span_ids
561+ assert "great_grandchild" in span_ids
562+
563+ child_descendants = SpanStorage . get_child_spans ( "child" , table_name: table_name )
564+ assert length ( child_descendants ) == 2
565+
566+ child_span_ids = Enum . map ( child_descendants , & & 1 . span_id )
567+ assert "grandchild" in child_span_ids
568+ assert "great_grandchild" in child_span_ids
569+
570+ grandchild_descendants = SpanStorage . get_child_spans ( "grandchild" , table_name: table_name )
571+ assert length ( grandchild_descendants ) == 1
572+ assert hd ( grandchild_descendants ) . span_id == "great_grandchild"
573+ end
574+
575+ @ tag span_storage: true
576+ test "handles multiple disconnected subtrees correctly" , % { table_name: table_name } do
577+ spans = [
578+ % SpanRecord {
579+ span_id: "branch1" ,
580+ parent_span_id: "root" ,
581+ trace_id: "trace123" ,
582+ name: "branch1_span" ,
583+ start_time: 1100 ,
584+ end_time: 1500
585+ } ,
586+ % SpanRecord {
587+ span_id: "leaf1" ,
588+ parent_span_id: "branch1" ,
589+ trace_id: "trace123" ,
590+ name: "leaf1_span" ,
591+ start_time: 1150 ,
592+ end_time: 1250
593+ } ,
594+ % SpanRecord {
595+ span_id: "leaf2" ,
596+ parent_span_id: "branch1" ,
597+ trace_id: "trace123" ,
598+ name: "leaf2_span" ,
599+ start_time: 1300 ,
600+ end_time: 1400
601+ } ,
602+ % SpanRecord {
603+ span_id: "branch2" ,
604+ parent_span_id: "root" ,
605+ trace_id: "trace123" ,
606+ name: "branch2_span" ,
607+ start_time: 1600 ,
608+ end_time: 1900
609+ } ,
610+ % SpanRecord {
611+ span_id: "leaf3" ,
612+ parent_span_id: "branch2" ,
613+ trace_id: "trace123" ,
614+ name: "leaf3_span" ,
615+ start_time: 1650 ,
616+ end_time: 1750
617+ }
618+ ]
619+
620+ Enum . each ( spans , & SpanStorage . store_span ( & 1 , table_name: table_name ) )
621+
622+ all_descendants = SpanStorage . get_child_spans ( "root" , table_name: table_name )
623+ assert length ( all_descendants ) == 5
624+
625+ span_ids = Enum . map ( all_descendants , & & 1 . span_id )
626+ assert "branch1" in span_ids
627+ assert "branch2" in span_ids
628+ assert "leaf1" in span_ids
629+ assert "leaf2" in span_ids
630+ assert "leaf3" in span_ids
631+
632+ start_times = Enum . map ( all_descendants , & & 1 . start_time )
633+ assert start_times == [ 1100 , 1150 , 1300 , 1600 , 1650 ]
634+ end
635+ end
636+
437637 describe "cleanup" do
438638 @ tag span_storage: [ cleanup_interval: 100 ]
439639 test "cleanup respects span TTL precisely" , % { table_name: table_name } do
0 commit comments