diff --git a/packages/core/src/baseclient.ts b/packages/core/src/baseclient.ts index c7a26f45ab70..777e0dc008c8 100644 --- a/packages/core/src/baseclient.ts +++ b/packages/core/src/baseclient.ts @@ -430,6 +430,9 @@ export abstract class BaseClient implements Client { /** @inheritdoc */ public on(hook: 'createDsc', callback: (dsc: DynamicSamplingContext, rootSpan?: Span) => void): () => void; + /** @inheritdoc */ + public on(hook: 'useFrozenDsc', callback: (dsc: Partial, rootSpan: Span) => void): () => void; + /** @inheritdoc */ public on( hook: 'beforeSendFeedback', @@ -527,6 +530,9 @@ export abstract class BaseClient implements Client { /** @inheritdoc */ public emit(hook: 'createDsc', dsc: DynamicSamplingContext, rootSpan?: Span): void; + /** @inheritdoc */ + public emit(hook: 'useFrozenDsc', dsc: Partial, rootSpan: Span): void; + /** @inheritdoc */ public emit(hook: 'beforeSendFeedback', feedback: FeedbackEvent, options?: { includeReplay: boolean }): void; diff --git a/packages/core/src/tracing/dynamicSamplingContext.ts b/packages/core/src/tracing/dynamicSamplingContext.ts index d96dd726d51f..0a19fe6910b1 100644 --- a/packages/core/src/tracing/dynamicSamplingContext.ts +++ b/packages/core/src/tracing/dynamicSamplingContext.ts @@ -72,6 +72,7 @@ export function getDynamicSamplingContextFromSpan(span: Span): Readonly) => { + if (!dsc.replay_id) { + return; + } + + const replayId = replay.getSessionId(); + if ( + !replayId || + !replay.isEnabled() || + // We do not want to set the DSC when in buffer mode, as that means the replay has not been sent (yet) + replay.recordingMode !== 'session' || + // This returns false if the session has expired in the meanwhile + !replay.checkAndHandleExpiredSession() + ) { + delete dsc.replay_id; + } + }); + client.on('spanStart', span => { replay.lastActiveSpan = span; }); diff --git a/packages/types/src/client.ts b/packages/types/src/client.ts index 94f28f9157aa..8621a75d4570 100644 --- a/packages/types/src/client.ts +++ b/packages/types/src/client.ts @@ -262,6 +262,12 @@ export interface Client { */ on(hook: 'createDsc', callback: (dsc: DynamicSamplingContext, rootSpan?: Span) => void): () => void; + /** + * Register a callback to be used whenever a frozen DSC is used. + * @returns A function that, when executed, removes the registered callback. + */ + on(hook: 'useFrozenDsc', callback: (dsc: Partial, rootSpan: Span) => void): () => void; + /** * Register a callback when a Feedback event has been prepared. * This should be used to mutate the event. The options argument can hint @@ -366,6 +372,12 @@ export interface Client { */ emit(hook: 'createDsc', dsc: DynamicSamplingContext, rootSpan?: Span): void; + /** + * Fire a hook for when a frozen DSC (Dynamic Sampling Context) from a span is used. + * Expects the DSC as second argument, and the root span as third. + */ + emit(hook: 'useFrozenDsc', dsc: Partial, rootSpan: Span): void; + /** * Fire a hook event for after preparing a feedback event. Events to be given * a feedback event as the second argument, and an optional options object as