|
| 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 to durable object', async ({ signal }) => { |
| 6 | + let workerTraceId: string | undefined; |
| 7 | + let workerSpanId: string | undefined; |
| 8 | + let doTraceId: string | undefined; |
| 9 | + let doParentSpanId: string | undefined; |
| 10 | + |
| 11 | + const runner = createRunner(__dirname) |
| 12 | + .expect(envelope => { |
| 13 | + const transactionEvent = envelope[1]?.[0]?.[1] as Event; |
| 14 | + |
| 15 | + expect(transactionEvent).toEqual( |
| 16 | + expect.objectContaining({ |
| 17 | + contexts: expect.objectContaining({ |
| 18 | + trace: expect.objectContaining({ |
| 19 | + op: 'http.server', |
| 20 | + data: expect.objectContaining({ |
| 21 | + 'sentry.origin': 'auto.http.cloudflare', |
| 22 | + }), |
| 23 | + origin: 'auto.http.cloudflare', |
| 24 | + }), |
| 25 | + }), |
| 26 | + transaction: 'GET /hello', |
| 27 | + }), |
| 28 | + ); |
| 29 | + doTraceId = transactionEvent.contexts?.trace?.trace_id as string; |
| 30 | + doParentSpanId = transactionEvent.contexts?.trace?.parent_span_id as string; |
| 31 | + }) |
| 32 | + .expect(envelope => { |
| 33 | + const transactionEvent = envelope[1]?.[0]?.[1] as Event; |
| 34 | + |
| 35 | + expect(transactionEvent).toEqual( |
| 36 | + expect.objectContaining({ |
| 37 | + contexts: expect.objectContaining({ |
| 38 | + trace: expect.objectContaining({ |
| 39 | + op: 'http.server', |
| 40 | + data: expect.objectContaining({ |
| 41 | + 'sentry.origin': 'auto.http.cloudflare', |
| 42 | + }), |
| 43 | + origin: 'auto.http.cloudflare', |
| 44 | + }), |
| 45 | + }), |
| 46 | + transaction: 'GET /', |
| 47 | + }), |
| 48 | + ); |
| 49 | + workerTraceId = transactionEvent.contexts?.trace?.trace_id as string; |
| 50 | + workerSpanId = transactionEvent.contexts?.trace?.span_id as string; |
| 51 | + }) |
| 52 | + .unordered() |
| 53 | + .start(signal); |
| 54 | + await runner.makeRequest('get', '/'); |
| 55 | + await runner.completed(); |
| 56 | + |
| 57 | + expect(workerTraceId).toBeDefined(); |
| 58 | + expect(doTraceId).toBeDefined(); |
| 59 | + expect(workerTraceId).toBe(doTraceId); |
| 60 | + |
| 61 | + expect(workerSpanId).toBeDefined(); |
| 62 | + expect(doParentSpanId).toBeDefined(); |
| 63 | + expect(doParentSpanId).toBe(workerSpanId); |
| 64 | +}); |
| 65 | + |
| 66 | +it('propagates trace from queue handler to durable object', async ({ signal }) => { |
| 67 | + let queueTraceId: string | undefined; |
| 68 | + let queueSpanId: string | undefined; |
| 69 | + let doTraceId: string | undefined; |
| 70 | + let doParentSpanId: string | undefined; |
| 71 | + |
| 72 | + const runner = createRunner(__dirname) |
| 73 | + .expect(envelope => { |
| 74 | + const transactionEvent = envelope[1]?.[0]?.[1] as Event; |
| 75 | + |
| 76 | + expect(transactionEvent).toEqual( |
| 77 | + expect.objectContaining({ |
| 78 | + contexts: expect.objectContaining({ |
| 79 | + trace: expect.objectContaining({ |
| 80 | + op: 'http.server', |
| 81 | + data: expect.objectContaining({ |
| 82 | + 'sentry.origin': 'auto.http.cloudflare', |
| 83 | + }), |
| 84 | + origin: 'auto.http.cloudflare', |
| 85 | + }), |
| 86 | + }), |
| 87 | + transaction: 'GET /hello', |
| 88 | + }), |
| 89 | + ); |
| 90 | + doTraceId = transactionEvent.contexts?.trace?.trace_id as string; |
| 91 | + doParentSpanId = transactionEvent.contexts?.trace?.parent_span_id as string; |
| 92 | + }) |
| 93 | + .expect(envelope => { |
| 94 | + const transactionEvent = envelope[1]?.[0]?.[1] as Event; |
| 95 | + |
| 96 | + expect(transactionEvent).toEqual( |
| 97 | + expect.objectContaining({ |
| 98 | + contexts: expect.objectContaining({ |
| 99 | + trace: expect.objectContaining({ |
| 100 | + op: 'queue.process', |
| 101 | + data: expect.objectContaining({ |
| 102 | + 'sentry.origin': 'auto.faas.cloudflare.queue', |
| 103 | + }), |
| 104 | + origin: 'auto.faas.cloudflare.queue', |
| 105 | + }), |
| 106 | + }), |
| 107 | + transaction: 'process my-queue', |
| 108 | + }), |
| 109 | + ); |
| 110 | + queueTraceId = transactionEvent.contexts?.trace?.trace_id as string; |
| 111 | + queueSpanId = transactionEvent.contexts?.trace?.span_id as string; |
| 112 | + }) |
| 113 | + // Also expect the fetch transaction from the /queue/send request |
| 114 | + .expect(envelope => { |
| 115 | + const transactionEvent = envelope[1]?.[0]?.[1] as Event; |
| 116 | + |
| 117 | + expect(transactionEvent).toEqual( |
| 118 | + expect.objectContaining({ |
| 119 | + contexts: expect.objectContaining({ |
| 120 | + trace: expect.objectContaining({ |
| 121 | + op: 'http.server', |
| 122 | + data: expect.objectContaining({ |
| 123 | + 'sentry.origin': 'auto.http.cloudflare', |
| 124 | + }), |
| 125 | + origin: 'auto.http.cloudflare', |
| 126 | + }), |
| 127 | + }), |
| 128 | + transaction: 'GET /queue/send', |
| 129 | + }), |
| 130 | + ); |
| 131 | + }) |
| 132 | + .unordered() |
| 133 | + .start(signal); |
| 134 | + // The fetch handler sends a message to the queue, which triggers the queue consumer |
| 135 | + await runner.makeRequest('get', '/queue/send'); |
| 136 | + await runner.completed(); |
| 137 | + |
| 138 | + expect(queueTraceId).toBeDefined(); |
| 139 | + expect(doTraceId).toBeDefined(); |
| 140 | + expect(queueTraceId).toBe(doTraceId); |
| 141 | + |
| 142 | + expect(queueSpanId).toBeDefined(); |
| 143 | + expect(doParentSpanId).toBeDefined(); |
| 144 | + expect(doParentSpanId).toBe(queueSpanId); |
| 145 | +}); |
| 146 | + |
| 147 | +it('propagates trace from scheduled handler to durable object', async ({ signal }) => { |
| 148 | + let scheduledTraceId: string | undefined; |
| 149 | + let scheduledSpanId: string | undefined; |
| 150 | + let doTraceId: string | undefined; |
| 151 | + let doParentSpanId: string | undefined; |
| 152 | + |
| 153 | + const runner = createRunner(__dirname) |
| 154 | + .withWranglerArgs('--test-scheduled') |
| 155 | + .expect(envelope => { |
| 156 | + const transactionEvent = envelope[1]?.[0]?.[1] as Event; |
| 157 | + |
| 158 | + expect(transactionEvent).toEqual( |
| 159 | + expect.objectContaining({ |
| 160 | + contexts: expect.objectContaining({ |
| 161 | + trace: expect.objectContaining({ |
| 162 | + op: 'http.server', |
| 163 | + data: expect.objectContaining({ |
| 164 | + 'sentry.origin': 'auto.http.cloudflare', |
| 165 | + }), |
| 166 | + origin: 'auto.http.cloudflare', |
| 167 | + }), |
| 168 | + }), |
| 169 | + transaction: 'GET /hello', |
| 170 | + }), |
| 171 | + ); |
| 172 | + doTraceId = transactionEvent.contexts?.trace?.trace_id as string; |
| 173 | + doParentSpanId = transactionEvent.contexts?.trace?.parent_span_id as string; |
| 174 | + }) |
| 175 | + .expect(envelope => { |
| 176 | + const transactionEvent = envelope[1]?.[0]?.[1] as Event; |
| 177 | + |
| 178 | + expect(transactionEvent).toEqual( |
| 179 | + expect.objectContaining({ |
| 180 | + contexts: expect.objectContaining({ |
| 181 | + trace: expect.objectContaining({ |
| 182 | + op: 'faas.cron', |
| 183 | + data: expect.objectContaining({ |
| 184 | + 'sentry.origin': 'auto.faas.cloudflare.scheduled', |
| 185 | + }), |
| 186 | + origin: 'auto.faas.cloudflare.scheduled', |
| 187 | + }), |
| 188 | + }), |
| 189 | + }), |
| 190 | + ); |
| 191 | + scheduledTraceId = transactionEvent.contexts?.trace?.trace_id as string; |
| 192 | + scheduledSpanId = transactionEvent.contexts?.trace?.span_id as string; |
| 193 | + }) |
| 194 | + .unordered() |
| 195 | + .start(signal); |
| 196 | + await runner.makeRequest('get', '/__scheduled?cron=*+*+*+*+*'); |
| 197 | + await runner.completed(); |
| 198 | + |
| 199 | + expect(scheduledTraceId).toBeDefined(); |
| 200 | + expect(doTraceId).toBeDefined(); |
| 201 | + expect(scheduledTraceId).toBe(doTraceId); |
| 202 | + |
| 203 | + expect(scheduledSpanId).toBeDefined(); |
| 204 | + expect(doParentSpanId).toBeDefined(); |
| 205 | + expect(doParentSpanId).toBe(scheduledSpanId); |
| 206 | +}); |
0 commit comments