|
| 1 | +import { expect, it } from 'vitest'; |
| 2 | +import type { Event } from '@sentry/core'; |
| 3 | +import { createRunner } from '../../../../runner'; |
| 4 | + |
| 5 | +it('propagates trace from Worker (ExportedHandler) to WorkerEntrypoint via service binding fetch', async ({ |
| 6 | + signal, |
| 7 | +}) => { |
| 8 | + let workerTraceId: string | undefined; |
| 9 | + let workerSpanId: string | undefined; |
| 10 | + let entrypointTraceId: string | undefined; |
| 11 | + let entrypointParentSpanId: string | undefined; |
| 12 | + |
| 13 | + const runner = createRunner(__dirname) |
| 14 | + .expect(envelope => { |
| 15 | + const transactionEvent = envelope[1]?.[0]?.[1] as Event; |
| 16 | + |
| 17 | + // Main worker HTTP server transaction |
| 18 | + expect(transactionEvent).toEqual( |
| 19 | + expect.objectContaining({ |
| 20 | + contexts: expect.objectContaining({ |
| 21 | + trace: expect.objectContaining({ |
| 22 | + op: 'http.server', |
| 23 | + data: expect.objectContaining({ |
| 24 | + 'sentry.origin': 'auto.http.cloudflare', |
| 25 | + }), |
| 26 | + origin: 'auto.http.cloudflare', |
| 27 | + }), |
| 28 | + }), |
| 29 | + transaction: 'GET /call-entrypoint', |
| 30 | + }), |
| 31 | + ); |
| 32 | + workerTraceId = transactionEvent.contexts?.trace?.trace_id as string; |
| 33 | + workerSpanId = transactionEvent.contexts?.trace?.span_id as string; |
| 34 | + }) |
| 35 | + .expect(envelope => { |
| 36 | + const transactionEvent = envelope[1]?.[0]?.[1] as Event; |
| 37 | + |
| 38 | + // WorkerEntrypoint HTTP server transaction (from service binding fetch) |
| 39 | + expect(transactionEvent).toEqual( |
| 40 | + expect.objectContaining({ |
| 41 | + contexts: expect.objectContaining({ |
| 42 | + trace: expect.objectContaining({ |
| 43 | + op: 'http.server', |
| 44 | + data: expect.objectContaining({ |
| 45 | + 'sentry.origin': 'auto.http.cloudflare', |
| 46 | + }), |
| 47 | + origin: 'auto.http.cloudflare', |
| 48 | + }), |
| 49 | + }), |
| 50 | + transaction: 'GET /answer', |
| 51 | + }), |
| 52 | + ); |
| 53 | + entrypointTraceId = transactionEvent.contexts?.trace?.trace_id as string; |
| 54 | + entrypointParentSpanId = transactionEvent.contexts?.trace?.parent_span_id as string; |
| 55 | + }) |
| 56 | + .unordered() |
| 57 | + .start(signal); |
| 58 | + |
| 59 | + const response = await runner.makeRequest<string>('get', '/call-entrypoint'); |
| 60 | + expect(response).toBe('The answer is 42'); |
| 61 | + |
| 62 | + await runner.completed(); |
| 63 | + |
| 64 | + // Both transactions should share the same trace_id |
| 65 | + expect(workerTraceId).toBeDefined(); |
| 66 | + expect(entrypointTraceId).toBeDefined(); |
| 67 | + expect(workerTraceId).toBe(entrypointTraceId); |
| 68 | + |
| 69 | + // Verify the parent-child relationship: Worker -> WorkerEntrypoint |
| 70 | + expect(workerSpanId).toBeDefined(); |
| 71 | + expect(entrypointParentSpanId).toBeDefined(); |
| 72 | + expect(entrypointParentSpanId).toBe(workerSpanId); |
| 73 | +}); |
| 74 | + |
| 75 | +it('propagates trace for request with query params from Worker to WorkerEntrypoint', async ({ signal }) => { |
| 76 | + let workerTraceId: string | undefined; |
| 77 | + let workerSpanId: string | undefined; |
| 78 | + let entrypointTraceId: string | undefined; |
| 79 | + let entrypointParentSpanId: string | undefined; |
| 80 | + |
| 81 | + const runner = createRunner(__dirname) |
| 82 | + .expect(envelope => { |
| 83 | + const transactionEvent = envelope[1]?.[0]?.[1] as Event; |
| 84 | + |
| 85 | + expect(transactionEvent).toEqual( |
| 86 | + expect.objectContaining({ |
| 87 | + contexts: expect.objectContaining({ |
| 88 | + trace: expect.objectContaining({ |
| 89 | + op: 'http.server', |
| 90 | + }), |
| 91 | + }), |
| 92 | + transaction: 'GET /call-entrypoint-greet', |
| 93 | + }), |
| 94 | + ); |
| 95 | + workerTraceId = transactionEvent.contexts?.trace?.trace_id as string; |
| 96 | + workerSpanId = transactionEvent.contexts?.trace?.span_id as string; |
| 97 | + }) |
| 98 | + .expect(envelope => { |
| 99 | + const transactionEvent = envelope[1]?.[0]?.[1] as Event; |
| 100 | + |
| 101 | + expect(transactionEvent).toEqual( |
| 102 | + expect.objectContaining({ |
| 103 | + contexts: expect.objectContaining({ |
| 104 | + trace: expect.objectContaining({ |
| 105 | + op: 'http.server', |
| 106 | + }), |
| 107 | + }), |
| 108 | + transaction: 'GET /greet', |
| 109 | + }), |
| 110 | + ); |
| 111 | + entrypointTraceId = transactionEvent.contexts?.trace?.trace_id as string; |
| 112 | + entrypointParentSpanId = transactionEvent.contexts?.trace?.parent_span_id as string; |
| 113 | + }) |
| 114 | + .unordered() |
| 115 | + .start(signal); |
| 116 | + |
| 117 | + const response = await runner.makeRequest<string>('get', '/call-entrypoint-greet'); |
| 118 | + expect(response).toBe('Hello, World!'); |
| 119 | + |
| 120 | + await runner.completed(); |
| 121 | + |
| 122 | + expect(workerTraceId).toBeDefined(); |
| 123 | + expect(entrypointTraceId).toBeDefined(); |
| 124 | + expect(workerTraceId).toBe(entrypointTraceId); |
| 125 | + |
| 126 | + expect(workerSpanId).toBeDefined(); |
| 127 | + expect(entrypointParentSpanId).toBeDefined(); |
| 128 | + expect(entrypointParentSpanId).toBe(workerSpanId); |
| 129 | +}); |
0 commit comments