diff --git a/packages/core/test/lib/tracing/trace.test.ts b/packages/core/test/lib/tracing/trace.test.ts index 31d04f0f971d..1b7de2ccf053 100644 --- a/packages/core/test/lib/tracing/trace.test.ts +++ b/packages/core/test/lib/tracing/trace.test.ts @@ -660,30 +660,57 @@ describe('startSpan', () => { }); }); - it('samples with a tracesSampler', () => { - const tracesSampler = vi.fn(() => { + describe('uses tracesSampler if defined', () => { + const tracesSampler = vi.fn<() => boolean | number>(() => { return true; }); - const options = getDefaultTestClientOptions({ tracesSampler }); - client = new TestClient(options); - setCurrentClient(client); - client.init(); + it.each([true, 1])('returns a positive sampling decision if tracesSampler returns %s', tracesSamplerResult => { + tracesSampler.mockReturnValueOnce(tracesSamplerResult); - startSpan({ name: 'outer', attributes: { test1: 'aa', test2: 'aa', test3: 'bb' } }, outerSpan => { - expect(outerSpan).toBeDefined(); + const options = getDefaultTestClientOptions({ tracesSampler }); + client = new TestClient(options); + setCurrentClient(client); + client.init(); + + startSpan({ name: 'outer', attributes: { test1: 'aa', test2: 'aa', test3: 'bb' } }, outerSpan => { + expect(outerSpan).toBeDefined(); + expect(spanIsSampled(outerSpan)).toBe(true); + }); + + expect(tracesSampler).toBeCalledTimes(1); + expect(tracesSampler).toHaveBeenLastCalledWith({ + parentSampled: undefined, + name: 'outer', + attributes: { + test1: 'aa', + test2: 'aa', + test3: 'bb', + }, + inheritOrSampleWith: expect.any(Function), + }); }); - expect(tracesSampler).toBeCalledTimes(1); - expect(tracesSampler).toHaveBeenLastCalledWith({ - parentSampled: undefined, - name: 'outer', - attributes: { - test1: 'aa', - test2: 'aa', - test3: 'bb', - }, - inheritOrSampleWith: expect.any(Function), + it.each([false, 0])('returns a negative sampling decision if tracesSampler returns %s', tracesSamplerResult => { + tracesSampler.mockReturnValueOnce(tracesSamplerResult); + + const options = getDefaultTestClientOptions({ tracesSampler }); + client = new TestClient(options); + setCurrentClient(client); + client.init(); + + startSpan({ name: 'outer' }, outerSpan => { + expect(outerSpan).toBeDefined(); + expect(spanIsSampled(outerSpan)).toBe(false); + }); + + expect(tracesSampler).toBeCalledTimes(1); + expect(tracesSampler).toHaveBeenLastCalledWith({ + parentSampled: undefined, + attributes: {}, + name: 'outer', + inheritOrSampleWith: expect.any(Function), + }); }); });