From 4fe7511ff63f8d68a022c064ceb997ea31a564ec Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Thu, 17 Jul 2025 12:06:01 +0200 Subject: [PATCH 1/4] test(core): Add explicit tests for `tracesSampler` returning negative sampling decisions --- packages/core/test/lib/tracing/trace.test.ts | 67 ++++++++++++++------ 1 file changed, 49 insertions(+), 18 deletions(-) diff --git a/packages/core/test/lib/tracing/trace.test.ts b/packages/core/test/lib/tracing/trace.test.ts index 31d04f0f971d..ad84e8e04ecd 100644 --- a/packages/core/test/lib/tracing/trace.test.ts +++ b/packages/core/test/lib/tracing/trace.test.ts @@ -1,5 +1,6 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { + ClientOptions, getCurrentScope, getGlobalScope, getIsolationScope, @@ -660,30 +661,60 @@ 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); + + startSpan({ name: 'outer', attributes: { test1: 'aa', test2: 'aa', test3: 'bb' } }, outerSpan => { + expect(outerSpan).toBeDefined(); + expect(spanIsSampled(outerSpan)).toBe(false); + }); + + expect(tracesSampler).toBeCalledTimes(1); + expect(tracesSampler).toHaveBeenLastCalledWith({ + parentSampled: undefined, + name: 'outer', + attributes: { + test1: 'aa', + test2: 'aa', + test3: 'bb', + }, + inheritOrSampleWith: expect.any(Function), + }); }); }); From 727eca4416690503d74919adbd8955a3a0ee7d8b Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Thu, 17 Jul 2025 12:12:13 +0200 Subject: [PATCH 2/4] simplify test --- packages/core/test/lib/tracing/trace.test.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/core/test/lib/tracing/trace.test.ts b/packages/core/test/lib/tracing/trace.test.ts index ad84e8e04ecd..cb0b14496d2b 100644 --- a/packages/core/test/lib/tracing/trace.test.ts +++ b/packages/core/test/lib/tracing/trace.test.ts @@ -699,7 +699,7 @@ describe('startSpan', () => { client = new TestClient(options); setCurrentClient(client); - startSpan({ name: 'outer', attributes: { test1: 'aa', test2: 'aa', test3: 'bb' } }, outerSpan => { + startSpan({ name: 'outer' }, outerSpan => { expect(outerSpan).toBeDefined(); expect(spanIsSampled(outerSpan)).toBe(false); }); @@ -707,12 +707,8 @@ describe('startSpan', () => { expect(tracesSampler).toBeCalledTimes(1); expect(tracesSampler).toHaveBeenLastCalledWith({ parentSampled: undefined, + attributes: {}, name: 'outer', - attributes: { - test1: 'aa', - test2: 'aa', - test3: 'bb', - }, inheritOrSampleWith: expect.any(Function), }); }); From 6d3934f0130e134f432d9d0beef0073f468ef83a Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Thu, 17 Jul 2025 12:13:13 +0200 Subject: [PATCH 3/4] client.init --- packages/core/test/lib/tracing/trace.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/core/test/lib/tracing/trace.test.ts b/packages/core/test/lib/tracing/trace.test.ts index cb0b14496d2b..03dc0cb02fb9 100644 --- a/packages/core/test/lib/tracing/trace.test.ts +++ b/packages/core/test/lib/tracing/trace.test.ts @@ -698,6 +698,7 @@ describe('startSpan', () => { const options = getDefaultTestClientOptions({ tracesSampler }); client = new TestClient(options); setCurrentClient(client); + client.init(); startSpan({ name: 'outer' }, outerSpan => { expect(outerSpan).toBeDefined(); From 66837d966863d8b43fefcde2fd7e45fca325117c Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Thu, 17 Jul 2025 16:52:11 +0200 Subject: [PATCH 4/4] lint --- packages/core/test/lib/tracing/trace.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/core/test/lib/tracing/trace.test.ts b/packages/core/test/lib/tracing/trace.test.ts index 03dc0cb02fb9..1b7de2ccf053 100644 --- a/packages/core/test/lib/tracing/trace.test.ts +++ b/packages/core/test/lib/tracing/trace.test.ts @@ -1,6 +1,5 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { - ClientOptions, getCurrentScope, getGlobalScope, getIsolationScope,