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