Skip to content

test(core): Add explicit tests for tracesSampler returning negative sampling decisions #17055

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 45 additions & 18 deletions packages/core/test/lib/tracing/trace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
});
});
});

Expand Down
Loading