@@ -20,7 +20,7 @@ import {
20
20
suppressTracing ,
21
21
withScope ,
22
22
} from '@sentry/core' ;
23
- import type { Event , Scope } from '@sentry/core' ;
23
+ import type { Event , Scope , SpanLink } from '@sentry/core' ;
24
24
25
25
import { SEMATTRS_HTTP_METHOD } from '@opentelemetry/semantic-conventions' ;
26
26
import { continueTrace , startInactiveSpan , startSpan , startSpanManual } from '../src/trace' ;
@@ -356,6 +356,40 @@ describe('trace', () => {
356
356
} ) ;
357
357
} ) ;
358
358
359
+ it ( 'allows to pass span links' , ( ) => {
360
+ const rawSpan1 = startInactiveSpan ( { name : 'pageload_span' } ) ;
361
+
362
+ // @ts -expect-error links exists on span
363
+ expect ( rawSpan1 ?. links ) . toEqual ( [ ] ) ;
364
+
365
+ const span1JSON = spanToJSON ( rawSpan1 ) ;
366
+
367
+ startSpan ( { name : '/users/:id' } , rawSpan2 => {
368
+ rawSpan2 . addLink ( {
369
+ context : rawSpan1 . spanContext ( ) ,
370
+ attributes : {
371
+ 'sentry.link.type' : 'previous_trace' ,
372
+ } ,
373
+ } ) ;
374
+
375
+ const span2LinkJSON = spanToJSON ( rawSpan2 ) . links ?. [ 0 ] ;
376
+
377
+ expect ( span2LinkJSON ?. attributes ?. [ 'sentry.link.type' ] ) . toBe ( 'previous_trace' ) ;
378
+
379
+ // @ts -expect-error links and _spanContext exist on span
380
+ expect ( ( rawSpan2 ?. links as SpanLink [ ] ) ?. [ 0 ] . context . traceId ) . toEqual ( rawSpan1 . _spanContext . traceId ) ;
381
+ // @ts -expect-error links and _spanContext exist on span
382
+ expect ( ( rawSpan2 ?. links as SpanLink [ ] ) ?. [ 0 ] . context . traceId ) . toEqual ( span1JSON . trace_id ) ;
383
+ expect ( span2LinkJSON ?. trace_id ) . toBe ( span1JSON . trace_id ) ;
384
+
385
+ // @ts -expect-error links and _spanContext exist on span
386
+ expect ( ( rawSpan2 ?. links as SpanLink [ ] ) ?. [ 0 ] . context . spanId ) . toEqual ( rawSpan1 ?. _spanContext . spanId ) ;
387
+ // @ts -expect-error links and _spanContext exist on span
388
+ expect ( ( rawSpan2 ?. links as SpanLink [ ] ) ?. [ 0 ] . context . spanId ) . toEqual ( span1JSON . span_id ) ;
389
+ expect ( span2LinkJSON ?. span_id ) . toBe ( span1JSON . span_id ) ;
390
+ } ) ;
391
+ } ) ;
392
+
359
393
it ( 'allows to force a transaction with forceTransaction=true' , async ( ) => {
360
394
const client = getClient ( ) ! ;
361
395
const transactionEvents : Event [ ] = [ ] ;
@@ -906,6 +940,40 @@ describe('trace', () => {
906
940
} ) ;
907
941
} ) ;
908
942
943
+ it ( 'allows to pass span links' , ( ) => {
944
+ const rawSpan1 = startInactiveSpan ( { name : 'pageload_span' } ) ;
945
+
946
+ // @ts -expect-error links exists on span
947
+ expect ( rawSpan1 ?. links ) . toEqual ( [ ] ) ;
948
+
949
+ const span1JSON = spanToJSON ( rawSpan1 ) ;
950
+
951
+ startSpanManual ( { name : '/users/:id' } , rawSpan2 => {
952
+ rawSpan2 . addLink ( {
953
+ context : rawSpan1 . spanContext ( ) ,
954
+ attributes : {
955
+ 'sentry.link.type' : 'previous_trace' ,
956
+ } ,
957
+ } ) ;
958
+
959
+ const span2LinkJSON = spanToJSON ( rawSpan2 ) . links ?. [ 0 ] ;
960
+
961
+ expect ( span2LinkJSON ?. attributes ?. [ 'sentry.link.type' ] ) . toBe ( 'previous_trace' ) ;
962
+
963
+ // @ts -expect-error links and _spanContext exist on span
964
+ expect ( ( rawSpan2 ?. links as SpanLink [ ] ) ?. [ 0 ] . context . traceId ) . toEqual ( rawSpan1 . _spanContext . traceId ) ;
965
+ // @ts -expect-error links and _spanContext exist on span
966
+ expect ( ( rawSpan2 ?. links as SpanLink [ ] ) ?. [ 0 ] . context . traceId ) . toEqual ( span1JSON . trace_id ) ;
967
+ expect ( span2LinkJSON ?. trace_id ) . toBe ( span1JSON . trace_id ) ;
968
+
969
+ // @ts -expect-error links and _spanContext exist on span
970
+ expect ( ( rawSpan2 ?. links as SpanLink [ ] ) ?. [ 0 ] . context . spanId ) . toEqual ( rawSpan1 ?. _spanContext . spanId ) ;
971
+ // @ts -expect-error links and _spanContext exist on span
972
+ expect ( ( rawSpan2 ?. links as SpanLink [ ] ) ?. [ 0 ] . context . spanId ) . toEqual ( span1JSON . span_id ) ;
973
+ expect ( span2LinkJSON ?. span_id ) . toBe ( span1JSON . span_id ) ;
974
+ } ) ;
975
+ } ) ;
976
+
909
977
it ( 'allows to force a transaction with forceTransaction=true' , async ( ) => {
910
978
const client = getClient ( ) ! ;
911
979
const transactionEvents : Event [ ] = [ ] ;
0 commit comments