Skip to content

Commit 7b47a06

Browse files
committed
add parent's sampling decision to defaultSampleContext
1 parent fd4d48c commit 7b47a06

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

packages/tracing/src/hubextensions.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,14 @@ function sample<T extends Transaction>(hub: Hub, transaction: T, sampleContext:
100100
*
101101
* @returns The default sample context
102102
*/
103-
function getDefaultSampleContext(): SampleContext {
103+
function getDefaultSampleContext<T extends Transaction>(transaction: T): SampleContext {
104104
const defaultSampleContext: SampleContext = {};
105105

106+
// include parent's sampling decision, if there is one
107+
if (transaction.parentSpanId && transaction.sampled !== undefined) {
108+
defaultSampleContext.parentSampled = transaction.sampled;
109+
}
110+
106111
if (isNodeEnv()) {
107112
const domain = getActiveDomain();
108113

@@ -177,7 +182,7 @@ function _startTransaction(
177182
customSampleContext?: CustomSampleContext,
178183
): Transaction {
179184
const transaction = new Transaction(context, this);
180-
return sample(this, transaction, { ...getDefaultSampleContext(), ...customSampleContext });
185+
return sample(this, transaction, { ...getDefaultSampleContext(transaction), ...customSampleContext });
181186
}
182187

183188
/**
@@ -190,7 +195,7 @@ export function startIdleTransaction(
190195
onScope?: boolean,
191196
): IdleTransaction {
192197
const transaction = new IdleTransaction(context, hub, idleTimeout, onScope);
193-
return sample(hub, transaction, getDefaultSampleContext());
198+
return sample(hub, transaction, getDefaultSampleContext(transaction));
194199
}
195200

196201
/**

packages/types/src/transaction.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,9 @@ export interface SampleContext extends CustomSampleContext {
7373
* Object representing the incoming request to a node server. Passed by default when using the TracingHandler.
7474
*/
7575
request?: ExtractedNodeRequestData;
76+
77+
/**
78+
* Sampling decision from the parent transaction, if any.
79+
*/
80+
parentSampled?: boolean;
7681
}

0 commit comments

Comments
 (0)