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-agent to span streaming#23862
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-autoinstrument
from
jp/e2e-stream-cloudflare-agentSep 2, 2026
Uh oh!
There was an error while loading. Please reload this page.
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
27 changes: 18 additions & 9 deletions
27 dev-packages/e2e-tests/test-applications/cloudflare-agent/tests/ai-streaming.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
253 changes: 119 additions & 134 deletions
253 dev-packages/e2e-tests/test-applications/cloudflare-agent/tests/callable.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,192 +1,177 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
| import { waitForTransaction } from '@sentry-internal/test-utils'; | ||
| import { collectStreamedSpans, getSpanOp, waitForStreamedSpan } from '@sentry-internal/test-utils'; | ||
| // The agent request segment is the Durable Object's `http.server` span. It has a parent because | ||
| // the worker propagates its trace over the RPC binding; the worker's own segment for the same URL | ||
| // does not. With span streaming the name is the method only, so the segment is picked by `url.path`. | ||
| test('@callable() methods work correctly with Sentry instrumentAgentWithSentry', async ({ page, baseURL }) => { | ||
| const transactionPromise = waitForTransaction('cloudflare-agent', transactionEvent => { | ||
| return ( | ||
| transactionEvent.transaction === 'GET /agents/my-agent/user-123' && | ||
| transactionEvent.contexts?.trace?.parent_span_id !== undefined | ||
| ); | ||
| }); | ||
| const requestSpanPromise = waitForStreamedSpan( | ||
| 'cloudflare-agent', | ||
| span => | ||
| getSpanOp(span) === 'http.server' && | ||
| span.is_segment && | ||
| span.attributes['url.path']?.value === '/agents/my-agent/user-123' && | ||
| span.parent_span_id !== undefined, | ||
| ); | ||
| // The greet() call goes over the websocket, so its storage spans land in a webSocketMessage | ||
| // transaction. Filter for the one carrying our put span — control messages produce their own | ||
| // webSocketMessage transactions without storage spans. | ||
| const storageTransactionPromise = waitForTransaction('cloudflare-agent', transactionEvent => { | ||
| return ( | ||
| transactionEvent.transaction === 'webSocketMessage' && | ||
| (transactionEvent.spans ?? []).some(span => span.description === 'durable_object_storage_put') | ||
| ); | ||
| }); | ||
| // The greet() call goes over the websocket, so its storage spans are children of the `greet` rpc | ||
| // span inside a webSocketMessage segment. Control messages produce their own webSocketMessage | ||
| // segments without storage spans. Streamed children arrive before their segment, so collect until | ||
| // the segment that closes the trace of a `greet` call has arrived. | ||
| const storageSpansPromise = collectStreamedSpans( | ||
| 'cloudflare-agent', | ||
| spans => | ||
| spans.some(span => getSpanOp(span) === 'rpc' && span.name === 'greet') && | ||
| spans.some(span => span.is_segment && span.name === 'webSocketMessage'), | ||
| ); | ||
| await page.goto(baseURL!); | ||
| await expect(page.getByText('Connected')).toBeVisible(); | ||
| await page.getByRole('button', { name: 'Call Agent' }).click(); | ||
| await expect(page.getByText('Hello, World!')).toBeVisible(); | ||
| const transaction = await transactionPromise; | ||
| expect(transaction).toEqual({ | ||
| contexts: { | ||
| trace: { | ||
| parent_span_id: expect.stringMatching(/[a-f0-9]{16}/), | ||
| span_id: expect.stringMatching(/[a-f0-9]{16}/), | ||
| trace_id: expect.stringMatching(/[a-f0-9]{32}/), | ||
| data: expect.any(Object), | ||
| op: 'http.server', | ||
| status: 'ok', | ||
| origin: 'auto.http.cloudflare', | ||
| }, | ||
| cloud_resource: { 'cloud.provider': 'cloudflare' }, | ||
| culture: { timezone: expect.any(String) }, | ||
| runtime: { name: 'cloudflare' }, | ||
| }, | ||
| spans: [], | ||
| const requestSpan = await requestSpanPromise; | ||
| expect(requestSpan).toEqual({ | ||
| trace_id: expect.stringMatching(/[a-f0-9]{32}/), | ||
| span_id: expect.stringMatching(/[a-f0-9]{16}/), | ||
| parent_span_id: expect.stringMatching(/[a-f0-9]{16}/), | ||
| name: 'GET', | ||
| start_timestamp: expect.any(Number), | ||
| timestamp: expect.any(Number), | ||
| transaction: 'GET /agents/my-agent/user-123', | ||
| type: 'transaction', | ||
| request: { | ||
| cookies: {}, | ||
| headers: expect.any(Object), | ||
| method: 'GET', | ||
| url: expect.stringContaining('/agents/my-agent/user-123'), | ||
| query_string: expect.any(String), | ||
| }, | ||
| transaction_info: { source: 'url' }, | ||
| platform: 'javascript', | ||
| event_id: expect.stringMatching(/[a-f0-9]{32}/), | ||
| environment: expect.any(String), | ||
| user: { | ||
| ip_address: '127.0.0.1', | ||
| }, | ||
| release: expect.any(String), | ||
| sdk: { | ||
| integrations: expect.any(Array), | ||
| name: 'sentry.javascript.cloudflare', | ||
| version: expect.any(String), | ||
| packages: expect.any(Array), | ||
| }, | ||
| end_timestamp: expect.any(Number), | ||
| status: 'ok', | ||
| is_segment: true, | ||
| attributes: expect.objectContaining({ | ||
| 'sentry.op': { value: 'http.server', type: 'string' }, | ||
| 'sentry.origin': { value: 'auto.http.cloudflare', type: 'string' }, | ||
| 'sentry.segment.name.source': { value: 'url', type: 'string' }, | ||
| 'http.request.method': { value: 'GET', type: 'string' }, | ||
| 'url.path': { value: '/agents/my-agent/user-123', type: 'string' }, | ||
| 'sentry.environment': { value: expect.any(String), type: 'string' }, | ||
| }), | ||
| }); | ||
| // greet() touches 6 storage keys: 2 user ops + 3 framework-internal keys (cf_, __ps_, /) that | ||
| // must be filtered + 1 allowlisted cf_ key. Spans carry no key attribute, so filtering can only | ||
| // be verified by count — exactly these 3 storage spans (in execution order) should survive, and | ||
| // any framework-internal span leaking through shows up as an extra entry here. | ||
| const storageTransaction = await storageTransactionPromise; | ||
| const spans = await storageSpansPromise; | ||
| const rpcSpan = spans.find(span => getSpanOp(span) === 'rpc' && span.name === 'greet')!; | ||
| const storageSpans = (storageTransaction.spans ?? []).filter( | ||
| span => span.origin === 'auto.db.cloudflare.durable_object', | ||
| ); | ||
| const storageSpans = spans | ||
| .filter( | ||
| span => | ||
| span.parent_span_id === rpcSpan.span_id && | ||
| span.attributes['sentry.origin']?.value === 'auto.db.cloudflare.durable_object', | ||
| ) | ||
| .sort((a, b) => a.start_timestamp - b.start_timestamp); | ||
| expect(storageSpans).toEqual([ | ||
| expect.objectContaining({ | ||
| data: { | ||
| 'db.operation.name': 'put', | ||
| 'db.system.name': 'cloudflare.durable_object.storage', | ||
| 'sentry.op': 'db', | ||
| 'sentry.origin': 'auto.db.cloudflare.durable_object', | ||
| }, | ||
| description: 'durable_object_storage_put', | ||
| op: 'db', | ||
| origin: 'auto.db.cloudflare.durable_object', | ||
| parent_span_id: expect.stringMatching(/[a-f0-9]{16}/), | ||
| span_id: expect.stringMatching(/[a-f0-9]{16}/), | ||
| { | ||
| name: 'durable_object_storage_put', | ||
| attributes: expect.objectContaining({ | ||
| 'db.operation.name': { value: 'put', type: 'string' }, | ||
| 'db.system.name': { value: 'cloudflare.durable_object.storage', type: 'string' }, | ||
| 'sentry.op': { value: 'db', type: 'string' }, | ||
| 'sentry.origin': { value: 'auto.db.cloudflare.durable_object', type: 'string' }, | ||
| }), | ||
| parent_span_id: rpcSpan.span_id, | ||
| span_id: expect.stringMatching(/^[a-f0-9]{16}$/), | ||
| start_timestamp: expect.any(Number), | ||
| timestamp: expect.any(Number), | ||
| trace_id: expect.stringMatching(/[a-f0-9]{32}/), | ||
| }), | ||
| expect.objectContaining({ | ||
| data: { | ||
| 'db.operation.name': 'get', | ||
| 'db.system.name': 'cloudflare.durable_object.storage', | ||
| 'sentry.op': 'db', | ||
| 'sentry.origin': 'auto.db.cloudflare.durable_object', | ||
| }, | ||
| description: 'durable_object_storage_get', | ||
| op: 'db', | ||
| origin: 'auto.db.cloudflare.durable_object', | ||
| parent_span_id: expect.stringMatching(/[a-f0-9]{16}/), | ||
| span_id: expect.stringMatching(/[a-f0-9]{16}/), | ||
| end_timestamp: expect.any(Number), | ||
| status: 'ok', | ||
| is_segment: false, | ||
| trace_id: rpcSpan.trace_id, | ||
| }, | ||
| { | ||
| name: 'durable_object_storage_get', | ||
| attributes: expect.objectContaining({ | ||
| 'db.operation.name': { value: 'get', type: 'string' }, | ||
| 'db.system.name': { value: 'cloudflare.durable_object.storage', type: 'string' }, | ||
| 'sentry.op': { value: 'db', type: 'string' }, | ||
| 'sentry.origin': { value: 'auto.db.cloudflare.durable_object', type: 'string' }, | ||
| }), | ||
| parent_span_id: rpcSpan.span_id, | ||
| span_id: expect.stringMatching(/^[a-f0-9]{16}$/), | ||
| start_timestamp: expect.any(Number), | ||
| timestamp: expect.any(Number), | ||
| trace_id: expect.stringMatching(/[a-f0-9]{32}/), | ||
| }), | ||
| expect.objectContaining({ | ||
| data: { | ||
| 'db.operation.name': 'get', | ||
| 'db.system.name': 'cloudflare.durable_object.storage', | ||
| 'sentry.op': 'db', | ||
| 'sentry.origin': 'auto.db.cloudflare.durable_object', | ||
| }, | ||
| description: 'durable_object_storage_get', | ||
| op: 'db', | ||
| origin: 'auto.db.cloudflare.durable_object', | ||
| parent_span_id: expect.stringMatching(/[a-f0-9]{16}/), | ||
| span_id: expect.stringMatching(/[a-f0-9]{16}/), | ||
| end_timestamp: expect.any(Number), | ||
| status: 'ok', | ||
| is_segment: false, | ||
| trace_id: rpcSpan.trace_id, | ||
| }, | ||
| { | ||
| name: 'durable_object_storage_get', | ||
| attributes: expect.objectContaining({ | ||
| 'db.operation.name': { value: 'get', type: 'string' }, | ||
| 'db.system.name': { value: 'cloudflare.durable_object.storage', type: 'string' }, | ||
| 'sentry.op': { value: 'db', type: 'string' }, | ||
| 'sentry.origin': { value: 'auto.db.cloudflare.durable_object', type: 'string' }, | ||
| }), | ||
| parent_span_id: rpcSpan.span_id, | ||
| span_id: expect.stringMatching(/^[a-f0-9]{16}$/), | ||
| start_timestamp: expect.any(Number), | ||
| timestamp: expect.any(Number), | ||
| trace_id: expect.stringMatching(/[a-f0-9]{32}/), | ||
| }), | ||
| end_timestamp: expect.any(Number), | ||
| status: 'ok', | ||
| is_segment: false, | ||
| trace_id: rpcSpan.trace_id, | ||
| }, | ||
| ]); | ||
| }); | ||
| test('does not emit db.query spans for the agents runtime `cf_`-prefixed internal tables', async ({ | ||
| page, | ||
| baseURL, | ||
| }) => { | ||
| const transactionPromise = waitForTransaction('cloudflare-agent', transactionEvent => { | ||
| return ( | ||
| transactionEvent.transaction === 'GET /agents/my-agent/user-123' && | ||
| transactionEvent.contexts?.trace?.parent_span_id !== undefined | ||
| ); | ||
| }); | ||
| const spansPromise = collectStreamedSpans('cloudflare-agent', spans => | ||
| spans.some( | ||
| span => | ||
| getSpanOp(span) === 'http.server' && | ||
| span.is_segment && | ||
| span.attributes['url.path']?.value === '/agents/my-agent/user-123' && | ||
| span.parent_span_id !== undefined, | ||
| ), | ||
| ); | ||
| await page.goto(baseURL!); | ||
| await expect(page.getByText('Connected')).toBeVisible(); | ||
| await page.getByRole('button', { name: 'Call Agent' }).click(); | ||
| await expect(page.getByText('Hello, World!')).toBeVisible(); | ||
| const transaction = await transactionPromise; | ||
| const spans = await spansPromise; | ||
| // The agents runtime constantly queries its own `cf_agents_*` / `cf_agent_*` bookkeeping tables. | ||
| // These are framework internals and are filtered out by default, so no such span should leak. | ||
| const internalTableSpans = (transaction.spans ?? []).filter( | ||
| span => span.op === 'db.query' && /\bcf_/.test((span.data?.['db.query.summary'] as string) ?? ''), | ||
| const internalTableSpans = spans.filter( | ||
| span => getSpanOp(span) === 'db.query' && /\bcf_/.test(String(span.attributes['db.query.summary']?.value ?? '')), | ||
| ); | ||
| expect(internalTableSpans).toEqual([]); | ||
| }); | ||
| test('creates an rpc span named after the @callable() method', async ({ page, baseURL }) => { | ||
| const transactionPromise = waitForTransaction('cloudflare-agent', transactionEvent => { | ||
| return ( | ||
| transactionEvent.transaction === 'webSocketMessage' && | ||
| (transactionEvent.spans ?? []).some(span => span.op === 'rpc' && span.description === 'greet') | ||
| ); | ||
| }); | ||
| const spansPromise = collectStreamedSpans( | ||
| 'cloudflare-agent', | ||
| spans => | ||
| spans.some(span => getSpanOp(span) === 'rpc' && span.name === 'greet') && | ||
| spans.some(span => span.is_segment && span.name === 'webSocketMessage'), | ||
| ); | ||
| await page.goto(baseURL!); | ||
| await expect(page.getByText('Connected')).toBeVisible(); | ||
| await page.getByRole('button', { name: 'Call Agent' }).click(); | ||
| await expect(page.getByText('Hello, World!')).toBeVisible(); | ||
| const transaction = await transactionPromise; | ||
| const spans = await spansPromise; | ||
| const rpcSpan = spans.find(span => getSpanOp(span) === 'rpc' && span.name === 'greet')!; | ||
| const rpcSpans = (transaction.spans ?? []).filter(span => span.op === 'rpc'); | ||
| const rpcSpans = spans.filter(span => getSpanOp(span) === 'rpc'); | ||
| expect(rpcSpans).toHaveLength(1); | ||
| expect(rpcSpans[0]).toEqual( | ||
| expect.objectContaining({ | ||
| op: 'rpc', | ||
| description: 'greet', | ||
| origin: 'auto.faas.cloudflare.agents', | ||
| data: expect.objectContaining({ | ||
| 'gen_ai.agent.name': 'MyBaseAgent', | ||
| }), | ||
| }), | ||
| ); | ||
| expect(rpcSpan.attributes['sentry.op']?.value).toBe('rpc'); | ||
| expect(rpcSpan.attributes['sentry.origin']?.value).toBe('auto.faas.cloudflare.agents'); | ||
| expect(rpcSpan.attributes['gen_ai.agent.name']?.value).toBe('MyBaseAgent'); | ||
| }); | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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 test for internal
db.queryspans is ineffective because it monitors a trace that is unlikely to contain these spans, causing the test to always pass.Severity: LOW
Suggested Fix
The test should be redesigned to capture spans from multiple, potentially unrelated traces simultaneously. Instead of resolving
collectStreamedSpanson a single trace's predicate, consider a mechanism that can observe all spans received by the event proxy over a period of time, regardless of theirtrace_id, and then perform the assertion.Prompt for AI Agent
Did we get this right? 👍 / 👎 to inform future reviews.