Skip to content

Commit 74b680d

Browse files
authored
test(core): Add explicit tests for tracesSampler returning negative sampling decisions (#17055)
Add a couple of unit tests to explicitly test sampling behaviour of the core SDK's sampling logic when users pass in a `tracesSampler` function returning negative sampling values (i.e. `0`, `false`).
1 parent 8cc324c commit 74b680d

File tree

1 file changed

+45
-18
lines changed

1 file changed

+45
-18
lines changed

packages/core/test/lib/tracing/trace.test.ts

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -660,30 +660,57 @@ describe('startSpan', () => {
660660
});
661661
});
662662

663-
it('samples with a tracesSampler', () => {
664-
const tracesSampler = vi.fn(() => {
663+
describe('uses tracesSampler if defined', () => {
664+
const tracesSampler = vi.fn<() => boolean | number>(() => {
665665
return true;
666666
});
667667

668-
const options = getDefaultTestClientOptions({ tracesSampler });
669-
client = new TestClient(options);
670-
setCurrentClient(client);
671-
client.init();
668+
it.each([true, 1])('returns a positive sampling decision if tracesSampler returns %s', tracesSamplerResult => {
669+
tracesSampler.mockReturnValueOnce(tracesSamplerResult);
672670

673-
startSpan({ name: 'outer', attributes: { test1: 'aa', test2: 'aa', test3: 'bb' } }, outerSpan => {
674-
expect(outerSpan).toBeDefined();
671+
const options = getDefaultTestClientOptions({ tracesSampler });
672+
client = new TestClient(options);
673+
setCurrentClient(client);
674+
client.init();
675+
676+
startSpan({ name: 'outer', attributes: { test1: 'aa', test2: 'aa', test3: 'bb' } }, outerSpan => {
677+
expect(outerSpan).toBeDefined();
678+
expect(spanIsSampled(outerSpan)).toBe(true);
679+
});
680+
681+
expect(tracesSampler).toBeCalledTimes(1);
682+
expect(tracesSampler).toHaveBeenLastCalledWith({
683+
parentSampled: undefined,
684+
name: 'outer',
685+
attributes: {
686+
test1: 'aa',
687+
test2: 'aa',
688+
test3: 'bb',
689+
},
690+
inheritOrSampleWith: expect.any(Function),
691+
});
675692
});
676693

677-
expect(tracesSampler).toBeCalledTimes(1);
678-
expect(tracesSampler).toHaveBeenLastCalledWith({
679-
parentSampled: undefined,
680-
name: 'outer',
681-
attributes: {
682-
test1: 'aa',
683-
test2: 'aa',
684-
test3: 'bb',
685-
},
686-
inheritOrSampleWith: expect.any(Function),
694+
it.each([false, 0])('returns a negative sampling decision if tracesSampler returns %s', tracesSamplerResult => {
695+
tracesSampler.mockReturnValueOnce(tracesSamplerResult);
696+
697+
const options = getDefaultTestClientOptions({ tracesSampler });
698+
client = new TestClient(options);
699+
setCurrentClient(client);
700+
client.init();
701+
702+
startSpan({ name: 'outer' }, outerSpan => {
703+
expect(outerSpan).toBeDefined();
704+
expect(spanIsSampled(outerSpan)).toBe(false);
705+
});
706+
707+
expect(tracesSampler).toBeCalledTimes(1);
708+
expect(tracesSampler).toHaveBeenLastCalledWith({
709+
parentSampled: undefined,
710+
attributes: {},
711+
name: 'outer',
712+
inheritOrSampleWith: expect.any(Function),
713+
});
687714
});
688715
});
689716

0 commit comments

Comments
 (0)