Skip to content

Commit feb2bb5

Browse files
authored
fix(tracing): Call hasTracingEnabled with correct options when invoking startTransaction (#4020)
1 parent f96d47b commit feb2bb5

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

packages/tracing/src/hubextensions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function traceHeaders(this: Hub): { [key: string]: string } {
4343
*/
4444
function sample<T extends Transaction>(transaction: T, options: Options, samplingContext: SamplingContext): T {
4545
// nothing to do if tracing is not enabled
46-
if (!hasTracingEnabled()) {
46+
if (!hasTracingEnabled(options)) {
4747
transaction.sampled = false;
4848
return transaction;
4949
}

packages/tracing/test/hub.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ addDOMPropertiesToGlobal(['XMLHttpRequest', 'Event', 'location', 'document']);
2626
describe('Hub', () => {
2727
afterEach(() => {
2828
jest.clearAllMocks();
29+
// Reset global carrier to the initial state
30+
const hub = new Hub();
31+
makeMain(hub);
2932
});
3033

3134
describe('getTransaction()', () => {
@@ -125,6 +128,13 @@ describe('Hub', () => {
125128
expect(transaction.sampled).toBe(true);
126129
});
127130

131+
it('should set sampled = true if tracesSampleRate is 1 (without global hub)', () => {
132+
const hub = new Hub(new BrowserClient({ tracesSampleRate: 1 }));
133+
const transaction = hub.startTransaction({ name: 'dogpark' });
134+
135+
expect(transaction.sampled).toBe(true);
136+
});
137+
128138
it("should call tracesSampler if it's defined", () => {
129139
const tracesSampler = jest.fn();
130140
const hub = new Hub(new BrowserClient({ tracesSampler }));
@@ -154,6 +164,15 @@ describe('Hub', () => {
154164
expect(transaction.sampled).toBe(true);
155165
});
156166

167+
it('should set sampled = true if tracesSampler returns 1 (without global hub)', () => {
168+
const tracesSampler = jest.fn().mockReturnValue(1);
169+
const hub = new Hub(new BrowserClient({ tracesSampler }));
170+
const transaction = hub.startTransaction({ name: 'dogpark' });
171+
172+
expect(tracesSampler).toHaveBeenCalled();
173+
expect(transaction.sampled).toBe(true);
174+
});
175+
157176
it('should not try to override explicitly set positive sampling decision', () => {
158177
// so that the decision otherwise would be false
159178
const tracesSampler = jest.fn().mockReturnValue(0);

0 commit comments

Comments
 (0)