Skip to content

Commit 9824fd2

Browse files
committed
include parentSampled as separate entry in transaction context
1 parent 24bfd84 commit 9824fd2

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

packages/tracing/src/hubextensions.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ function traceHeaders(this: Hub): { [key: string]: string } {
2929
}
3030

3131
/**
32-
* Uses existing sampling decision if available; falls back to user-provided static rate.
32+
* Implements sampling inheritance and falls back to user-provided static rate if no parent decision is available.
3333
*
34-
* @param existingDecision: The transaction's existing sampling decision, if any (likely inherited).
34+
* @param parentSampled: The parent transaction's sampling decision, if any.
3535
* @param givenRate: The rate to use if no parental decision is available.
3636
*
37-
* @returns The existing sampling decision (if one exists), or the provided static rate
37+
* @returns The parent's sampling decision (if one exists), or the provided static rate
3838
*/
39-
function _useExistingDecisionOrGivenRate(existingDecision: boolean | undefined, givenRate: unknown): boolean | unknown {
40-
return existingDecision !== undefined ? existingDecision : givenRate;
39+
function _inheritOrUseGivenRate(parentSampled: boolean | undefined, givenRate: unknown): boolean | unknown {
40+
return parentSampled !== undefined ? parentSampled : givenRate;
4141
}
4242

4343
/**
@@ -67,7 +67,7 @@ function sample<T extends Transaction>(hub: Hub, transaction: T, samplingContext
6767
const sampleRate =
6868
typeof options.tracesSampler === 'function'
6969
? options.tracesSampler(samplingContext)
70-
: _useExistingDecisionOrGivenRate(samplingContext.transactionContext.sampled, options.tracesSampleRate);
70+
: _inheritOrUseGivenRate(samplingContext.parentSampled, options.tracesSampleRate);
7171

7272
// Since this is coming from the user (or from a function provided by the user), who knows what we might get. (The
7373
// only valid values are booleans or numbers between 0 and 1.)
@@ -117,7 +117,9 @@ function sample<T extends Transaction>(hub: Hub, transaction: T, samplingContext
117117
* @returns The default sample context
118118
*/
119119
function getDefaultSamplingContext(transactionContext: TransactionContext): SamplingContext {
120-
const defaultSamplingContext: SamplingContext = { transactionContext };
120+
// promote parent sampling decision (if any) for easy access
121+
const { parentSampled } = transactionContext;
122+
const defaultSamplingContext: SamplingContext = { transactionContext, parentSampled };
121123

122124
if (isNodeEnv()) {
123125
const domain = getActiveDomain();

packages/tracing/test/hub.test.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,29 @@ describe('Hub', () => {
149149
const transactionContext = {
150150
name: 'dogpark',
151151
parentSpanId: '12312012',
152-
sampled: true,
152+
parentSampled: true,
153153
};
154154

155155
hub.startTransaction(transactionContext);
156156

157157
expect(tracesSampler).toHaveBeenLastCalledWith(expect.objectContaining({ transactionContext }));
158158
});
159+
160+
it("should add parent's sampling decision to default sample context", () => {
161+
const tracesSampler = jest.fn();
162+
const hub = new Hub(new BrowserClient({ tracesSampler }));
163+
const parentSamplingDecsion = false;
164+
165+
hub.startTransaction({
166+
name: 'dogpark',
167+
parentSpanId: '12312012',
168+
parentSampled: parentSamplingDecsion,
169+
});
170+
171+
expect(tracesSampler).toHaveBeenLastCalledWith(
172+
expect.objectContaining({ parentSampled: parentSamplingDecsion }),
173+
);
174+
});
159175
});
160176

161177
describe('sample()', () => {
@@ -289,7 +305,7 @@ describe('Hub', () => {
289305
const transaction = hub.startTransaction({
290306
name: 'dogpark',
291307
parentSpanId: '12312012',
292-
sampled: parentSamplingDecsion,
308+
parentSampled: parentSamplingDecsion,
293309
});
294310

295311
expect(transaction.sampled).toBe(parentSamplingDecsion);
@@ -306,7 +322,7 @@ describe('Hub', () => {
306322
const transaction = hub.startTransaction({
307323
name: 'dogpark',
308324
parentSpanId: '12312012',
309-
sampled: parentSamplingDecsion,
325+
parentSampled: parentSamplingDecsion,
310326
});
311327

312328
expect(transaction.sampled).not.toBe(parentSamplingDecsion);

packages/types/src/transaction.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ export interface TransactionContext extends SpanContext {
1313
* transaction after a given "idle time" and we don't want this "idle time" to be part of the transaction.
1414
*/
1515
trimEnd?: boolean;
16+
17+
/**
18+
* If this transaction has a parent, the parent's sampling decision
19+
*/
20+
parentSampled?: boolean;
1621
}
1722

1823
/**
@@ -68,6 +73,11 @@ export interface SamplingContext extends CustomSamplingContext {
6873
*/
6974
transactionContext: TransactionContext;
7075

76+
/**
77+
* Sampling decision from the parent transaction, if any.
78+
*/
79+
parentSampled?: boolean;
80+
7181
/**
7282
* Object representing the URL of the current page or worker script. Passed by default in a browser or service worker
7383
* context.

0 commit comments

Comments
 (0)