-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(cloudflare): Send correct events in local development #19900
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/fix-local-cloudflare
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.
+389
−4
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions
1
dev-packages/e2e-tests/test-applications/cloudflare-local-workers/.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 |
2 changes: 2 additions & 0 deletions
2
dev-packages/e2e-tests/test-applications/cloudflare-local-workers/.npmrc
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,2 @@ | ||
| @sentry:registry=http://127.0.0.1:4873 | ||
| @sentry-internal:registry=http://127.0.0.1:4873 |
38 changes: 38 additions & 0 deletions
38
dev-packages/e2e-tests/test-applications/cloudflare-local-workers/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,38 @@ | ||
| { | ||
| "name": "cloudflare-local-workers", | ||
| "version": "0.0.0", | ||
| "private": true, | ||
| "scripts": { | ||
| "deploy": "wrangler deploy", | ||
| "dev": "wrangler dev --var \"E2E_TEST_DSN:$E2E_TEST_DSN\" --log-level=$(test $CI && echo 'none' || echo 'log')", | ||
| "build": "wrangler deploy --dry-run", | ||
| "test": "vitest --run", | ||
| "typecheck": "tsc --noEmit", | ||
| "cf-typegen": "wrangler types", | ||
| "test:build": "pnpm install && pnpm build", | ||
| "test:assert": "pnpm test:prod", | ||
| "test:prod": "TEST_ENV=production playwright test", | ||
| "test:dev": "TEST_ENV=development playwright test" | ||
| }, | ||
| "dependencies": { | ||
| "@sentry/cloudflare": "latest || *" | ||
| }, | ||
| "devDependencies": { | ||
| "@playwright/test": "~1.56.0", | ||
| "@cloudflare/vitest-pool-workers": "^0.8.19", | ||
| "@cloudflare/workers-types": "^4.20240725.0", | ||
| "@sentry-internal/test-utils": "link:../../../test-utils", | ||
| "typescript": "^5.5.2", | ||
| "vitest": "~3.2.0", | ||
| "wrangler": "^4.61.0", | ||
| "ws": "^8.18.3" | ||
| }, | ||
| "volta": { | ||
| "extends": "../../package.json" | ||
| }, | ||
| "pnpm": { | ||
| "overrides": { | ||
| "strip-literal": "~2.0.0" | ||
| } | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
dev-packages/e2e-tests/test-applications/cloudflare-local-workers/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,22 @@ | ||
| import { getPlaywrightConfig } from '@sentry-internal/test-utils'; | ||
| const testEnv = process.env.TEST_ENV; | ||
|
|
||
| if (!testEnv) { | ||
| throw new Error('No test env defined'); | ||
| } | ||
|
|
||
| const APP_PORT = 38787; | ||
|
|
||
| const config = getPlaywrightConfig( | ||
| { | ||
| startCommand: `pnpm dev --port ${APP_PORT}`, | ||
| port: APP_PORT, | ||
| }, | ||
| { | ||
| // This comes with the risk of tests leaking into each other but the tests run quite slow so we should parallelize | ||
| workers: '100%', | ||
| retries: 0, | ||
| }, | ||
| ); | ||
|
|
||
| export default config; |
7 changes: 7 additions & 0 deletions
7
dev-packages/e2e-tests/test-applications/cloudflare-local-workers/src/env.d.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,7 @@ | ||
| // Generated by Wrangler on Mon Jul 29 2024 21:44:31 GMT-0400 (Eastern Daylight Time) | ||
| // by running `wrangler types` | ||
|
|
||
| interface Env { | ||
| E2E_TEST_DSN: ''; | ||
| MY_DURABLE_OBJECT: DurableObjectNamespace; | ||
| } |
58 changes: 58 additions & 0 deletions
58
dev-packages/e2e-tests/test-applications/cloudflare-local-workers/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,58 @@ | ||
| import * as Sentry from '@sentry/cloudflare'; | ||
| import { DurableObject } from 'cloudflare:workers'; | ||
|
|
||
| class MyDurableObjectBase extends DurableObject<Env> { | ||
| async fetch(request: Request) { | ||
| const url = new URL(request.url); | ||
| switch (url.pathname) { | ||
| case '/storage/put': { | ||
| await this.ctx.storage.put('test-key', 'test-value'); | ||
| return new Response('Stored'); | ||
| } | ||
| case '/storage/get': { | ||
| const value = await this.ctx.storage.get('test-key'); | ||
| return new Response(`Got: ${value}`); | ||
| } | ||
| default: { | ||
| return new Response('Not found'); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export const MyDurableObject = Sentry.instrumentDurableObjectWithSentry( | ||
| (env: Env) => ({ | ||
| dsn: env.E2E_TEST_DSN, | ||
| environment: 'qa', // dynamic sampling bias to keep transactions | ||
| tunnel: `http://localhost:3031/`, // proxy server | ||
| tracesSampleRate: 1.0, | ||
| sendDefaultPii: true, | ||
| }), | ||
| MyDurableObjectBase, | ||
| ); | ||
|
|
||
| export default Sentry.withSentry( | ||
| (env: Env) => ({ | ||
| dsn: env.E2E_TEST_DSN, | ||
| environment: 'qa', // dynamic sampling bias to keep transactions | ||
| tunnel: `http://localhost:3031/`, // proxy server | ||
| tracesSampleRate: 1.0, | ||
| sendDefaultPii: true, | ||
| }), | ||
| { | ||
| async fetch(request, env) { | ||
| const url = new URL(request.url); | ||
|
|
||
| if (url.pathname.startsWith('/pass-to-object/')) { | ||
| const id = env.MY_DURABLE_OBJECT.idFromName('foo'); | ||
| const stub = env.MY_DURABLE_OBJECT.get(id) as DurableObjectStub<MyDurableObjectBase>; | ||
| url.pathname = url.pathname.replace('/pass-to-object/', ''); | ||
| const response = await stub.fetch(new Request(url, request)); | ||
| await new Promise(resolve => setTimeout(resolve, 500)); | ||
| return response; | ||
| } | ||
|
|
||
| return new Response('Hello World!'); | ||
| }, | ||
| } satisfies ExportedHandler<Env>, | ||
| ); |
6 changes: 6 additions & 0 deletions
6
dev-packages/e2e-tests/test-applications/cloudflare-local-workers/start-event-proxy.mjs
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,6 @@ | ||
| import { startEventProxyServer } from '@sentry-internal/test-utils'; | ||
|
|
||
| startEventProxyServer({ | ||
| port: 3031, | ||
| proxyServerName: 'cloudflare-local-workers', | ||
| }); |
33 changes: 33 additions & 0 deletions
33
dev-packages/e2e-tests/test-applications/cloudflare-local-workers/tests/index.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,33 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
| import { waitForTransaction } from '@sentry-internal/test-utils'; | ||
|
|
||
| /** | ||
| * This must be the only test in here. | ||
| * | ||
| * Both the Worker and the Durable Object initialize their own AsyncLocalStorage | ||
| * context. Wrangler dev is currently single-threaded locally, so when a previous | ||
| * test (e.g. a websocket test) already sets up ALS, that context carries over | ||
| * and masks bugs in our instrumentation - causing this test to pass when it | ||
| * should fail. | ||
| */ | ||
| test('Worker and Durable Object both send transactions when worker calls DO', async ({ baseURL }) => { | ||
| const workerTransactionPromise = waitForTransaction('cloudflare-local-workers', event => { | ||
| return event.transaction === 'GET /pass-to-object/storage/get' && event.contexts?.trace?.op === 'http.server'; | ||
| }); | ||
|
|
||
| const doTransactionPromise = waitForTransaction('cloudflare-local-workers', event => { | ||
| return event.transaction === 'GET /storage/get' && event.contexts?.trace?.op === 'http.server'; | ||
| }); | ||
|
|
||
| const response = await fetch(`${baseURL}/pass-to-object/storage/get`); | ||
| expect(response.status).toBe(200); | ||
|
|
||
| const [workerTransaction, doTransaction] = await Promise.all([workerTransactionPromise, doTransactionPromise]); | ||
|
|
||
| expect(workerTransaction.transaction).toBe('GET /pass-to-object/storage/get'); | ||
| expect(workerTransaction.contexts?.trace?.op).toBe('http.server'); | ||
|
|
||
| expect(doTransaction.transaction).toBe('GET /storage/get'); | ||
| expect(doTransaction.contexts?.trace?.op).toBe('http.server'); | ||
| expect(doTransaction.spans?.some(span => span.op === 'db')).toBe(true); | ||
| }); |
8 changes: 8 additions & 0 deletions
8
dev-packages/e2e-tests/test-applications/cloudflare-local-workers/tests/tsconfig.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,8 @@ | ||
| { | ||
| "extends": "../tsconfig.json", | ||
| "compilerOptions": { | ||
| "types": ["@cloudflare/vitest-pool-workers"] | ||
| }, | ||
| "include": ["./**/*.ts"], | ||
| "exclude": [] | ||
| } |
43 changes: 43 additions & 0 deletions
43
dev-packages/e2e-tests/test-applications/cloudflare-local-workers/tsconfig.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,43 @@ | ||
| { | ||
| "compilerOptions": { | ||
| /* Visit https://aka.ms/tsconfig.json to read more about this file */ | ||
|
|
||
| /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ | ||
| "target": "es2021", | ||
| /* Specify a set of bundled library declaration files that describe the target runtime environment. */ | ||
| "lib": ["es2021"], | ||
| /* Specify what JSX code is generated. */ | ||
| "jsx": "react-jsx", | ||
|
|
||
| /* Specify what module code is generated. */ | ||
| "module": "es2022", | ||
| /* Specify how TypeScript looks up a file from a given module specifier. */ | ||
| "moduleResolution": "Bundler", | ||
| /* Enable importing .json files */ | ||
| "resolveJsonModule": true, | ||
|
|
||
| /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */ | ||
| "allowJs": true, | ||
| /* Enable error reporting in type-checked JavaScript files. */ | ||
| "checkJs": false, | ||
|
|
||
| /* Disable emitting files from a compilation. */ | ||
| "noEmit": true, | ||
|
|
||
| /* Ensure that each file can be safely transpiled without relying on other imports. */ | ||
| "isolatedModules": true, | ||
| /* Allow 'import x from y' when a module doesn't have a default export. */ | ||
| "allowSyntheticDefaultImports": true, | ||
| /* Ensure that casing is correct in imports. */ | ||
| "forceConsistentCasingInFileNames": true, | ||
|
|
||
| /* Enable all strict type-checking options. */ | ||
| "strict": true, | ||
|
|
||
| /* Skip type checking all .d.ts files. */ | ||
| "skipLibCheck": true, | ||
| "types": ["@cloudflare/workers-types/experimental"] | ||
| }, | ||
| "exclude": ["test"], | ||
| "include": ["src/**/*.ts"] | ||
| } |
11 changes: 11 additions & 0 deletions
11
dev-packages/e2e-tests/test-applications/cloudflare-local-workers/vitest.config.mts
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,11 @@ | ||
| import { defineWorkersConfig } from '@cloudflare/vitest-pool-workers/config'; | ||
|
|
||
| export default defineWorkersConfig({ | ||
| test: { | ||
| poolOptions: { | ||
| workers: { | ||
| wrangler: { configPath: './wrangler.toml' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }); |
111 changes: 111 additions & 0 deletions
111
dev-packages/e2e-tests/test-applications/cloudflare-local-workers/wrangler.toml
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,111 @@ | ||
| #:schema node_modules/wrangler/config-schema.json | ||
| name = "cloudflare-local-workers" | ||
| main = "src/index.ts" | ||
| compatibility_date = "2024-07-25" | ||
| compatibility_flags = ["nodejs_compat"] | ||
|
|
||
| # [vars] | ||
| # E2E_TEST_DSN = "" | ||
|
|
||
| # Automatically place your workloads in an optimal location to minimize latency. | ||
| # If you are running back-end logic in a Worker, running it closer to your back-end infrastructure | ||
| # rather than the end user may result in better performance. | ||
| # Docs: https://developers.cloudflare.com/workers/configuration/smart-placement/#smart-placement | ||
| # [placement] | ||
| # mode = "smart" | ||
|
|
||
| # Variable bindings. These are arbitrary, plaintext strings (similar to environment variables) | ||
| # Docs: | ||
| # - https://developers.cloudflare.com/workers/wrangler/configuration/#environment-variables | ||
| # Note: Use secrets to store sensitive data. | ||
| # - https://developers.cloudflare.com/workers/configuration/secrets/ | ||
| # [vars] | ||
| # MY_VARIABLE = "production_value" | ||
|
|
||
| # Bind the Workers AI model catalog. Run machine learning models, powered by serverless GPUs, on Cloudflare’s global network | ||
| # Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#workers-ai | ||
| # [ai] | ||
| # binding = "AI" | ||
|
|
||
| # Bind an Analytics Engine dataset. Use Analytics Engine to write analytics within your Pages Function. | ||
| # Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#analytics-engine-datasets | ||
| # [[analytics_engine_datasets]] | ||
| # binding = "MY_DATASET" | ||
|
|
||
| # Bind a headless browser instance running on Cloudflare's global network. | ||
| # Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#browser-rendering | ||
| # [browser] | ||
| # binding = "MY_BROWSER" | ||
|
|
||
| # Bind a D1 database. D1 is Cloudflare’s native serverless SQL database. | ||
| # Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#d1-databases | ||
| # [[d1_databases]] | ||
| # binding = "MY_DB" | ||
| # database_name = "my-database" | ||
| # database_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
|
|
||
| # Bind a dispatch namespace. Use Workers for Platforms to deploy serverless functions programmatically on behalf of your customers. | ||
| # Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#dispatch-namespace-bindings-workers-for-platforms | ||
| # [[dispatch_namespaces]] | ||
| # binding = "MY_DISPATCHER" | ||
| # namespace = "my-namespace" | ||
|
|
||
| # Bind a Durable Object. Durable objects are a scale-to-zero compute primitive based on the actor model. | ||
| # Durable Objects can live for as long as needed. Use these when you need a long-running "server", such as in realtime apps. | ||
| # Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#durable-objects | ||
| [[durable_objects.bindings]] | ||
| name = "MY_DURABLE_OBJECT" | ||
| class_name = "MyDurableObject" | ||
|
|
||
| # Durable Object migrations. | ||
| # Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#migrations | ||
| [[migrations]] | ||
| tag = "v1" | ||
| new_sqlite_classes = ["MyDurableObject"] | ||
|
|
||
| # Bind a Hyperdrive configuration. Use to accelerate access to your existing databases from Cloudflare Workers. | ||
| # Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#hyperdrive | ||
| # [[hyperdrive]] | ||
| # binding = "MY_HYPERDRIVE" | ||
| # id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" | ||
|
|
||
| # Bind a KV Namespace. Use KV as persistent storage for small key-value pairs. | ||
| # Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#kv-namespaces | ||
| # [[kv_namespaces]] | ||
| # binding = "MY_KV_NAMESPACE" | ||
| # id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" | ||
|
|
||
| # Bind an mTLS certificate. Use to present a client certificate when communicating with another service. | ||
| # Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#mtls-certificates | ||
| # [[mtls_certificates]] | ||
| # binding = "MY_CERTIFICATE" | ||
| # certificate_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
|
|
||
| # Bind a Queue producer. Use this binding to schedule an arbitrary task that may be processed later by a Queue consumer. | ||
| # Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#queues | ||
| # [[queues.producers]] | ||
| # binding = "MY_QUEUE" | ||
| # queue = "my-queue" | ||
|
|
||
| # Bind a Queue consumer. Queue Consumers can retrieve tasks scheduled by Producers to act on them. | ||
| # Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#queues | ||
| # [[queues.consumers]] | ||
| # queue = "my-queue" | ||
|
|
||
| # Bind an R2 Bucket. Use R2 to store arbitrarily large blobs of data, such as files. | ||
| # Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#r2-buckets | ||
| # [[r2_buckets]] | ||
| # binding = "MY_BUCKET" | ||
| # bucket_name = "my-bucket" | ||
|
|
||
| # Bind another Worker service. Use this binding to call another Worker without network overhead. | ||
| # Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#service-bindings | ||
| # [[services]] | ||
| # binding = "MY_SERVICE" | ||
| # service = "my-service" | ||
|
|
||
| # Bind a Vectorize index. Use to store and query vector embeddings for semantic search, classification and other vector search use-cases. | ||
| # Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#vectorize-indexes | ||
| # [[vectorize]] | ||
| # binding = "MY_INDEX" | ||
| # index_name = "my-index" |
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
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.
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.
super-l: We could get of the optional chaining by moving the dispose call into the if block above. Feel free to disregard though