Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
test(e2e): Port cloudflare-autoinstrument to span streaming#23861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
JPeer264
merged 1 commit into
jp/e2e-stream-cloudflare-orchestrion-mysql
from
jp/e2e-stream-cloudflare-autoinstrumentSep 2, 2026
+46
−36
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 0 additions & 1 deletion
1 dev-packages/e2e-tests/test-applications/cloudflare-autoinstrument/src/instrument.server.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 46 additions & 35 deletions
81 ...ckages/e2e-tests/test-applications/cloudflare-autoinstrument/tests/autoinstrument.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,31 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
| import { waitForTransaction } from '@sentry-internal/test-utils'; | ||
| import { collectStreamedSpans, getSpanOp, waitForStreamedSpan } from '@sentry-internal/test-utils'; | ||
| import { callRpc } from './agent-socket'; | ||
| // The worker entry (`src/index.ts`) contains no Sentry calls at all — every | ||
| // wrapper below was injected by the Vite auto-instrument plugin at build time. | ||
| // Any transaction arriving here therefore proves the injection happened. | ||
| // Any span arriving here therefore proves the injection happened. | ||
| // | ||
| // With span streaming, URL-sourced `http.server` spans are named by method only, so the | ||
| // `/plain-do` request segment is identified by its `url.path` attribute. | ||
| test('wraps the default export with withSentry (options from instrument.server.ts)', async ({ baseURL }) => { | ||
| const transactionPromise = waitForTransaction('cloudflare-autoinstrument', event => { | ||
| return event.contexts?.trace?.op === 'http.server' && (event.request?.url ?? '').includes('/plain-do'); | ||
| }); | ||
| const spanPromise = waitForStreamedSpan( | ||
| 'cloudflare-autoinstrument', | ||
| span => getSpanOp(span) === 'http.server' && span.is_segment && span.attributes['url.path']?.value === '/plain-do', | ||
| ); | ||
| const res = await fetch(`${baseURL}/plain-do`); | ||
| expect(res.status).toBe(200); | ||
| await expect(res.json()).resolves.toEqual({ durableObject: true }); | ||
| const transaction = await transactionPromise; | ||
| const span = await spanPromise; | ||
| expect(transaction.contexts?.trace?.origin).toBe('auto.http.cloudflare'); | ||
| expect(span.attributes['sentry.origin']?.value).toBe('auto.http.cloudflare'); | ||
| // `environment: 'qa'` is only set in `instrument.server.ts`, so seeing it here | ||
| // proves the plugin sourced its options callback from that file rather than | ||
| // falling back to reading configuration off `env`. | ||
| expect(transaction.environment).toBe('qa'); | ||
| expect(span.attributes['sentry.environment']?.value).toBe('qa'); | ||
| }); | ||
| // Each of these three classes is registered in wrangler.jsonc exactly like the | ||
| @@ -49,50 +53,57 @@ for (const { title, binding, agentClass } of [ | ||
| test(`applies agent instrumentation to ${title}`, async ({ baseURL }) => { | ||
| const instance = `${binding}-instance`; | ||
| const transactionPromise = waitForTransaction('cloudflare-autoinstrument', event => { | ||
| return ( | ||
| event.transaction === 'webSocketMessage' && | ||
| (event.spans ?? []).some(span => span.op === 'rpc' && span.description === 'greet') | ||
| ); | ||
| }); | ||
| // The rpc span is a child of the `webSocketMessage` segment span, which ends after it and is | ||
| // streamed in a later envelope, so collect until both have arrived. | ||
| const spansPromise = collectStreamedSpans( | ||
| 'cloudflare-autoinstrument', | ||
| spans => | ||
| spans.some( | ||
| span => | ||
| getSpanOp(span) === 'rpc' && | ||
| span.name === 'greet' && | ||
| String(span.attributes['gen_ai.agent.name']?.value ?? '').includes(agentClass), | ||
| ) && spans.some(span => span.is_segment && span.name === 'webSocketMessage'), | ||
| ); | ||
| // Each agent's greet() returns a string naming its class, so the reply | ||
| // identifies exactly which class handled the call. | ||
| const reply = await callRpc(baseURL!, { binding, instance, method: 'greet', args: ['World'] }); | ||
| expect(reply).toBe(`Hello, World! (from ${agentClass})`); | ||
| const transaction = await transactionPromise; | ||
| const rpcSpan = (transaction.spans ?? []).find(span => span.op === 'rpc' && span.description === 'greet'); | ||
| const spans = await spansPromise; | ||
| const rpcSpan = spans.find( | ||
sentry[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| span => | ||
| getSpanOp(span) === 'rpc' && | ||
| span.name === 'greet' && | ||
| String(span.attributes['gen_ai.agent.name']?.value ?? '').includes(agentClass), | ||
| )!; | ||
| expect(rpcSpan).toEqual( | ||
| expect.objectContaining({ | ||
| op: 'rpc', | ||
| description: 'greet', | ||
| origin: 'auto.faas.cloudflare.agents', | ||
| data: expect.objectContaining({ | ||
| // Read back off the instance at runtime (`_ParentClass.name`), so it | ||
| // confirms the wrapper landed on the user's real class. Matched loosely | ||
| // because the transform renames the class it wraps to | ||
| // `__SENTRY_ORIGINAL_<name>__` and the bundler infers that name. | ||
| 'gen_ai.agent.name': expect.stringContaining(agentClass), | ||
| }), | ||
| }), | ||
| ); | ||
| expect(rpcSpan.attributes['sentry.op']?.value).toBe('rpc'); | ||
| expect(rpcSpan.attributes['sentry.origin']?.value).toBe('auto.faas.cloudflare.agents'); | ||
| // Read back off the instance at runtime (`_ParentClass.name`), so it | ||
| // confirms the wrapper landed on the user's real class. Matched loosely | ||
| // because the transform renames the class it wraps to | ||
| // `__SENTRY_ORIGINAL_<name>__` and the bundler infers that name. | ||
| expect(rpcSpan.attributes['gen_ai.agent.name']?.value).toContain(agentClass); | ||
| }); | ||
| } | ||
| test('applies plain Durable Object instrumentation to a non-Agent class', async ({ baseURL }) => { | ||
| const transactionPromise = waitForTransaction('cloudflare-autoinstrument', event => { | ||
| return event.contexts?.trace?.op === 'http.server' && (event.request?.url ?? '').includes('/plain-do'); | ||
| }); | ||
| const spansPromise = collectStreamedSpans('cloudflare-autoinstrument', spans => | ||
| spans.some( | ||
| span => | ||
| getSpanOp(span) === 'http.server' && span.is_segment && span.attributes['url.path']?.value === '/plain-do', | ||
| ), | ||
| ); | ||
| const res = await fetch(`${baseURL}/plain-do`); | ||
| expect(res.status).toBe(200); | ||
| const transaction = await transactionPromise; | ||
| const spans = await spansPromise; | ||
| // A plain Durable Object must NOT pick up agent instrumentation: detection has | ||
| // to discriminate, not blanket-upgrade every `durable_objects` binding. | ||
| const agentSpans = (transaction.spans ?? []).filter(span => span.origin === 'auto.faas.cloudflare.agents'); | ||
| const agentSpans = spans.filter(span => span.attributes['sentry.origin']?.value === 'auto.faas.cloudflare.agents'); | ||
| expect(agentSpans).toEqual([]); | ||
| }); | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: The
waitForStreamedSpantest utility lacks trace isolation, which can cause race conditions and flaky results when tests are run in parallel.Severity: MEDIUM
Suggested Fix
Modify
waitForStreamedSpanto implement trace isolation. It should group spans bytrace_idbefore evaluating them, similar to howcollectStreamedSpansis implemented. This will ensure that the function only resolves with a span that belongs to the same trace as the action that triggered it, preventing cross-test contamination during parallel execution.Prompt for AI Agent