diff --git a/packages/tracing/src/hubextensions.ts b/packages/tracing/src/hubextensions.ts index 081b9ba8b5b3..0238cde6422e 100644 --- a/packages/tracing/src/hubextensions.ts +++ b/packages/tracing/src/hubextensions.ts @@ -43,7 +43,7 @@ function traceHeaders(this: Hub): { [key: string]: string } { */ function sample(transaction: T, options: Options, samplingContext: SamplingContext): T { // nothing to do if tracing is not enabled - if (!hasTracingEnabled()) { + if (!hasTracingEnabled(options)) { transaction.sampled = false; return transaction; } diff --git a/packages/tracing/test/hub.test.ts b/packages/tracing/test/hub.test.ts index 0890240fae8c..576cf0a34ca4 100644 --- a/packages/tracing/test/hub.test.ts +++ b/packages/tracing/test/hub.test.ts @@ -26,6 +26,9 @@ addDOMPropertiesToGlobal(['XMLHttpRequest', 'Event', 'location', 'document']); describe('Hub', () => { afterEach(() => { jest.clearAllMocks(); + // Reset global carrier to the initial state + const hub = new Hub(); + makeMain(hub); }); describe('getTransaction()', () => { @@ -125,6 +128,13 @@ describe('Hub', () => { expect(transaction.sampled).toBe(true); }); + it('should set sampled = true if tracesSampleRate is 1 (without global hub)', () => { + const hub = new Hub(new BrowserClient({ tracesSampleRate: 1 })); + const transaction = hub.startTransaction({ name: 'dogpark' }); + + expect(transaction.sampled).toBe(true); + }); + it("should call tracesSampler if it's defined", () => { const tracesSampler = jest.fn(); const hub = new Hub(new BrowserClient({ tracesSampler })); @@ -154,6 +164,15 @@ describe('Hub', () => { expect(transaction.sampled).toBe(true); }); + it('should set sampled = true if tracesSampler returns 1 (without global hub)', () => { + const tracesSampler = jest.fn().mockReturnValue(1); + const hub = new Hub(new BrowserClient({ tracesSampler })); + const transaction = hub.startTransaction({ name: 'dogpark' }); + + expect(tracesSampler).toHaveBeenCalled(); + expect(transaction.sampled).toBe(true); + }); + it('should not try to override explicitly set positive sampling decision', () => { // so that the decision otherwise would be false const tracesSampler = jest.fn().mockReturnValue(0);