Skip to content
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