test(cloudflare): Add E2E test that deploys a real Worker and sends to Sentry - #24280
Conversation
size-limit report 📦
|
11f110f to
5a1d0dc
Compare
isaacs
left a comment
There was a problem hiding this comment.
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]; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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`. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
that is correct. I changed it to a console.error
| - closed | ||
|
|
||
| jobs: | ||
| cleanup: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
…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>
445bb58 to
e589a4d
Compare
| 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>
6c27d13 to
cb25b55
Compare
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 ofreact-send-to-sentry. Localwrangler devcannot reproduce what the platform does aroundwaitUntil, 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-pluginandsentryCloudflareVitePlugin(), so the worker entry stays uninstrumented and the options live insrc/instrument.server.tsviadefineCloudflareOptions. 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/cliand log a paste-ablesentry trace viewcommand per trace.Decisions:
e2e-send-to-sentry-pr-<number>, pushes toe2e-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 newcleanup-e2e-workers.ymlworkflow 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=1keeps it.baggagenext tosentry-trace. The test has to pick the trace id itself, because a failed request cannot report ids. Withoutbaggagethe SDK freezes an empty dynamic sampling context (propagationContextFromHeadersin core, on purpose: "not head of trace"), and Relay drops the streamed span with the outcomemissing_dsc. The error still arrives, only the request span is lost. This affects every server request whose upstream sendssentry-tracewithoutbaggageunder span streaming.CLOUDFLARE_ACCOUNT_IDis required by the setup. Wrangler authenticates withCLOUDFLARE_API_TOKENin CI or awrangler loginsession locally.Repository setup:
CLOUDFLARE_API_TOKEN(Workers Scripts: Edit, Workers KV Storage: Read) andCLOUDFLARE_ACCOUNT_IDpoint 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