Skip to content

Commit 24bfd84

Browse files
committed
pass transaction context in sampling context
1 parent ce227d0 commit 24bfd84

File tree

3 files changed

+30
-34
lines changed

3 files changed

+30
-34
lines changed

packages/tracing/src/hubextensions.ts

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

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

4343
/**
@@ -52,7 +52,7 @@ function _inheritOrUseGivenRate(parentSampled: boolean | undefined, givenRate: u
5252
*
5353
* @returns The given transaction with its `sampled` value set
5454
*/
55-
function sample<T extends Transaction>(hub: Hub, transaction: T, samplingContext: SamplingContext = {}): T {
55+
function sample<T extends Transaction>(hub: Hub, transaction: T, samplingContext: SamplingContext): T {
5656
const client = hub.getClient();
5757
const options = (client && client.getOptions()) || {};
5858

@@ -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-
: _inheritOrUseGivenRate(samplingContext.parentSampled, options.tracesSampleRate);
70+
: _useExistingDecisionOrGivenRate(samplingContext.transactionContext.sampled, 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.)
@@ -116,13 +116,8 @@ function sample<T extends Transaction>(hub: Hub, transaction: T, samplingContext
116116
*
117117
* @returns The default sample context
118118
*/
119-
function getDefaultSamplingContext<T extends Transaction>(transaction: T): SamplingContext {
120-
const defaultSamplingContext: SamplingContext = {};
121-
122-
// include parent's sampling decision, if there is one
123-
if (transaction.parentSpanId && transaction.sampled !== undefined) {
124-
defaultSamplingContext.parentSampled = transaction.sampled;
125-
}
119+
function getDefaultSamplingContext(transactionContext: TransactionContext): SamplingContext {
120+
const defaultSamplingContext: SamplingContext = { transactionContext };
126121

127122
if (isNodeEnv()) {
128123
const domain = getActiveDomain();
@@ -196,24 +191,27 @@ function isValidSampleRate(rate: unknown): boolean {
196191
*/
197192
function _startTransaction(
198193
this: Hub,
199-
context: TransactionContext,
194+
transactionContext: TransactionContext,
200195
customSamplingContext?: CustomSamplingContext,
201196
): Transaction {
202-
const transaction = new Transaction(context, this);
203-
return sample(this, transaction, { ...getDefaultSamplingContext(transaction), ...customSamplingContext });
197+
const transaction = new Transaction(transactionContext, this);
198+
return sample(this, transaction, {
199+
...getDefaultSamplingContext(transactionContext),
200+
...customSamplingContext,
201+
});
204202
}
205203

206204
/**
207205
* Create new idle transaction.
208206
*/
209207
export function startIdleTransaction(
210208
hub: Hub,
211-
context: TransactionContext,
209+
transactionContext: TransactionContext,
212210
idleTimeout?: number,
213211
onScope?: boolean,
214212
): IdleTransaction {
215-
const transaction = new IdleTransaction(context, hub, idleTimeout, onScope);
216-
return sample(hub, transaction, getDefaultSamplingContext(transaction));
213+
const transaction = new IdleTransaction(transactionContext, hub, idleTimeout, onScope);
214+
return sample(hub, transaction, getDefaultSamplingContext(transactionContext));
217215
}
218216

219217
/**

packages/tracing/test/hub.test.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,20 +143,18 @@ describe('Hub', () => {
143143
expect(tracesSampler).toHaveBeenCalledWith(expect.objectContaining({ location: dogParkLocation }));
144144
});
145145

146-
it("should add parent's sampling decision to default sample context", () => {
146+
it('should add transaction context data to default sample context', () => {
147147
const tracesSampler = jest.fn();
148148
const hub = new Hub(new BrowserClient({ tracesSampler }));
149-
const parentSamplingDecsion = false;
150-
151-
hub.startTransaction({
149+
const transactionContext = {
152150
name: 'dogpark',
153151
parentSpanId: '12312012',
154-
sampled: parentSamplingDecsion,
155-
});
152+
sampled: true,
153+
};
156154

157-
expect(tracesSampler).toHaveBeenLastCalledWith(
158-
expect.objectContaining({ parentSampled: parentSamplingDecsion }),
159-
);
155+
hub.startTransaction(transactionContext);
156+
157+
expect(tracesSampler).toHaveBeenLastCalledWith(expect.objectContaining({ transactionContext }));
160158
});
161159
});
162160

packages/types/src/transaction.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ export interface CustomSamplingContext {
6363
* Adds default data to data provided by the user. See {@link Hub.startTransaction}
6464
*/
6565
export interface SamplingContext extends CustomSamplingContext {
66+
/**
67+
* Context data with which transaction being sampled was created
68+
*/
69+
transactionContext: TransactionContext;
70+
6671
/**
6772
* Object representing the URL of the current page or worker script. Passed by default in a browser or service worker
6873
* context.
@@ -73,9 +78,4 @@ export interface SamplingContext extends CustomSamplingContext {
7378
* Object representing the incoming request to a node server. Passed by default when using the TracingHandler.
7479
*/
7580
request?: ExtractedNodeRequestData;
76-
77-
/**
78-
* Sampling decision from the parent transaction, if any.
79-
*/
80-
parentSampled?: boolean;
8181
}

0 commit comments

Comments
 (0)