|
| 1 | +import { expect, test } from '@playwright/test'; |
| 2 | +import { waitForError, waitForRequest, waitForTransaction } from '@sentry-internal/test-utils'; |
| 3 | +import { SDK_VERSION } from '@sentry/cloudflare'; |
| 4 | +import { WebSocket } from 'ws'; |
| 5 | + |
| 6 | +test('Index page', async ({ baseURL }) => { |
| 7 | + const result = await fetch(baseURL!); |
| 8 | + expect(result.status).toBe(200); |
| 9 | + await expect(result.text()).resolves.toBe('Hello World!'); |
| 10 | +}); |
| 11 | + |
| 12 | +test("worker's withSentry", async ({ baseURL }) => { |
| 13 | + const eventWaiter = waitForError('cloudflare-workers-static', event => { |
| 14 | + return event.exception?.values?.[0]?.mechanism?.type === 'auto.http.cloudflare'; |
| 15 | + }); |
| 16 | + const response = await fetch(`${baseURL}/throwException`); |
| 17 | + expect(response.status).toBe(500); |
| 18 | + const event = await eventWaiter; |
| 19 | + expect(event.exception?.values?.[0]?.value).toBe('To be recorded in Sentry.'); |
| 20 | +}); |
| 21 | + |
| 22 | +test('RPC method which throws an exception to be logged to sentry', async ({ baseURL }) => { |
| 23 | + const eventWaiter = waitForError('cloudflare-workers-static', event => { |
| 24 | + return event.exception?.values?.[0]?.mechanism?.type === 'auto.faas.cloudflare.durable_object'; |
| 25 | + }); |
| 26 | + const response = await fetch(`${baseURL}/rpc/throwException`); |
| 27 | + expect(response.status).toBe(500); |
| 28 | + const event = await eventWaiter; |
| 29 | + expect(event.exception?.values?.[0]?.value).toBe('Should be recorded in Sentry.'); |
| 30 | +}); |
| 31 | + |
| 32 | +test("Request processed by DurableObject's fetch is recorded", async ({ baseURL }) => { |
| 33 | + const eventWaiter = waitForError('cloudflare-workers-static', event => { |
| 34 | + return event.exception?.values?.[0]?.mechanism?.type === 'auto.faas.cloudflare.durable_object'; |
| 35 | + }); |
| 36 | + const response = await fetch(`${baseURL}/pass-to-object/throwException`); |
| 37 | + expect(response.status).toBe(500); |
| 38 | + const event = await eventWaiter; |
| 39 | + expect(event.exception?.values?.[0]?.value).toBe('Should be recorded in Sentry.'); |
| 40 | +}); |
| 41 | + |
| 42 | +test('Websocket.webSocketMessage', async ({ baseURL }) => { |
| 43 | + const eventWaiter = waitForError('cloudflare-workers-static', event => { |
| 44 | + return !!event.exception?.values?.[0]; |
| 45 | + }); |
| 46 | + const url = new URL('/pass-to-object/ws', baseURL); |
| 47 | + url.protocol = url.protocol.replace('http', 'ws'); |
| 48 | + const socket = new WebSocket(url.toString()); |
| 49 | + socket.addEventListener('open', () => { |
| 50 | + socket.send('throwException'); |
| 51 | + }); |
| 52 | + const event = await eventWaiter; |
| 53 | + socket.close(); |
| 54 | + expect(event.exception?.values?.[0]?.value).toBe('Should be recorded in Sentry: webSocketMessage'); |
| 55 | + expect(event.exception?.values?.[0]?.mechanism?.type).toBe('auto.faas.cloudflare.durable_object'); |
| 56 | +}); |
| 57 | + |
| 58 | +test('Websocket.webSocketClose', async ({ baseURL }) => { |
| 59 | + const eventWaiter = waitForError('cloudflare-workers-static', event => { |
| 60 | + return !!event.exception?.values?.[0]; |
| 61 | + }); |
| 62 | + const url = new URL('/pass-to-object/ws', baseURL); |
| 63 | + url.protocol = url.protocol.replace('http', 'ws'); |
| 64 | + const socket = new WebSocket(url.toString()); |
| 65 | + socket.addEventListener('open', () => { |
| 66 | + socket.send('throwOnExit'); |
| 67 | + socket.close(); |
| 68 | + }); |
| 69 | + const event = await eventWaiter; |
| 70 | + expect(event.exception?.values?.[0]?.value).toBe('Should be recorded in Sentry: webSocketClose'); |
| 71 | + expect(event.exception?.values?.[0]?.mechanism?.type).toBe('auto.faas.cloudflare.durable_object'); |
| 72 | +}); |
| 73 | + |
| 74 | +test('sends user-agent header with SDK name and version in envelope requests', async ({ baseURL }) => { |
| 75 | + const requestPromise = waitForRequest('cloudflare-workers-static', () => true); |
| 76 | + |
| 77 | + await fetch(`${baseURL}/throwException`); |
| 78 | + |
| 79 | + const request = await requestPromise; |
| 80 | + |
| 81 | + expect(request.rawProxyRequestHeaders).toMatchObject({ |
| 82 | + 'user-agent': `sentry.javascript.cloudflare/${SDK_VERSION}`, |
| 83 | + }); |
| 84 | +}); |
| 85 | + |
| 86 | +test('Storage operations create spans in Durable Object transactions', async ({ baseURL }) => { |
| 87 | + const transactionWaiter = waitForTransaction('cloudflare-workers-static', event => { |
| 88 | + return event.spans?.some(span => span.op === 'db' && span.description === 'durable_object_storage_put') ?? false; |
| 89 | + }); |
| 90 | + |
| 91 | + const response = await fetch(`${baseURL}/pass-to-object/storage/put`); |
| 92 | + expect(response.status).toBe(200); |
| 93 | + |
| 94 | + const transaction = await transactionWaiter; |
| 95 | + const putSpan = transaction.spans?.find(span => span.description === 'durable_object_storage_put'); |
| 96 | + |
| 97 | + expect(putSpan).toBeDefined(); |
| 98 | + expect(putSpan?.op).toBe('db'); |
| 99 | + expect(putSpan?.data?.['db.system.name']).toBe('cloudflare.durable_object.storage'); |
| 100 | + expect(putSpan?.data?.['db.operation.name']).toBe('put'); |
| 101 | +}); |
| 102 | + |
| 103 | +test.describe('Alarm instrumentation', () => { |
| 104 | + test.describe.configure({ mode: 'serial' }); |
| 105 | + |
| 106 | + test('captures error from alarm handler', async ({ baseURL }) => { |
| 107 | + const errorWaiter = waitForError('cloudflare-workers-static', event => { |
| 108 | + return event.exception?.values?.[0]?.value === 'Alarm error captured by Sentry'; |
| 109 | + }); |
| 110 | + |
| 111 | + const response = await fetch(`${baseURL}/pass-to-object/setAlarm?action=throw`); |
| 112 | + expect(response.status).toBe(200); |
| 113 | + |
| 114 | + const event = await errorWaiter; |
| 115 | + expect(event.exception?.values?.[0]?.mechanism?.type).toBe('auto.faas.cloudflare.durable_object'); |
| 116 | + }); |
| 117 | + |
| 118 | + test('creates a transaction for alarm with new trace linked to setAlarm', async ({ baseURL }) => { |
| 119 | + const setAlarmTransactionWaiter = waitForTransaction('cloudflare-workers-static', event => { |
| 120 | + return event.spans?.some(span => span.description?.includes('storage_setAlarm')) ?? false; |
| 121 | + }); |
| 122 | + |
| 123 | + const alarmTransactionWaiter = waitForTransaction('cloudflare-workers-static', event => { |
| 124 | + return event.transaction === 'alarm' && event.contexts?.trace?.op === 'function'; |
| 125 | + }); |
| 126 | + |
| 127 | + const response = await fetch(`${baseURL}/pass-to-object/setAlarm`); |
| 128 | + expect(response.status).toBe(200); |
| 129 | + |
| 130 | + const setAlarmTransaction = await setAlarmTransactionWaiter; |
| 131 | + const alarmTransaction = await alarmTransactionWaiter; |
| 132 | + |
| 133 | + // Alarm creates a transaction with correct attributes |
| 134 | + expect(alarmTransaction.contexts?.trace?.op).toBe('function'); |
| 135 | + expect(alarmTransaction.contexts?.trace?.origin).toBe('auto.faas.cloudflare.durable_object'); |
| 136 | + |
| 137 | + // Alarm starts a new trace (different trace ID from the request that called setAlarm) |
| 138 | + expect(alarmTransaction.contexts?.trace?.trace_id).not.toBe(setAlarmTransaction.contexts?.trace?.trace_id); |
| 139 | + |
| 140 | + // Alarm links to the trace that called setAlarm via sentry.previous_trace attribute |
| 141 | + const previousTrace = alarmTransaction.contexts?.trace?.data?.['sentry.previous_trace']; |
| 142 | + expect(previousTrace).toBeDefined(); |
| 143 | + expect(previousTrace).toContain(setAlarmTransaction.contexts?.trace?.trace_id); |
| 144 | + }); |
| 145 | +}); |
0 commit comments