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
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| 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
Uh oh!There was an error while loading. Please reload this page. | ||
| status: 500, | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| }); | ||
| } | ||
| } | ||
| return new Response('Step Context Test Worker'); | ||
| }, | ||
| } satisfies ExportedHandler<Env>, | ||
| ); | ||
| 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.
Uh oh!There was an error while loading. Please reload this page. | ||
| 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(); | ||
| }); | ||
| 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", | ||
| }, | ||
| ], | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| .wrangler |
| 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" | ||
| } | ||
| } |
| 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; |
| 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>, | ||
| ); |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.