1- import type { Client , DynamicSamplingContext , Scope , Span } from '@sentry/types' ;
1+ import type { Client , DynamicSamplingContext , Scope , Span , Transaction } from '@sentry/types' ;
22import { dropUndefinedKeys } from '@sentry/utils' ;
33
44import { DEFAULT_ENVIRONMENT } from '../constants' ;
@@ -38,7 +38,7 @@ export function getDynamicSamplingContextFromClient(
3838/**
3939 * A Span with a frozen dynamic sampling context.
4040 */
41- type TransactionWithV7FrozenDsc = Span & { _frozenDynamicSamplingContext ?: DynamicSamplingContext } ;
41+ type TransactionWithV7FrozenDsc = Transaction & { _frozenDynamicSamplingContext ?: DynamicSamplingContext } ;
4242
4343/**
4444 * Creates a dynamic sampling context from a span (and client and scope)
@@ -48,29 +48,30 @@ type TransactionWithV7FrozenDsc = Span & { _frozenDynamicSamplingContext?: Dynam
4848 * @returns a dynamic sampling context
4949 */
5050export function getDynamicSamplingContextFromSpan ( span : Span ) : Readonly < Partial < DynamicSamplingContext > > {
51- // As long as we use `Transaction`s internally, this should be fine.
52- // TODO: We need to replace this with a `getRootSpan(span)` function though
53- const txn = span . transaction ;
54-
55- // TODO (v8): Remove v7FrozenDsc as a Transaction will no longer have _frozenDynamicSamplingContext
56- // For now we need to avoid breaking users who directly created a txn with a DSC, where this field is still set.
57- // @see Transaction class constructor
58- const v7FrozenDsc = ( txn as TransactionWithV7FrozenDsc ) . _frozenDynamicSamplingContext ;
59- if ( v7FrozenDsc ) {
60- return v7FrozenDsc ;
61- }
62-
6351 const client = getClient ( ) ;
6452 if ( ! client ) {
6553 return { } ;
6654 }
6755
6856 // passing emit=false here to only emit later once the DSC is actually populated
6957 const dsc = getDynamicSamplingContextFromClient ( span . traceId , client , getCurrentScope ( ) , false ) ;
58+
59+ const txn = span . transaction as TransactionWithV7FrozenDsc | undefined ;
7060 if ( ! txn ) {
7161 return dsc ;
7262 }
7363
64+ // As long as we use `Transaction`s internally, this should be fine.
65+ // TODO: We need to replace this with a `getRootSpan(span)` function though
66+
67+ // TODO (v8): Remove v7FrozenDsc as a Transaction will no longer have _frozenDynamicSamplingContext
68+ // For now we need to avoid breaking users who directly created a txn with a DSC, where this field is still set.
69+ // @see Transaction class constructor
70+ const v7FrozenDsc = txn && txn . _frozenDynamicSamplingContext ;
71+ if ( v7FrozenDsc ) {
72+ return v7FrozenDsc ;
73+ }
74+
7475 const maybeSampleRate = txn . metadata . sampleRate ;
7576 if ( maybeSampleRate !== undefined ) {
7677 dsc . sample_rate = `${ maybeSampleRate } ` ;
0 commit comments