|
1 | 1 | import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
2 | 2 | import {
|
| 3 | + ClientOptions, |
3 | 4 | getCurrentScope,
|
4 | 5 | getGlobalScope,
|
5 | 6 | getIsolationScope,
|
@@ -660,30 +661,60 @@ describe('startSpan', () => {
|
660 | 661 | });
|
661 | 662 | });
|
662 | 663 |
|
663 |
| - it('samples with a tracesSampler', () => { |
664 |
| - const tracesSampler = vi.fn(() => { |
| 664 | + describe('uses tracesSampler if defined', () => { |
| 665 | + const tracesSampler = vi.fn<() => boolean | number>(() => { |
665 | 666 | return true;
|
666 | 667 | });
|
667 | 668 |
|
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); |
672 | 671 |
|
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 | + }); |
675 | 693 | });
|
676 | 694 |
|
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 | + }); |
687 | 718 | });
|
688 | 719 | });
|
689 | 720 |
|
|
0 commit comments