-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(cloudflare): Only capture workflow step error on final retry attempt #21025
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
Open
JPeer264
wants to merge
2
commits into
develop
Choose a base branch
from
jp/cloudflare-workflow-single-error
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
107 changes: 107 additions & 0 deletions
107
dev-packages/cloudflare-integration-tests/suites/workflows/step-context/index.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 |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import * as Sentry from '@sentry/cloudflare'; | ||
| import { WorkflowEntrypoint } from 'cloudflare:workers'; | ||
| import type { WorkflowEvent, WorkflowStep } from 'cloudflare:workers'; | ||
|
|
||
| interface Env { | ||
| SENTRY_DSN: string; | ||
| STEP_CONTEXT_WORKFLOW: Workflow; | ||
| } | ||
|
|
||
| interface WorkflowParams { | ||
| failCount: number; | ||
| captureManual?: boolean; | ||
| } | ||
| class StepContextTestWorkflowBase extends WorkflowEntrypoint<Env, WorkflowParams> { | ||
| async run(event: WorkflowEvent<WorkflowParams>, step: WorkflowStep): Promise<void> { | ||
| let remainingFailures = event.payload.failCount; | ||
|
|
||
| const result = await step.do( | ||
| 'failing-step', | ||
| { | ||
| retries: { | ||
| limit: 2, | ||
| delay: 100, | ||
| }, | ||
| }, | ||
| async ctx => { | ||
| if (event.payload.captureManual) { | ||
| Sentry.captureException(new Error(`Manual capture on attempt ${ctx.attempt}`)); | ||
| } | ||
|
|
||
| if (remainingFailures > 0) { | ||
| remainingFailures--; | ||
| throw new Error('Intentional failure for retry test'); | ||
| } | ||
| }, | ||
| ); | ||
|
|
||
| return result; | ||
| } | ||
| } | ||
|
|
||
| export const StepContextTestWorkflow = Sentry.instrumentWorkflowWithSentry( | ||
| (env: Env) => ({ | ||
| dsn: env.SENTRY_DSN, | ||
| }), | ||
| StepContextTestWorkflowBase, | ||
| ); | ||
|
|
||
| export default Sentry.withSentry( | ||
| (env: Env) => ({ | ||
| dsn: env.SENTRY_DSN, | ||
| }), | ||
| { | ||
| async fetch(request, env, _ctx) { | ||
| const url = new URL(request.url); | ||
|
|
||
| if (url.pathname === '/trigger-workflow') { | ||
| const failCount = parseInt(url.searchParams.get('failCount') || '0', 10); | ||
| const captureManual = url.searchParams.get('captureManual') === 'true'; | ||
|
|
||
| try { | ||
| const instance = await env.STEP_CONTEXT_WORKFLOW.create({ | ||
| params: { failCount, captureManual }, | ||
| }); | ||
|
|
||
| return new Response(JSON.stringify({ id: instance.id }), { headers: { 'Content-Type': 'application/json' } }); | ||
| } catch (e) { | ||
| return new Response(JSON.stringify({ error: 'Failed to create workflow', details: String(e) }), { | ||
| status: 500, | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| if (url.pathname === '/flush-marker') { | ||
| Sentry.captureMessage('flush-marker'); | ||
| return new Response(JSON.stringify({ ok: true }), { headers: { 'Content-Type': 'application/json' } }); | ||
| } | ||
|
|
||
| const statusMatch = url.pathname.match(/^\/workflow-status\/(.+)$/); | ||
|
|
||
| if (statusMatch) { | ||
| const workflowId = statusMatch[1]; | ||
|
|
||
| try { | ||
| const instance = await env.STEP_CONTEXT_WORKFLOW.get(workflowId!); | ||
| const status = await instance.status(); | ||
|
|
||
| return new Response( | ||
| JSON.stringify({ | ||
| id: workflowId, | ||
| status, | ||
| }), | ||
| { headers: { 'Content-Type': 'application/json' } }, | ||
| ); | ||
| } catch (e) { | ||
| return new Response(JSON.stringify({ error: 'Failed to get workflow status', details: String(e) }), { | ||
|
JPeer264 marked this conversation as resolved.
Dismissed
|
||
| status: 500, | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| return new Response('Step Context Test Worker'); | ||
| }, | ||
| } satisfies ExportedHandler<Env>, | ||
| ); | ||
109 changes: 109 additions & 0 deletions
109
dev-packages/cloudflare-integration-tests/suites/workflows/step-context/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 |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import type { Envelope } from '@sentry/core'; | ||
| import { expect, it } from 'vitest'; | ||
| import { createRunner } from '../../../runner'; | ||
|
|
||
| interface TriggerResponse { | ||
| id: string; | ||
| } | ||
|
|
||
| interface StatusResponse { | ||
| id: string; | ||
| status: { status: string }; | ||
| } | ||
|
|
||
| async function waitForWorkflowStatus( | ||
| makeRequest: <T>(method: 'get' | 'post', path: string) => Promise<T | undefined>, | ||
| workflowId: string, | ||
| ): Promise<StatusResponse | undefined> { | ||
| for (let i = 0; i < 30; i++) { | ||
| const status = await makeRequest<StatusResponse>('get', `/workflow-status/${workflowId}`); | ||
| if (status?.status?.status === 'errored' || status?.status?.status === 'complete') { | ||
| return status; | ||
| } | ||
| await new Promise(r => setTimeout(r, 200)); | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| const flushMarkerMatcher = (envelope: Envelope): void => { | ||
| const [, items] = envelope; | ||
| const [itemHeader, itemBody] = items[0] as [{ type: string }, Record<string, unknown>]; | ||
|
|
||
| expect(itemHeader.type).toBe('event'); | ||
| expect(itemBody.message).toBe('flush-marker'); | ||
| }; | ||
|
|
||
| it('With step context, only one error is captured on final retry attempt', async ({ signal }) => { | ||
| const runner = createRunner(__dirname) | ||
| .expect((envelope: Envelope): void => { | ||
| const [, items] = envelope; | ||
| const [itemHeader, itemBody] = items[0] as [{ type: string }, Record<string, unknown>]; | ||
|
|
||
| expect(itemHeader.type).toBe('event'); | ||
| expect(itemBody.exception).toBeDefined(); | ||
|
|
||
| const exception = itemBody.exception as { values?: Array<{ value?: string }> }; | ||
| expect(exception?.values?.[0]?.value).toBe('Intentional failure for retry test'); | ||
| }) | ||
| .expect(flushMarkerMatcher) | ||
| .start(signal); | ||
|
|
||
| const trigger = await runner.makeRequest<TriggerResponse>('get', '/trigger-workflow?failCount=3'); | ||
|
|
||
| expect(trigger?.id).toBeDefined(); | ||
|
|
||
| const status = await waitForWorkflowStatus(runner.makeRequest.bind(runner), trigger!.id); | ||
| expect(status?.status?.status).toBe('errored'); | ||
|
|
||
| await runner.makeRequest('get', '/flush-marker'); | ||
| await runner.completed(); | ||
| }); | ||
|
|
||
| it('No error event when step eventually succeeds within retry limit', async ({ signal }) => { | ||
| const runner = createRunner(__dirname).expect(flushMarkerMatcher).start(signal); | ||
|
|
||
| const trigger = await runner.makeRequest<TriggerResponse>('get', '/trigger-workflow?failCount=1'); | ||
| expect(trigger?.id).toBeDefined(); | ||
|
|
||
| const status = await waitForWorkflowStatus(runner.makeRequest.bind(runner), trigger!.id); | ||
| expect(status?.status?.status).toBe('complete'); | ||
|
|
||
| await runner.makeRequest('get', '/flush-marker'); | ||
| await runner.completed(); | ||
| }); | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| it('Manually captured exceptions are always sent on every attempt', async ({ signal }) => { | ||
| const runner = createRunner(__dirname) | ||
| .expectN(3, (envelope: Envelope): void => { | ||
| const [, items] = envelope; | ||
| const [itemHeader, itemBody] = items[0] as [{ type: string }, Record<string, unknown>]; | ||
|
|
||
| expect(itemHeader.type).toBe('event'); | ||
| expect(itemBody.exception).toBeDefined(); | ||
|
|
||
| const exception = itemBody.exception as { values?: Array<{ value?: string }> }; | ||
| expect(exception?.values?.[0]?.value).toMatch(/^Manual capture on attempt \d+$/); | ||
| }) | ||
| .expect((envelope: Envelope): void => { | ||
| const [, items] = envelope; | ||
| const [itemHeader, itemBody] = items[0] as [{ type: string }, Record<string, unknown>]; | ||
|
|
||
| expect(itemHeader.type).toBe('event'); | ||
| expect(itemBody.exception).toBeDefined(); | ||
|
|
||
| const exception = itemBody.exception as { values?: Array<{ value?: string }> }; | ||
| expect(exception?.values?.[0]?.value).toBe('Intentional failure for retry test'); | ||
| }) | ||
| .expect(flushMarkerMatcher) | ||
| .unordered() | ||
| .start(signal); | ||
|
|
||
| const trigger = await runner.makeRequest<TriggerResponse>('get', '/trigger-workflow?failCount=3&captureManual=true'); | ||
| expect(trigger?.id).toBeDefined(); | ||
|
|
||
| const status = await waitForWorkflowStatus(runner.makeRequest.bind(runner), trigger!.id); | ||
| expect(status?.status?.status).toBe('errored'); | ||
|
|
||
| await runner.makeRequest('get', '/flush-marker'); | ||
| await runner.completed(); | ||
| }); | ||
13 changes: 13 additions & 0 deletions
13
dev-packages/cloudflare-integration-tests/suites/workflows/step-context/wrangler.jsonc
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 |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| { | ||
| "name": "workflow-step-context-test", | ||
| "compatibility_date": "2026-05-01", | ||
| "main": "index.ts", | ||
| "compatibility_flags": ["nodejs_compat"], | ||
| "workflows": [ | ||
| { | ||
| "name": "step-context-test-workflow", | ||
| "binding": "STEP_CONTEXT_WORKFLOW", | ||
| "class_name": "StepContextTestWorkflow", | ||
| }, | ||
| ], | ||
| } |
1 change: 1 addition & 0 deletions
1
dev-packages/e2e-tests/test-applications/cloudflare-workers-workflow-legacy/.gitignore
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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| .wrangler |
27 changes: 27 additions & 0 deletions
27
dev-packages/e2e-tests/test-applications/cloudflare-workers-workflow-legacy/package.json
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 |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| { | ||
| "name": "cloudflare-workers-workflow-legacy", | ||
| "version": "0.0.0", | ||
| "private": true, | ||
| "scripts": { | ||
| "dev": "wrangler dev --var \"E2E_TEST_DSN:$E2E_TEST_DSN\" --log-level=$(test $CI && echo 'none' || echo 'log')", | ||
| "build": "wrangler deploy --dry-run", | ||
| "typecheck": "tsc --noEmit", | ||
| "test:build": "pnpm install && pnpm build", | ||
| "test:assert": "pnpm typecheck && pnpm test:dev", | ||
| "test:dev": "TEST_ENV=development playwright test" | ||
| }, | ||
| "dependencies": { | ||
| "@sentry/cloudflare": "file:../../packed/sentry-cloudflare-packed.tgz" | ||
| }, | ||
| "devDependencies": { | ||
| "@playwright/test": "~1.56.0", | ||
| "@cloudflare/workers-types": "^4.20260303.0", | ||
| "@sentry-internal/test-utils": "link:../../../test-utils", | ||
| "typescript": "^5.5.2", | ||
| "wrangler": "4.70.0" | ||
| }, | ||
| "volta": { | ||
| "node": "24.15.0", | ||
| "extends": "../../package.json" | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
...kages/e2e-tests/test-applications/cloudflare-workers-workflow-legacy/playwright.config.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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { getPlaywrightConfig } from '@sentry-internal/test-utils'; | ||
|
|
||
| const config = getPlaywrightConfig({ | ||
| startCommand: 'pnpm dev', | ||
| port: 8787, | ||
| eventProxyFile: 'start-event-proxy.mjs', | ||
| }); | ||
|
|
||
| export default config; |
91 changes: 91 additions & 0 deletions
91
dev-packages/e2e-tests/test-applications/cloudflare-workers-workflow-legacy/src/index.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 |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import * as Sentry from '@sentry/cloudflare'; | ||
| import { WorkflowEntrypoint } from 'cloudflare:workers'; | ||
| import type { WorkflowEvent, WorkflowStep } from 'cloudflare:workers'; | ||
|
|
||
| interface Env { | ||
| E2E_TEST_DSN: string; | ||
| RETRY_WORKFLOW: Workflow; | ||
| } | ||
|
|
||
| interface WorkflowParams { | ||
| failCount: number; | ||
| } | ||
|
|
||
| class RetryTestWorkflowBase extends WorkflowEntrypoint<Env, WorkflowParams> { | ||
| async run(event: WorkflowEvent<WorkflowParams>, step: WorkflowStep): Promise<string> { | ||
| let remainingFailures = event.payload.failCount; | ||
|
|
||
| await step.do( | ||
| 'failing-step', | ||
| { | ||
| retries: { | ||
| limit: 2, | ||
| delay: 100, | ||
| }, | ||
| }, | ||
| async () => { | ||
| if (remainingFailures > 0) { | ||
| remainingFailures--; | ||
| throw new Error('Intentional failure for retry test'); | ||
| } | ||
| return 'success'; | ||
| }, | ||
| ); | ||
|
|
||
| return 'workflow completed'; | ||
| } | ||
| } | ||
|
|
||
| export const RetryTestWorkflow = Sentry.instrumentWorkflowWithSentry( | ||
| (env: Env) => ({ | ||
| dsn: env.E2E_TEST_DSN, | ||
| tunnel: 'http://localhost:3031/', | ||
| }), | ||
| RetryTestWorkflowBase, | ||
| ); | ||
|
|
||
| export default Sentry.withSentry( | ||
| (env: Env) => ({ | ||
| dsn: env.E2E_TEST_DSN, | ||
| tunnel: 'http://localhost:3031/', | ||
| }), | ||
| { | ||
| async fetch(request, env, _ctx) { | ||
| const url = new URL(request.url); | ||
|
|
||
| if (url.pathname === '/flush-marker') { | ||
| Sentry.captureMessage('flush-marker'); | ||
| return new Response(JSON.stringify({ ok: true }), { headers: { 'Content-Type': 'application/json' } }); | ||
| } | ||
|
|
||
| if (url.pathname === '/trigger-workflow') { | ||
| const failCount = parseInt(url.searchParams.get('failCount') || '3', 10); | ||
|
|
||
| const instance = await env.RETRY_WORKFLOW.create({ | ||
| params: { failCount }, | ||
| }); | ||
|
|
||
| // Poll for workflow completion | ||
| for (let i = 0; i < 30; i++) { | ||
| try { | ||
| const status = await instance.status(); | ||
| if (status.status === 'complete' || status.status === 'errored') { | ||
| return new Response(JSON.stringify({ id: instance.id, status }), { | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| }); | ||
| } | ||
| } catch { | ||
| // status() may not be available yet | ||
| } | ||
| await new Promise(r => setTimeout(r, 500)); | ||
| } | ||
|
|
||
| return new Response(JSON.stringify({ id: instance.id, status: 'timeout' }), { | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| }); | ||
| } | ||
|
|
||
| return new Response('Workflow Legacy Test Worker'); | ||
| }, | ||
| } satisfies ExportedHandler<Env>, | ||
| ); |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.