|
| 1 | +import { cleanupChildProcesses, createRunner } from '../../../utils/runner'; |
| 2 | + |
| 3 | +describe('sample_rand propagation (default)', () => { |
| 4 | + afterAll(() => { |
| 5 | + cleanupChildProcesses(); |
| 6 | + }); |
| 7 | + |
| 8 | + test('propagates a sample rand when there are no incoming trace headers', async () => { |
| 9 | + const runner = createRunner(__dirname, 'server.js').start(); |
| 10 | + const response = await runner.makeRequest('get', '/check'); |
| 11 | + expect(response).toEqual({ |
| 12 | + propagatedData: { |
| 13 | + baggage: expect.stringMatching(/sentry-sample_rand=0\.[0-9]+/), |
| 14 | + }, |
| 15 | + }); |
| 16 | + }); |
| 17 | + |
| 18 | + test('propagates a sample rand when there is a sentry-trace header and incoming sentry baggage', async () => { |
| 19 | + const runner = createRunner(__dirname, 'server.js').start(); |
| 20 | + const response = await runner.makeRequest('get', '/check', { |
| 21 | + headers: { |
| 22 | + 'sentry-trace': '530699e319cc067ce440315d74acb312-414dc2a08d5d1dac-1', |
| 23 | + baggage: 'sentry-release=foo,sentry-sample_rand=0.424242', |
| 24 | + }, |
| 25 | + }); |
| 26 | + expect(response).toEqual({ |
| 27 | + propagatedData: { |
| 28 | + baggage: expect.stringMatching(/sentry-sample_rand=0\.424242/), |
| 29 | + }, |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + test('propagates a sample rand when there is an incoming sentry-trace header but no baggage header', async () => { |
| 34 | + const runner = createRunner(__dirname, 'server.js').start(); |
| 35 | + const response = await runner.makeRequest('get', '/check', { |
| 36 | + headers: { |
| 37 | + 'sentry-trace': '530699e319cc067ce440315d74acb312-414dc2a08d5d1dac-1', |
| 38 | + }, |
| 39 | + }); |
| 40 | + expect(response).toEqual({ |
| 41 | + propagatedData: { |
| 42 | + baggage: expect.stringMatching(/sentry-sample_rand=0\.[0-9]+/), |
| 43 | + }, |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + test('propagates a sample_rand that would lead to a positive sampling decision when there is an incoming positive sampling decision but no sample_rand in the baggage header', async () => { |
| 48 | + const runner = createRunner(__dirname, 'server.js').start(); |
| 49 | + const response = await runner.makeRequest('get', '/check', { |
| 50 | + headers: { |
| 51 | + 'sentry-trace': '530699e319cc067ce440315d74acb312-414dc2a08d5d1dac-1', |
| 52 | + baggage: 'sentry-sample_rate=0.25', |
| 53 | + }, |
| 54 | + }); |
| 55 | + |
| 56 | + const sampleRand = Number((response as any).propagatedData.baggage.match(/sentry-sample_rand=(0\.[0-9]+)/)[1]); |
| 57 | + |
| 58 | + expect(sampleRand).toStrictEqual(expect.any(Number)); |
| 59 | + expect(sampleRand).not.toBeNaN(); |
| 60 | + expect(sampleRand).toBeLessThan(0.25); |
| 61 | + }); |
| 62 | + |
| 63 | + test('propagates a sample_rand that would lead to a negative sampling decision when there is an incoming negative sampling decision but no sample_rand in the baggage header', async () => { |
| 64 | + const runner = createRunner(__dirname, 'server.js').start(); |
| 65 | + const response = await runner.makeRequest('get', '/check', { |
| 66 | + headers: { |
| 67 | + 'sentry-trace': '530699e319cc067ce440315d74acb312-414dc2a08d5d1dac-0', |
| 68 | + baggage: 'sentry-sample_rate=0.75', |
| 69 | + }, |
| 70 | + }); |
| 71 | + |
| 72 | + const sampleRand = Number((response as any).propagatedData.baggage.match(/sentry-sample_rand=(0\.[0-9]+)/)[1]); |
| 73 | + |
| 74 | + expect(sampleRand).toStrictEqual(expect.any(Number)); |
| 75 | + expect(sampleRand).not.toBeNaN(); |
| 76 | + expect(sampleRand).toBeGreaterThanOrEqual(0.75); |
| 77 | + }); |
| 78 | + |
| 79 | + test('a new sample_rand when there is no sentry-trace header but a baggage header with sample_rand', async () => { |
| 80 | + const runner = createRunner(__dirname, 'server.js').start(); |
| 81 | + const response = await runner.makeRequest('get', '/check', { |
| 82 | + headers: { |
| 83 | + baggage: 'sentry-sample_rate=0.75,sentry-sample_rand=0.5', |
| 84 | + }, |
| 85 | + }); |
| 86 | + |
| 87 | + expect((response as any).propagatedData.baggage).toMatch(/sentry-sample_rand=0\.[0-9]+/); |
| 88 | + const sampleRandStr = (response as any).propagatedData.baggage.match(/sentry-sample_rand=(0\.[0-9]+)/)[1]; |
| 89 | + expect(sampleRandStr).not.toBe('0.5'); |
| 90 | + }); |
| 91 | +}); |
0 commit comments