@@ -25,7 +25,7 @@ import {
25
25
import { SentryNonRecordingSpan } from '../../../src/tracing/sentryNonRecordingSpan' ;
26
26
import { startNewTrace } from '../../../src/tracing/trace' ;
27
27
import type { Event , Span , StartSpanOptions } from '../../../src/types-hoist' ;
28
- import { _setSpanForScope } from '../../../src/utils/spanOnScope' ;
28
+ import { _getSpanForScope , _setSpanForScope } from '../../../src/utils/spanOnScope' ;
29
29
import { getActiveSpan , getRootSpan , getSpanDescendants , spanIsSampled } from '../../../src/utils/spanUtils' ;
30
30
import { TestClient , getDefaultTestClientOptions } from '../../mocks/client' ;
31
31
@@ -251,20 +251,44 @@ describe('startSpan', () => {
251
251
expect ( getActiveSpan ( ) ) . toBe ( undefined ) ;
252
252
} ) ;
253
253
254
- it ( 'allows to pass a scope' , ( ) => {
254
+ it ( 'starts the span on the fork of a passed custom scope' , ( ) => {
255
255
const initialScope = getCurrentScope ( ) ;
256
256
257
- const manualScope = initialScope . clone ( ) ;
257
+ const customScope = initialScope . clone ( ) ;
258
+ customScope . setTag ( 'dogs' , 'great' ) ;
259
+
258
260
const parentSpan = new SentrySpan ( { spanId : 'parent-span-id' , sampled : true } ) ;
259
- _setSpanForScope ( manualScope , parentSpan ) ;
261
+ _setSpanForScope ( customScope , parentSpan ) ;
260
262
261
- startSpan ( { name : 'GET users/[id]' , scope : manualScope } , span => {
263
+ startSpan ( { name : 'GET users/[id]' , scope : customScope } , span => {
264
+ // current scope is forked from the customScope
262
265
expect ( getCurrentScope ( ) ) . not . toBe ( initialScope ) ;
263
- expect ( getCurrentScope ( ) ) . toBe ( manualScope ) ;
266
+ expect ( getCurrentScope ( ) ) . not . toBe ( customScope ) ;
267
+ expect ( getCurrentScope ( ) . getScopeData ( ) . tags ) . toEqual ( { dogs : 'great' } ) ;
268
+
269
+ // active span is set correctly
264
270
expect ( getActiveSpan ( ) ) . toBe ( span ) ;
271
+
272
+ // span has the correct parent span
265
273
expect ( spanToJSON ( span ) . parent_span_id ) . toBe ( 'parent-span-id' ) ;
274
+
275
+ // scope data modifications
276
+ getCurrentScope ( ) . setTag ( 'cats' , 'great' ) ;
277
+ customScope . setTag ( 'bears' , 'great' ) ;
278
+
279
+ expect ( getCurrentScope ( ) . getScopeData ( ) . tags ) . toEqual ( { dogs : 'great' , cats : 'great' } ) ;
280
+ expect ( customScope . getScopeData ( ) . tags ) . toEqual ( { dogs : 'great' , bears : 'great' } ) ;
266
281
} ) ;
267
282
283
+ // customScope modifications are persisted
284
+ expect ( customScope . getScopeData ( ) . tags ) . toEqual ( { dogs : 'great' , bears : 'great' } ) ;
285
+
286
+ // span is parent span again on customScope
287
+ withScope ( customScope , ( ) => {
288
+ expect ( getActiveSpan ( ) ) . toBe ( parentSpan ) ;
289
+ } ) ;
290
+
291
+ // but activeSpan and currentScope are reset, since customScope was never active
268
292
expect ( getCurrentScope ( ) ) . toBe ( initialScope ) ;
269
293
expect ( getActiveSpan ( ) ) . toBe ( undefined ) ;
270
294
} ) ;
@@ -273,29 +297,35 @@ describe('startSpan', () => {
273
297
it ( 'with parent span' , ( ) => {
274
298
const initialScope = getCurrentScope ( ) ;
275
299
276
- const manualScope = initialScope . clone ( ) ;
300
+ const customScope = initialScope . clone ( ) ;
277
301
const parentSpan = new SentrySpan ( { spanId : 'parent-span-id' , sampled : true } ) ;
278
- _setSpanForScope ( manualScope , parentSpan ) ;
302
+ _setSpanForScope ( customScope , parentSpan ) ;
279
303
280
- startSpan ( { name : 'span 1' , scope : manualScope } , span1 => {
304
+ startSpan ( { name : 'span 1' , scope : customScope } , span1 => {
305
+ // current scope is forked from the customScope
281
306
expect ( getCurrentScope ( ) ) . not . toBe ( initialScope ) ;
282
- expect ( getCurrentScope ( ) ) . toBe ( manualScope ) ;
307
+ expect ( getCurrentScope ( ) ) . not . toBe ( customScope ) ;
308
+
283
309
expect ( getActiveSpan ( ) ) . toBe ( span1 ) ;
284
310
expect ( spanToJSON ( span1 ) . parent_span_id ) . toBe ( 'parent-span-id' ) ;
285
311
} ) ;
286
312
287
- withScope ( manualScope , ( ) => {
313
+ // active span on customScope is reset
314
+ withScope ( customScope , ( ) => {
288
315
expect ( getActiveSpan ( ) ) . toBe ( parentSpan ) ;
289
316
} ) ;
290
317
291
- startSpan ( { name : 'span 2' , scope : manualScope } , span2 => {
318
+ startSpan ( { name : 'span 2' , scope : customScope } , span2 => {
319
+ // current scope is forked from the customScope
292
320
expect ( getCurrentScope ( ) ) . not . toBe ( initialScope ) ;
293
- expect ( getCurrentScope ( ) ) . toBe ( manualScope ) ;
321
+ expect ( getCurrentScope ( ) ) . not . toBe ( customScope ) ;
322
+
294
323
expect ( getActiveSpan ( ) ) . toBe ( span2 ) ;
324
+ // both, span1 and span2 are children of the parent span
295
325
expect ( spanToJSON ( span2 ) . parent_span_id ) . toBe ( 'parent-span-id' ) ;
296
326
} ) ;
297
327
298
- withScope ( manualScope , ( ) => {
328
+ withScope ( customScope , ( ) => {
299
329
expect ( getActiveSpan ( ) ) . toBe ( parentSpan ) ;
300
330
} ) ;
301
331
@@ -305,31 +335,35 @@ describe('startSpan', () => {
305
335
306
336
it ( 'without parent span' , ( ) => {
307
337
const initialScope = getCurrentScope ( ) ;
308
- const manualScope = initialScope . clone ( ) ;
338
+ const customScope = initialScope . clone ( ) ;
309
339
310
- const traceId = manualScope . getPropagationContext ( ) ?. traceId ;
340
+ const traceId = customScope . getPropagationContext ( ) ?. traceId ;
311
341
312
- startSpan ( { name : 'span 1' , scope : manualScope } , span1 => {
342
+ startSpan ( { name : 'span 1' , scope : customScope } , span1 => {
313
343
expect ( getCurrentScope ( ) ) . not . toBe ( initialScope ) ;
314
- expect ( getCurrentScope ( ) ) . toBe ( manualScope ) ;
344
+ expect ( getCurrentScope ( ) ) . not . toBe ( customScope ) ;
345
+
315
346
expect ( getActiveSpan ( ) ) . toBe ( span1 ) ;
316
- expect ( spanToJSON ( span1 ) . parent_span_id ) . toBe ( undefined ) ;
347
+ expect ( getRootSpan ( getActiveSpan ( ) ! ) ) . toBe ( span1 ) ;
348
+
317
349
expect ( span1 . spanContext ( ) . traceId ) . toBe ( traceId ) ;
318
350
} ) ;
319
351
320
- withScope ( manualScope , ( ) => {
352
+ withScope ( customScope , ( ) => {
321
353
expect ( getActiveSpan ( ) ) . toBe ( undefined ) ;
322
354
} ) ;
323
355
324
- startSpan ( { name : 'span 2' , scope : manualScope } , span2 => {
356
+ startSpan ( { name : 'span 2' , scope : customScope } , span2 => {
325
357
expect ( getCurrentScope ( ) ) . not . toBe ( initialScope ) ;
326
- expect ( getCurrentScope ( ) ) . toBe ( manualScope ) ;
358
+ expect ( getCurrentScope ( ) ) . not . toBe ( customScope ) ;
359
+
327
360
expect ( getActiveSpan ( ) ) . toBe ( span2 ) ;
328
- expect ( spanToJSON ( span2 ) . parent_span_id ) . toBe ( undefined ) ;
361
+ expect ( getRootSpan ( getActiveSpan ( ) ! ) ) . toBe ( span2 ) ;
362
+
329
363
expect ( span2 . spanContext ( ) . traceId ) . toBe ( traceId ) ;
330
364
} ) ;
331
365
332
- withScope ( manualScope , ( ) => {
366
+ withScope ( customScope , ( ) => {
333
367
expect ( getActiveSpan ( ) ) . toBe ( undefined ) ;
334
368
} ) ;
335
369
0 commit comments