@@ -21,7 +21,7 @@ import type {
21
21
} from '../types-hoist' ;
22
22
import type { SpanLink , SpanLinkJSON } from '../types-hoist/link' ;
23
23
import { consoleSandbox } from '../utils-hoist/logger' ;
24
- import { addNonEnumerableProperty , dropUndefinedKeys } from '../utils-hoist/object' ;
24
+ import { dropUndefinedKeys } from '../utils-hoist/object' ;
25
25
import { generateSpanId } from '../utils-hoist/propagationContext' ;
26
26
import { timestampInSeconds } from '../utils-hoist/time' ;
27
27
import { generateSentryTraceHeader } from '../utils-hoist/tracing' ;
@@ -223,55 +223,54 @@ export function getStatusMessage(status: SpanStatus | undefined): string | undef
223
223
return status . message || 'unknown_error' ;
224
224
}
225
225
226
- const CHILD_SPANS_FIELD = '_sentryChildSpans' ;
227
- const ROOT_SPAN_FIELD = '_sentryRootSpan' ;
228
-
229
- type SpanWithPotentialChildren = Span & {
230
- [ CHILD_SPANS_FIELD ] ?: Set < Span > ;
231
- [ ROOT_SPAN_FIELD ] ?: Span ;
232
- } ;
226
+ const SPAN_TO_ROOT_SPAN_MAP = new WeakMap < Span , Span > ( ) ;
227
+ const SPAN_TO_CHILD_SPANS_MAP = new WeakMap < Span , Set < Span > > ( ) ;
233
228
234
229
/**
235
230
* Adds an opaque child span reference to a span.
236
231
*/
237
- export function addChildSpanToSpan ( span : SpanWithPotentialChildren , childSpan : Span ) : void {
232
+ export function addChildSpanToSpan ( span : Span , childSpan : Span ) : void {
238
233
// We store the root span reference on the child span
239
234
// We need this for `getRootSpan()` to work
240
- const rootSpan = span [ ROOT_SPAN_FIELD ] || span ;
241
- addNonEnumerableProperty ( childSpan as SpanWithPotentialChildren , ROOT_SPAN_FIELD , rootSpan ) ;
235
+ const rootSpan = SPAN_TO_ROOT_SPAN_MAP . get ( span ) || span ;
236
+ SPAN_TO_ROOT_SPAN_MAP . set ( childSpan , rootSpan ) ;
242
237
243
238
// We store a list of child spans on the parent span
244
239
// We need this for `getSpanDescendants()` to work
245
- if ( span [ CHILD_SPANS_FIELD ] ) {
246
- span [ CHILD_SPANS_FIELD ] . add ( childSpan ) ;
240
+ const childSpans = SPAN_TO_CHILD_SPANS_MAP . get ( span ) ;
241
+ if ( childSpans ) {
242
+ childSpans . add ( childSpan ) ;
247
243
} else {
248
- addNonEnumerableProperty ( span , CHILD_SPANS_FIELD , new Set ( [ childSpan ] ) ) ;
244
+ SPAN_TO_CHILD_SPANS_MAP . set ( span , new Set ( [ childSpan ] ) ) ;
249
245
}
250
246
}
251
247
252
248
/** This is only used internally by Idle Spans. */
253
- export function removeChildSpanFromSpan ( span : SpanWithPotentialChildren , childSpan : Span ) : void {
254
- if ( span [ CHILD_SPANS_FIELD ] ) {
255
- span [ CHILD_SPANS_FIELD ] . delete ( childSpan ) ;
249
+ export function removeChildSpanFromSpan ( span : Span , childSpan : Span ) : void {
250
+ const childSpans = SPAN_TO_CHILD_SPANS_MAP . get ( span ) ;
251
+ if ( childSpans ) {
252
+ childSpans . delete ( childSpan ) ;
256
253
}
257
254
}
258
255
259
256
/**
260
257
* Returns an array of the given span and all of its descendants.
261
258
*/
262
- export function getSpanDescendants ( span : SpanWithPotentialChildren ) : Span [ ] {
259
+ export function getSpanDescendants ( span : Span ) : Span [ ] {
263
260
const resultSet = new Set < Span > ( ) ;
264
261
265
- function addSpanChildren ( span : SpanWithPotentialChildren ) : void {
262
+ function addSpanChildren ( span : Span ) : void {
266
263
// This exit condition is required to not infinitely loop in case of a circular dependency.
267
264
if ( resultSet . has ( span ) ) {
268
265
return ;
269
266
// We want to ignore unsampled spans (e.g. non recording spans)
270
267
} else if ( spanIsSampled ( span ) ) {
271
268
resultSet . add ( span ) ;
272
- const childSpans = span [ CHILD_SPANS_FIELD ] ? Array . from ( span [ CHILD_SPANS_FIELD ] ) : [ ] ;
273
- for ( const childSpan of childSpans ) {
274
- addSpanChildren ( childSpan ) ;
269
+ const childSpans = SPAN_TO_CHILD_SPANS_MAP . get ( span ) ;
270
+ if ( childSpans ) {
271
+ for ( const childSpan of childSpans ) {
272
+ addSpanChildren ( childSpan ) ;
273
+ }
275
274
}
276
275
}
277
276
}
@@ -284,8 +283,8 @@ export function getSpanDescendants(span: SpanWithPotentialChildren): Span[] {
284
283
/**
285
284
* Returns the root span of a given span.
286
285
*/
287
- export function getRootSpan ( span : SpanWithPotentialChildren ) : Span {
288
- return span [ ROOT_SPAN_FIELD ] || span ;
286
+ export function getRootSpan ( span : Span ) : Span {
287
+ return SPAN_TO_ROOT_SPAN_MAP . get ( span ) || span ;
289
288
}
290
289
291
290
/**
0 commit comments