Skip to content

test(cloudflare): Add E2E test that deploys a real Worker and sends to Sentry - #24280

Merged
JPeer264 merged 7 commits into
jp/test-utils-sentry-cli-helpersfrom
jp/cloudflare-e2e-send-to-sentry
Sep 18, 2026
Merged

JPeer264 merged 7 commits into
jp/test-utils-sentry-cli-helpersfrom
jp/cloudflare-e2e-send-to-sentry

Conversation

@JPeer264

@JPeer264 JPeer264 commented Sep 10, 2026

Copy link
Copy Markdown
Member

Adds cloudflare-workers-send-to-sentry, an optional E2E app that deploys a real Cloudflare Worker and verifies in Sentry that the SDK's data arrives, in the spirit of react-send-to-sentry. Local wrangler dev cannot reproduce what the platform does around waitUntil, isolate reuse, Durable Object hibernation and eviction. This app is the place for tests that need the real runtime.

The app is built with Vite, @cloudflare/vite-plugin and sentryCloudflareVitePlugin(), so the worker entry stays uninstrumented and the options live in src/instrument.server.ts via defineCloudflareOptions. Playwright's global setup deploys the Worker and waits for its workers.dev URL. The three tests cover a captured exception, an unhandled exception together with its request span, and a plain request span. They poll Sentry through @sentry-internal/test-utils/cli and log a paste-able sentry trace view command per trace.

Decisions:

  • One Worker per ref in CI, never deleted by the test run. Pull requests and merge-queue runs deploy to e2e-send-to-sentry-pr-<number>, pushes to e2e-send-to-sentry-<branch> (develop, master, v10). A run overwrites the Worker of its ref, so a failed run leaves its Worker in place for debugging, and Workers Logs are enabled for it. The new cleanup-e2e-workers.yml workflow deletes the PR Worker when the PR closes. The branch Workers stay. Local runs use a throwaway -local-<hex> name that the teardown deletes; E2E_KEEP_WORKER=1 keeps it.
  • Redeploying is always done, never skipped. A deploy takes about 10 s and the SDK tarball changes on almost every run.
  • The unhandled-error test sends baggage next to sentry-trace. The test has to pick the trace id itself, because a failed request cannot report ids. Without baggage the SDK freezes an empty dynamic sampling context (propagationContextFromHeaders in core, on purpose: "not head of trace"), and Relay drops the streamed span with the outcome missing_dsc. The error still arrives, only the request span is lost. This affects every server request whose upstream sends sentry-trace without baggage under span streaming.
  • Only CLOUDFLARE_ACCOUNT_ID is required by the setup. Wrangler authenticates with CLOUDFLARE_API_TOKEN in CI or a wrangler login session locally.

Repository setup: CLOUDFLARE_API_TOKEN (Workers Scripts: Edit, Workers KV Storage: Read) and CLOUDFLARE_ACCOUNT_ID point at the Sentry Dev account, and the optional E2E job passes both through.

Not in this PR: protecting the Worker URLs with Cloudflare Access and a service token. Worker-level Access rejects WebSocket upgrades, so it should be a hostname-based application per Worker, which needs "Access: Apps and Policies: Edit" on the API token.

Verified with real runs against the Sentry Dev account: all three tests pass in about 1.3 minutes, and the throwaway Worker is deleted afterwards.

Fixes #23610

🤖 Generated with Claude Code

@JPeer264
JPeer264 added this pull request to stack #24281 September 10, 2026 06:29
@github-actions

github-actions Bot commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 29.09 kB - -
@sentry/browser - with treeshaking flags 27.35 kB - -
@sentry/browser - with treeshaking flags tracing without tracing 27.26 kB - -
@sentry/browser (incl. Tracing) 50.6 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 50.62 kB - -
@sentry/browser (incl. Tracing, Profiling) 53.61 kB - -
@sentry/browser (incl. Tracing, Replay) 90.15 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 79.25 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 94.85 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 107.83 kB - -
@sentry/browser (incl. Feedback) 46.62 kB - -
@sentry/browser (incl. sendFeedback) 34.15 kB - -
@sentry/browser (incl. FeedbackAsync) 39.26 kB - -
@sentry/browser (incl. Metrics) 30.1 kB - -
@sentry/browser (incl. Logs) 30.35 kB - -
@sentry/browser (incl. Metrics & Logs) 31.02 kB - -
@sentry/react 30.84 kB - -
@sentry/react (incl. Tracing) 52.94 kB - -
@sentry/vue 36.34 kB - -
@sentry/vue (incl. Tracing) 52.91 kB - -
@sentry/svelte 29.11 kB - -
CDN Bundle 30.8 kB - -
CDN Bundle (incl. Tracing) 51.15 kB - -
CDN Bundle (incl. Logs, Metrics) 33.06 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 53.14 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 73.75 kB - -
CDN Bundle (incl. Tracing, Replay) 88.69 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 90.63 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 94.73 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 96.78 kB - -
CDN Bundle - uncompressed 91.16 kB - -
CDN Bundle (incl. Tracing) - uncompressed 152.66 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 97.73 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 158.61 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 227.14 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 272.23 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 278.17 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 285.93 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 291.86 kB - -
@sentry/nextjs (client) 55.27 kB - -
@sentry/sveltekit (client) 51.05 kB - -
@sentry/core/server 39.63 kB - -
@sentry/core/browser 13.66 kB - -
@sentry/node 132.37 kB +0.02% +16 B 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 82.03 kB - -
@sentry/node - without tracing 89.82 kB +0.04% +30 B 🔺
@sentry/node - without channel injection 111.23 kB +0.02% +20 B 🔺
@sentry/aws-serverless 98.06 kB +0.03% +25 B 🔺
@sentry/cloudflare (withSentry) - minified 204.52 kB -0.09% -169 B 🔽
@sentry/cloudflare (withSentry) 508.73 kB -0.11% -526 B 🔽

View base workflow run

@JPeer264 JPeer264 self-assigned this Sep 11, 2026
@JPeer264
JPeer264 marked this pull request as ready for review September 11, 2026 15:13
@JPeer264
JPeer264 force-pushed the jp/cloudflare-e2e-send-to-sentry branch from 11f110f to 5a1d0dc Compare September 11, 2026 15:13

@isaacs isaacs left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's some things I found that we could maybe harden a bit, but this is great!

Specifically: the decision is to deploy a real Worker from globalSetup and keep it afterwards, with naming as the cleanup strategy, means one Worker per ref, overwritten by the next run of that ref, so a red run leaves a live Worker with observability on for 7 days of logs. That is a good call. The alternative, deleting on completion, is what makes most deploy-based E2E suites just vanish into the ether when there's a problem we want to debug.

/** Deploys the worker under `name` and returns its workers.dev URL. */
export function deployWorker(name, dsn) {
const output = wrangler(['deploy', '--name', name, '--var', `E2E_TEST_DSN:${dsn}`]);
const url = output.match(/https:\/\/\S+\.workers\.dev/)?.[0];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parsing the worker url out of the human readable output seems a bit brittle.

We could set WRANGLER_OUTPUT_FILE_PATH to have it output ND-json data, and we'd get something like:

{"type":"deploy","version":1,"worker_name":"my-worker","version_id":"v1-abc123","targets":["https://my-worker.example.workers.dev"],...}

which can be parsed more deterministically.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that makes sense. I yeeted regex.

test('Sends an unhandled exception and its request span to Sentry', async () => {
// The worker cannot report ids for a request it fails, so the test picks the trace id and the
// SDK continues it from the incoming headers. Relay drops streamed spans of a trace without a
// dynamic sampling context, so `baggage` has to come along with `sentry-trace`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is the bit in the PR description where you said "This affects every server request whose upstream sends sentry-trace without baggage under span streaming and deserves its own issue", yes?

Seems like it'd be good to post that issue and provide a link to it? Otherwise feels like we're working around something and possibly ignoring it?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for that slop part. This is actually expected and was decided (within ingest) that we only accept incoming trace headers from us (that includes the baggage). I removed the comment and the part in the description.

return;
}

deleteWorker(workerName);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a little weird that this one lets a failure to delete just crash loudly. It's defensible, but in the setup, we treat a delete failure as just something to log and move on.

Suggestion: pick one. I would log and continue here in teardown too, and print the Worker name so a leak is actionable, because a cleanup failure is not a product failure. If we decide that leaks should be loud, then the error in global-setup.mjs should probably not swallow either.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is correct. I changed it to a console.error

- closed

jobs:
cleanup:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good, but we never clean up workers that get created from a local job. Maybe we could have a cron or something that cleans up any with -local in the name and are older than a few weeks, or something? Could be done as a followup, but worth an issue to make sure we don't forget it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm you're right. I thought about having also a nice experience locally, but forgot the teardown experience 🤔
Cron would be great, but I don't want to delete irrelevant - I'll create a follow up for this as this sounds like a great improvement, but wouldn't be harmful for now - as workers are "for free" if they don't run.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

follow up: #24459

@JPeer264 JPeer264 Sep 17, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually give me a second. It actually does delete it already, because the global-teardown.mjs is actually deleting the worker already locally immediately. We have a keepsWorker() protection that keeps the worker then one of the variables is set, but locally, by default, we don't have any of these so it will be deleted right away

(it would still be good to have this cron, just in case something is off)

JPeer264 and others added 6 commits September 17, 2026 14:29
…o Sentry

Local wrangler cannot reproduce what the platform does around waitUntil,
isolate reuse and Durable Object hibernation. The new optional E2E app
builds with the Sentry Vite plugin, deploys one Worker per ref in CI,
verifies errors and request spans in Sentry through the CLI helpers, and
deletes PR Workers through a new workflow when the PR closes.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…nd-to-sentry/global-setup.mjs

Co-authored-by: isaacs <i@izs.me>
…nd-to-sentry/package.json

Co-authored-by: isaacs <i@izs.me>
@JPeer264
JPeer264 force-pushed the jp/cloudflare-e2e-send-to-sentry branch from 445bb58 to e589a4d Compare September 17, 2026 12:29
while (Date.now() < deadline) {
try {
// The SDK does not trace HEAD requests, so the probe leaves no spans behind in Sentry.
const response = await fetch(url, { method: 'HEAD' });
…sends to Sentry

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@JPeer264
JPeer264 force-pushed the jp/cloudflare-e2e-send-to-sentry branch from 6c27d13 to cb25b55 Compare September 17, 2026 14:30
@JPeer264
JPeer264 merged commit db6a395 into develop Sep 18, 2026
325 checks passed
@JPeer264
JPeer264 deleted the jp/cloudflare-e2e-send-to-sentry branch September 18, 2026 09:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create E2E tests that use real Cloudflare Workers and send the data directly to Sentry

3 participants