@@ -780,39 +780,57 @@ describe('startSpanManual', () => {
780
780
expect ( getActiveSpan ( ) ) . toBe ( undefined ) ;
781
781
} ) ;
782
782
783
- describe ( 'allows to pass a scope' , ( ) => {
783
+ describe ( 'starts a span on the fork of a custom scope if passed ' , ( ) => {
784
784
it ( 'with parent span' , ( ) => {
785
785
const initialScope = getCurrentScope ( ) ;
786
786
787
- const manualScope = initialScope . clone ( ) ;
788
- const parentSpan = new SentrySpan ( { spanId : 'parent-span-id' , sampled : true } ) ;
789
- _setSpanForScope ( manualScope , parentSpan ) ;
787
+ const customScope = initialScope . clone ( ) ;
788
+ customScope . setTag ( 'dogs' , 'great' ) ;
790
789
791
- let span1 : Span | undefined ;
790
+ const parentSpan = new SentrySpan ( { spanId : 'parent-span-id' , sampled : true } ) ;
791
+ _setSpanForScope ( customScope , parentSpan ) ;
792
792
793
- startSpanManual ( { name : 'GET users/[id]' , scope : manualScope } , span => {
794
- span1 = span ;
793
+ startSpanManual ( { name : 'GET users/[id]' , scope : customScope } , span => {
794
+ // current scope is forked from the customScope
795
795
expect ( getCurrentScope ( ) ) . not . toBe ( initialScope ) ;
796
- expect ( getCurrentScope ( ) ) . toBe ( manualScope ) ;
797
- expect ( getActiveSpan ( ) ) . toBe ( span ) ;
796
+ expect ( getCurrentScope ( ) ) . not . toBe ( customScope ) ;
798
797
expect ( spanToJSON ( span ) . parent_span_id ) . toBe ( 'parent-span-id' ) ;
799
798
799
+ // span is active span
800
+ expect ( getActiveSpan ( ) ) . toBe ( span ) ;
801
+
800
802
span . end ( ) ;
801
803
802
- // Is still the active span
804
+ // span is still the active span (weird but it is what it is)
803
805
expect ( getActiveSpan ( ) ) . toBe ( span ) ;
806
+
807
+ getCurrentScope ( ) . setTag ( 'cats' , 'great' ) ;
808
+ customScope . setTag ( 'bears' , 'great' ) ;
809
+
810
+ expect ( getCurrentScope ( ) . getScopeData ( ) . tags ) . toEqual ( { dogs : 'great' , cats : 'great' } ) ;
811
+ expect ( customScope . getScopeData ( ) . tags ) . toEqual ( { dogs : 'great' , bears : 'great' } ) ;
804
812
} ) ;
805
813
806
- startSpanManual ( { name : 'POST users/[id]' , scope : manualScope } , ( span , finish ) => {
814
+ expect ( getCurrentScope ( ) ) . toBe ( initialScope ) ;
815
+ expect ( getActiveSpan ( ) ) . toBe ( undefined ) ;
816
+
817
+ startSpanManual ( { name : 'POST users/[id]' , scope : customScope } , ( span , finish ) => {
818
+ // current scope is forked from the customScope
807
819
expect ( getCurrentScope ( ) ) . not . toBe ( initialScope ) ;
808
- expect ( getCurrentScope ( ) ) . toBe ( manualScope ) ;
820
+ expect ( getCurrentScope ( ) ) . not . toBe ( customScope ) ;
821
+ expect ( spanToJSON ( span ) . parent_span_id ) . toBe ( 'parent-span-id' ) ;
822
+
823
+ // scope data modification from customScope in previous callback is persisted
824
+ expect ( getCurrentScope ( ) . getScopeData ( ) . tags ) . toEqual ( { dogs : 'great' , bears : 'great' } ) ;
825
+
826
+ // span is active span
809
827
expect ( getActiveSpan ( ) ) . toBe ( span ) ;
810
- expect ( spanToJSON ( span ) . parent_span_id ) . toBe ( span1 ?. spanContext ( ) . spanId ) ;
811
828
829
+ // calling finish() or span.end() has the same effect
812
830
finish ( ) ;
813
831
814
832
// using finish() resets the scope correctly
815
- expect ( getActiveSpan ( ) ) . toBe ( span1 ) ;
833
+ expect ( getActiveSpan ( ) ) . toBe ( span ) ;
816
834
} ) ;
817
835
818
836
expect ( getCurrentScope ( ) ) . toBe ( initialScope ) ;
@@ -824,27 +842,33 @@ describe('startSpanManual', () => {
824
842
const manualScope = initialScope . clone ( ) ;
825
843
826
844
startSpanManual ( { name : 'GET users/[id]' , scope : manualScope } , ( span , finish ) => {
845
+ // current scope is forked from the customScope
827
846
expect ( getCurrentScope ( ) ) . not . toBe ( initialScope ) ;
828
- expect ( getCurrentScope ( ) ) . toBe ( manualScope ) ;
847
+ expect ( getCurrentScope ( ) ) . not . toBe ( manualScope ) ;
848
+ expect ( getCurrentScope ( ) ) . toEqual ( manualScope ) ;
849
+
850
+ // span is active span and a root span
829
851
expect ( getActiveSpan ( ) ) . toBe ( span ) ;
830
- expect ( spanToJSON ( span ) . parent_span_id ) . toBe ( undefined ) ;
852
+ expect ( getRootSpan ( span ) ) . toBe ( span ) ;
831
853
832
- finish ( ) ;
854
+ span . end ( ) ;
833
855
834
- // Is still the active span
835
- expect ( getActiveSpan ( ) ) . toBe ( undefined ) ;
856
+ expect ( getActiveSpan ( ) ) . toBe ( span ) ;
836
857
} ) ;
837
858
838
- startSpanManual ( { name : 'GET users/[id]' , scope : manualScope } , ( span , finish ) => {
859
+ startSpanManual ( { name : 'POST users/[id]' , scope : manualScope } , ( span , finish ) => {
839
860
expect ( getCurrentScope ( ) ) . not . toBe ( initialScope ) ;
840
- expect ( getCurrentScope ( ) ) . toBe ( manualScope ) ;
861
+ expect ( getCurrentScope ( ) ) . not . toBe ( manualScope ) ;
862
+ expect ( getCurrentScope ( ) ) . toEqual ( manualScope ) ;
863
+
864
+ // second span is active span and its own root span
841
865
expect ( getActiveSpan ( ) ) . toBe ( span ) ;
842
- expect ( spanToJSON ( span ) . parent_span_id ) . toBe ( undefined ) ;
866
+ expect ( getRootSpan ( span ) ) . toBe ( span ) ;
843
867
844
868
finish ( ) ;
845
869
846
- // using finish() resets the scope correctly
847
- expect ( getActiveSpan ( ) ) . toBe ( undefined ) ;
870
+ // calling finish() or span.end() has the same effect
871
+ expect ( getActiveSpan ( ) ) . toBe ( span ) ;
848
872
} ) ;
849
873
850
874
expect ( getCurrentScope ( ) ) . toBe ( initialScope ) ;
0 commit comments