|
| 1 | +import { expect, test } from '@playwright/test'; |
| 2 | +import { waitForTransaction } from '@sentry-internal/test-utils'; |
| 3 | +import { callRpc } from './agent-socket'; |
| 4 | + |
| 5 | +// The worker entry (`src/index.ts`) contains no Sentry calls at all — every |
| 6 | +// wrapper below was injected by the Vite auto-instrument plugin at build time. |
| 7 | +// Any transaction arriving here therefore proves the injection happened. |
| 8 | + |
| 9 | +test('wraps the default export with withSentry (options from instrument.server.ts)', async ({ baseURL }) => { |
| 10 | + const transactionPromise = waitForTransaction('cloudflare-autoinstrument', event => { |
| 11 | + return event.contexts?.trace?.op === 'http.server' && (event.request?.url ?? '').includes('/plain-do'); |
| 12 | + }); |
| 13 | + |
| 14 | + const res = await fetch(`${baseURL}/plain-do`); |
| 15 | + expect(res.status).toBe(200); |
| 16 | + await expect(res.json()).resolves.toEqual({ durableObject: true }); |
| 17 | + |
| 18 | + const transaction = await transactionPromise; |
| 19 | + |
| 20 | + expect(transaction.contexts?.trace?.origin).toBe('auto.http.cloudflare'); |
| 21 | + // `environment: 'qa'` is only set in `instrument.server.ts`, so seeing it here |
| 22 | + // proves the plugin sourced its options callback from that file rather than |
| 23 | + // falling back to reading configuration off `env`. |
| 24 | + expect(transaction.environment).toBe('qa'); |
| 25 | +}); |
| 26 | + |
| 27 | +// Each of these three classes is registered in wrangler.jsonc exactly like the |
| 28 | +// plain Durable Object below — only the base-class chain marks them as Agents. |
| 29 | +// An `rpc` span with origin `auto.faas.cloudflare.agents` is produced solely by |
| 30 | +// `instrumentAgentWithSentry`, so its presence is what distinguishes a correct |
| 31 | +// agent upgrade from a plain `instrumentDurableObjectWithSentry` wrap. |
| 32 | +for (const { title, binding, agentClass } of [ |
| 33 | + { |
| 34 | + title: 'an Agent subclass declared in the entry', |
| 35 | + binding: 'my-agent', |
| 36 | + agentClass: 'MyAgent', |
| 37 | + }, |
| 38 | + { |
| 39 | + title: 'an AIChatAgent subclass declared in the entry', |
| 40 | + binding: 'my-chat-agent', |
| 41 | + agentClass: 'MyChatAgent', |
| 42 | + }, |
| 43 | + { |
| 44 | + title: 'an Agent subclass whose base class lives in another module', |
| 45 | + binding: 'derived-agent', |
| 46 | + agentClass: 'DerivedAgent', |
| 47 | + }, |
| 48 | +]) { |
| 49 | + test(`applies agent instrumentation to ${title}`, async ({ baseURL }) => { |
| 50 | + const instance = `${binding}-instance`; |
| 51 | + |
| 52 | + const transactionPromise = waitForTransaction('cloudflare-autoinstrument', event => { |
| 53 | + return ( |
| 54 | + event.transaction === 'webSocketMessage' && |
| 55 | + (event.spans ?? []).some(span => span.op === 'rpc' && span.description === 'greet') |
| 56 | + ); |
| 57 | + }); |
| 58 | + |
| 59 | + // Each agent's greet() returns a string naming its class, so the reply |
| 60 | + // identifies exactly which class handled the call. |
| 61 | + const reply = await callRpc(baseURL!, { binding, instance, method: 'greet', args: ['World'] }); |
| 62 | + expect(reply).toBe(`Hello, World! (from ${agentClass})`); |
| 63 | + |
| 64 | + const transaction = await transactionPromise; |
| 65 | + const rpcSpan = (transaction.spans ?? []).find(span => span.op === 'rpc' && span.description === 'greet'); |
| 66 | + |
| 67 | + expect(rpcSpan).toEqual( |
| 68 | + expect.objectContaining({ |
| 69 | + op: 'rpc', |
| 70 | + description: 'greet', |
| 71 | + origin: 'auto.faas.cloudflare.agents', |
| 72 | + data: expect.objectContaining({ |
| 73 | + // Read back off the instance at runtime (`_ParentClass.name`), so it |
| 74 | + // confirms the wrapper landed on the user's real class. Matched loosely |
| 75 | + // because the transform renames the class it wraps to |
| 76 | + // `__SENTRY_ORIGINAL_<name>__` and the bundler infers that name. |
| 77 | + 'cloudflare.agent.class': expect.stringContaining(agentClass), |
| 78 | + 'cloudflare.agent.name': instance, |
| 79 | + }), |
| 80 | + }), |
| 81 | + ); |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | +test('applies plain Durable Object instrumentation to a non-Agent class', async ({ baseURL }) => { |
| 86 | + const transactionPromise = waitForTransaction('cloudflare-autoinstrument', event => { |
| 87 | + return event.contexts?.trace?.op === 'http.server' && (event.request?.url ?? '').includes('/plain-do'); |
| 88 | + }); |
| 89 | + |
| 90 | + const res = await fetch(`${baseURL}/plain-do`); |
| 91 | + expect(res.status).toBe(200); |
| 92 | + |
| 93 | + const transaction = await transactionPromise; |
| 94 | + |
| 95 | + // A plain Durable Object must NOT pick up agent instrumentation: detection has |
| 96 | + // to discriminate, not blanket-upgrade every `durable_objects` binding. |
| 97 | + const agentSpans = (transaction.spans ?? []).filter(span => span.origin === 'auto.faas.cloudflare.agents'); |
| 98 | + expect(agentSpans).toEqual([]); |
| 99 | +}); |
0 commit comments