Repository navigation
feat(nuxt): Set up Cloudflare from instrument.server.ts
#26754
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
+1,011
−47
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
611e69a
feat(nuxt): Set up Cloudflare from `instrument.server.ts`
JPeer264 1e3c816
fixup! feat(nuxt): Set up Cloudflare from `instrument.server.ts`
JPeer264 fef93f8
fixup! feat(nuxt): Set up Cloudflare from `instrument.server.ts`
JPeer264 827ab43
fixup! feat(nuxt): Set up Cloudflare from `instrument.server.ts`
JPeer264 20c3ddf
fixup! feat(nuxt): Set up Cloudflare from `instrument.server.ts`
JPeer264 15c392c
fixup! feat(nuxt): Set up Cloudflare from `instrument.server.ts`
JPeer264 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
21 changes: 21 additions & 0 deletions
21
packages/nuxt/src/runtime/plugins/cloudflare-entry.server.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,21 @@ | ||
| import { getIsolationScope, stripUrlQueryAndFragment } from '@sentry/core'; | ||
| import type { H3Event } from 'h3'; | ||
| import type { NitroAppPlugin } from 'nitropack'; | ||
| import { markCloudflareEntryWrapped } from '../utils/cloudflare-entry'; | ||
|
|
||
| type H3V2Event = { req: { method: string }; url: { pathname: string } }; | ||
|
|
||
| export default (nitroApp => { | ||
| // This plugin runs before the plugins of the app, so a `sentryCloudflareNitroPlugin` there sees the flag. | ||
| markCloudflareEntryWrapped(); | ||
|
|
||
| // `withSentry` sets no transaction name, so errors captured during the request would have none. | ||
| // This is the same best-effort name the Node SDK sets when a request starts. | ||
| nitroApp.hooks.hook('request', (event: H3Event) => { | ||
| // h3 v1 (Nitro 2) has the method and the path on the event, h3 v2 (Nitro 3) on `event.req` and `event.url`. | ||
| const { req, url } = event as unknown as Partial<H3V2Event>; | ||
| const method = event.method ?? req?.method; | ||
| const path = event.path ?? url?.pathname; | ||
| getIsolationScope().setTransactionName(`${method} ${stripUrlQueryAndFragment(path)}`); | ||
| }); | ||
| }) satisfies NitroAppPlugin; | ||
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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| // oxlint-disable-next-line typescript/no-deprecated -- supported until removal | ||
| export { sentryCloudflareNitroPlugin } from './sentry-cloudflare.server'; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { GLOBAL_OBJ } from '@sentry/core'; | ||
|
|
||
| const CLOUDFLARE_ENTRY_WRAPPED_FLAG = '__SENTRY_NUXT_CLOUDFLARE_ENTRY_WRAPPED__'; | ||
|
|
||
| type GlobalWithCloudflareEntryFlag = typeof GLOBAL_OBJ & { [CLOUDFLARE_ENTRY_WRAPPED_FLAG]?: boolean }; | ||
|
|
||
| /** Records that the Sentry Nuxt module wrapped the Worker entry with `withSentry`. */ | ||
| export function markCloudflareEntryWrapped(): void { | ||
| (GLOBAL_OBJ as GlobalWithCloudflareEntryFlag)[CLOUDFLARE_ENTRY_WRAPPED_FLAG] = true; | ||
| } | ||
|
|
||
| /** Whether the Sentry Nuxt module wrapped the Worker entry with `withSentry`. */ | ||
| export function isCloudflareEntryWrapped(): boolean { | ||
| return (GLOBAL_OBJ as GlobalWithCloudflareEntryFlag)[CLOUDFLARE_ENTRY_WRAPPED_FLAG] === true; | ||
| } |
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 @@ | ||
| // `@cloudflare/workers-types` declares this module, but pulling it in would also add Workers globals | ||
| // that clash with the DOM lib the client half of this package is checked against. Only the export | ||
| // the SDK reads is declared here; the declaration is build-time only and never emitted. | ||
| declare module 'cloudflare:workers' { | ||
| // workerd versions from before March 2025 have no `withEnv`. | ||
| export const withEnv: (<T>(env: unknown, callback: () => T) => T) | undefined; | ||
| } |
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,75 @@ | ||
| import type { DurableObjectState, ExecutionContext, ExportedHandler } from '@cloudflare/workers-types'; | ||
| import type { CloudflareOptions } from '@sentry/cloudflare'; | ||
| import { instrumentDurableObjectWithSentry, withSentry } from '@sentry/cloudflare'; | ||
| import { applySdkMetadata } from '@sentry/core'; | ||
| // A namespace import keeps a missing `withEnv` export a missing property instead of a module linking error. | ||
| import * as cloudflareWorkers from 'cloudflare:workers'; | ||
|
|
||
| type OptionsCallback = (env: unknown) => CloudflareOptions; | ||
| type NitroDurableObjectClass = new (state: DurableObjectState, env: unknown) => object; | ||
| type Method = (this: unknown, ...args: unknown[]) => unknown; | ||
|
|
||
| /** | ||
| * Wraps the default export of the Nitro Worker entry with `withSentry`. Each handler runs with the `env` that | ||
| * `withSentry` instruments as the `env` of `cloudflare:workers`, so db0 of Nitro 3 reads instrumented D1 bindings. | ||
| */ | ||
| export function wrapNitroCloudflareEntryWithSentry<Handler extends ExportedHandler>( | ||
| optionsCallback: OptionsCallback, | ||
| handler: Handler, | ||
| ): Handler { | ||
| const handlerWithEnv = Object.fromEntries( | ||
| Object.entries(handler).map(([name, value]) => [ | ||
| name, | ||
| typeof value === 'function' | ||
| ? (event: unknown, env: unknown, context: ExecutionContext): unknown => | ||
| runWithEnv(env, () => (value as Method).call(handler, event, env, context)) | ||
| : value, | ||
| ]), | ||
| ) as Handler; | ||
|
|
||
| return withSentry(withNuxtSdkMetadata(optionsCallback), handlerWithEnv); | ||
| } | ||
|
|
||
| /** | ||
| * Wraps the Durable Object class of the Nitro Worker entry with `instrumentDurableObjectWithSentry`. Each method runs | ||
| * with the instrumented `this.env` as the `env` of `cloudflare:workers`. | ||
| */ | ||
| export function wrapNitroDurableObjectWithSentry( | ||
| optionsCallback: OptionsCallback, | ||
| NitroDurableObject: NitroDurableObjectClass, | ||
| ): NitroDurableObjectClass { | ||
| class NitroDurableObjectWithEnv extends NitroDurableObject {} | ||
|
|
||
| for (const name of Object.getOwnPropertyNames(NitroDurableObject.prototype)) { | ||
| const method: unknown = Object.getOwnPropertyDescriptor(NitroDurableObject.prototype, name)?.value; | ||
| if (name === 'constructor' || typeof method !== 'function') { | ||
| continue; | ||
| } | ||
|
|
||
| // Frameworks can read the name of a method, so the wrapper keeps it. | ||
| const { [name]: methodWithEnv } = { | ||
| [name](this: { env?: unknown }, ...args: unknown[]): unknown { | ||
| return runWithEnv(this.env, () => (method as Method).apply(this, args)); | ||
| }, | ||
| }; | ||
| Object.defineProperty(NitroDurableObjectWithEnv.prototype, name, { | ||
| value: methodWithEnv, | ||
| writable: true, | ||
| configurable: true, | ||
| }); | ||
| } | ||
|
|
||
| return instrumentDurableObjectWithSentry(withNuxtSdkMetadata(optionsCallback), NitroDurableObjectWithEnv); | ||
| } | ||
|
|
||
| function runWithEnv(env: unknown, callback: () => unknown): unknown { | ||
| return cloudflareWorkers.withEnv && env !== undefined ? cloudflareWorkers.withEnv(env, callback) : callback(); | ||
| } | ||
|
|
||
| function withNuxtSdkMetadata(optionsCallback: OptionsCallback): OptionsCallback { | ||
| return env => { | ||
| const options = { ...optionsCallback(env) }; | ||
| applySdkMetadata(options, 'nuxt', ['nuxt', 'cloudflare']); | ||
| return options; | ||
| }; | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.