Skip to content

Commit 4240557

Browse files
committed
make explicit sampling decision win
1 parent bb40f48 commit 4240557

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

packages/tracing/src/hubextensions.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ function sample<T extends Transaction>(hub: Hub, transaction: T, samplingContext
6262
return transaction;
6363
}
6464

65+
// if the user has forced a sampling decision by passing a `sampled` value in their transaction context, go with that
66+
if (transaction.sampled !== undefined) {
67+
return transaction;
68+
}
69+
6570
// we would have bailed already if neither `tracesSampler` nor `tracesSampleRate` were defined, so one of these should
6671
// work; prefer the hook if so
6772
const sampleRate =

packages/tracing/test/hub.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,24 @@ describe('Hub', () => {
197197
expect(transaction.sampled).toBe(true);
198198
});
199199

200+
it('should not try to override positive sampling decision provided in transaction context', () => {
201+
// so that the decision otherwise would be false
202+
const tracesSampler = jest.fn().mockReturnValue(0);
203+
const hub = new Hub(new BrowserClient({ tracesSampler }));
204+
const transaction = hub.startTransaction({ name: 'dogpark', sampled: true });
205+
206+
expect(transaction.sampled).toBe(true);
207+
});
208+
209+
it('should not try to override negative sampling decision provided in transaction context', () => {
210+
// so that the decision otherwise would be true
211+
const tracesSampler = jest.fn().mockReturnValue(1);
212+
const hub = new Hub(new BrowserClient({ tracesSampler }));
213+
const transaction = hub.startTransaction({ name: 'dogpark', sampled: false });
214+
215+
expect(transaction.sampled).toBe(false);
216+
});
217+
200218
it('should prefer tracesSampler to tracesSampleRate', () => {
201219
// make the two options do opposite things to prove precedence
202220
const tracesSampler = jest.fn().mockReturnValue(true);

0 commit comments

Comments
 (0)