|
| 1 | +import type { Event, SerializedStreamedSpanContainer } from '@sentry/core'; |
| 2 | +import { createRequire } from 'module'; |
| 3 | +import { expect, it } from 'vitest'; |
| 4 | +import { SHORT_UUID_MATCHER } from '../../../expect'; |
| 5 | +import { createRunner } from '../../../runner'; |
| 6 | + |
| 7 | +// `wrangler dev` runs the workerd of its own miniflare. `Span.spanContext()` ships in workerd 1.20261009.1, so the |
| 8 | +// pinned wrangler runs these tests without it and the latest wrangler runs them with it. |
| 9 | +const wranglerRequire = createRequire(createRequire(__filename).resolve('wrangler/package.json')); |
| 10 | +const miniflareRequire = createRequire(wranglerRequire.resolve('miniflare/package.json')); |
| 11 | +const [, workerdDate = 0, workerdPatch = 0] = (miniflareRequire('workerd/package.json') as { version: string }).version |
| 12 | + .split('.') |
| 13 | + .map(Number); |
| 14 | +const HAS_SPAN_CONTEXT = workerdDate > 20261009 || (workerdDate === 20261009 && workerdPatch >= 1); |
| 15 | + |
| 16 | +const VALID_TRACE_ID = expect.stringMatching(/^(?!0{32})[\da-f]{32}$/); |
| 17 | +// Without `Span.spanContext()`, the Worker responds with `null` and the SDK starts its own trace. On a runtime with it, |
| 18 | +// a `null` response fails the test, so the `VALID_TRACE_ID` fallbacks below apply only to older runtimes. |
| 19 | +const EXPECTED_CLOUDFLARE_SPAN = HAS_SPAN_CONTEXT |
| 20 | + ? expect.objectContaining({ traceId: VALID_TRACE_ID, spanId: SHORT_UUID_MATCHER }) |
| 21 | + : null; |
| 22 | + |
| 23 | +it('uses the Cloudflare trace for the error event and the segment span', async ({ signal }) => { |
| 24 | + const errorEvents: Event[] = []; |
| 25 | + const spanContainers: SerializedStreamedSpanContainer[] = []; |
| 26 | + |
| 27 | + const runner = createRunner(__dirname) |
| 28 | + .unordered() |
| 29 | + .expect(envelope => { |
| 30 | + expect(envelope[1][0][0].type).toBe('event'); |
| 31 | + errorEvents.push(envelope[1][0][1] as Event); |
| 32 | + }) |
| 33 | + .expect(envelope => { |
| 34 | + expect(envelope[1][0][0].type).toBe('span'); |
| 35 | + spanContainers.push(envelope[1][0][1] as SerializedStreamedSpanContainer); |
| 36 | + }) |
| 37 | + .start(signal); |
| 38 | + |
| 39 | + const cloudflareSpan = await runner.makeRequest<{ traceId: string; spanId: string } | null>('get', '/'); |
| 40 | + await runner.completed(); |
| 41 | + |
| 42 | + const segmentSpans = spanContainers.flatMap(container => container.items).filter(span => span.is_segment); |
| 43 | + expect(cloudflareSpan).toEqual(EXPECTED_CLOUDFLARE_SPAN); |
| 44 | + expect(errorEvents).toHaveLength(1); |
| 45 | + expect(segmentSpans).toHaveLength(1); |
| 46 | + expect(segmentSpans[0]?.trace_id).toEqual(cloudflareSpan?.traceId ?? VALID_TRACE_ID); |
| 47 | + expect(segmentSpans[0]?.parent_span_id).toBe(cloudflareSpan?.spanId); |
| 48 | + expect(errorEvents[0]?.contexts?.trace?.trace_id).toBe(segmentSpans[0]?.trace_id); |
| 49 | +}); |
| 50 | + |
| 51 | +it('uses the Cloudflare trace for the error event when no span is sampled', async ({ signal }) => { |
| 52 | + const errorEvents: Event[] = []; |
| 53 | + |
| 54 | + const runner = createRunner(__dirname) |
| 55 | + .withWranglerArgs('--var', 'TRACES_SAMPLE_RATE:0') |
| 56 | + .expect(envelope => { |
| 57 | + expect(envelope[1][0][0].type).toBe('event'); |
| 58 | + errorEvents.push(envelope[1][0][1] as Event); |
| 59 | + }) |
| 60 | + .start(signal); |
| 61 | + |
| 62 | + const cloudflareSpan = await runner.makeRequest<{ traceId: string; spanId: string } | null>('get', '/'); |
| 63 | + await runner.completed(); |
| 64 | + |
| 65 | + expect(cloudflareSpan).toEqual(EXPECTED_CLOUDFLARE_SPAN); |
| 66 | + expect(errorEvents).toHaveLength(1); |
| 67 | + expect(errorEvents[0]?.contexts?.trace).toEqual({ |
| 68 | + trace_id: cloudflareSpan?.traceId ?? VALID_TRACE_ID, |
| 69 | + span_id: SHORT_UUID_MATCHER, |
| 70 | + parent_span_id: cloudflareSpan?.spanId, |
| 71 | + }); |
| 72 | +}); |
| 73 | + |
| 74 | +it('continues an incoming Sentry trace instead of the Cloudflare trace', async ({ signal }) => { |
| 75 | + const errorEvents: Event[] = []; |
| 76 | + |
| 77 | + const runner = createRunner(__dirname) |
| 78 | + .unordered() |
| 79 | + .expect(envelope => { |
| 80 | + expect(envelope[1][0][0].type).toBe('event'); |
| 81 | + errorEvents.push(envelope[1][0][1] as Event); |
| 82 | + }) |
| 83 | + .expect(envelope => { |
| 84 | + expect(envelope[1][0][0].type).toBe('span'); |
| 85 | + }) |
| 86 | + .start(signal); |
| 87 | + |
| 88 | + await runner.makeRequest('get', '/', { |
| 89 | + headers: { 'sentry-trace': '12345678901234567890123456789012-1234567890123456-1' }, |
| 90 | + }); |
| 91 | + await runner.completed(); |
| 92 | + |
| 93 | + expect(errorEvents).toHaveLength(1); |
| 94 | + expect(errorEvents[0]?.contexts?.trace?.trace_id).toBe('12345678901234567890123456789012'); |
| 95 | + expect(errorEvents[0]?.contexts?.trace?.parent_span_id).toBe('1234567890123456'); |
| 96 | +}); |
0 commit comments