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