Skip to content

Commit 4fe7511

Browse files
committed
test(core): Add explicit tests for tracesSampler returning negative sampling decisions
1 parent 962d697 commit 4fe7511

File tree

1 file changed

+49
-18
lines changed

1 file changed

+49
-18
lines changed

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

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
22
import {
3+
ClientOptions,
34
getCurrentScope,
45
getGlobalScope,
56
getIsolationScope,
@@ -660,30 +661,60 @@ describe('startSpan', () => {
660661
});
661662
});
662663

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

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

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

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),
695+
it.each([false, 0])('returns a negative sampling decision if tracesSampler returns %s', tracesSamplerResult => {
696+
tracesSampler.mockReturnValueOnce(tracesSamplerResult);
697+
698+
const options = getDefaultTestClientOptions({ tracesSampler });
699+
client = new TestClient(options);
700+
setCurrentClient(client);
701+
702+
startSpan({ name: 'outer', attributes: { test1: 'aa', test2: 'aa', test3: 'bb' } }, 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+
name: 'outer',
711+
attributes: {
712+
test1: 'aa',
713+
test2: 'aa',
714+
test3: 'bb',
715+
},
716+
inheritOrSampleWith: expect.any(Function),
717+
});
687718
});
688719
});
689720

0 commit comments

Comments
 (0)