From b5e2e3fa8ddb88285be32ec424cd231cbcf44cf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Berg=C3=A9?= Date: Thu, 9 Jan 2025 09:40:47 +0100 Subject: [PATCH] Setup Sentry with Cloudflare --- packages/gitbook/package.json | 1 + packages/gitbook/src/cloudflare-entrypoint.ts | 26 ++++++++++++++----- packages/gitbook/src/instrumentation.ts | 15 ----------- packages/gitbook/src/middleware.ts | 3 --- packages/gitbook/src/sentry.ts | 11 -------- 5 files changed, 21 insertions(+), 35 deletions(-) delete mode 100644 packages/gitbook/src/instrumentation.ts delete mode 100644 packages/gitbook/src/sentry.ts diff --git a/packages/gitbook/package.json b/packages/gitbook/package.json index f06b980d7..be7b580db 100644 --- a/packages/gitbook/package.json +++ b/packages/gitbook/package.json @@ -25,6 +25,7 @@ "@gitbook/react-openapi": "workspace:*", "@radix-ui/react-checkbox": "^1.0.4", "@radix-ui/react-popover": "^1.0.7", + "@sentry/cloudflare": "8.35.0", "@sentry/nextjs": "8.35.0", "@sindresorhus/fnv1a": "^3.1.0", "@tailwindcss/container-queries": "^0.1.1", diff --git a/packages/gitbook/src/cloudflare-entrypoint.ts b/packages/gitbook/src/cloudflare-entrypoint.ts index 5625ce9b8..7134d8631 100644 --- a/packages/gitbook/src/cloudflare-entrypoint.ts +++ b/packages/gitbook/src/cloudflare-entrypoint.ts @@ -1,13 +1,10 @@ // @ts-ignore import nextOnPagesHandler from '@cloudflare/next-on-pages/fetch-handler'; +import { withSentry } from '@sentry/cloudflare'; import { withMiddlewareHeadersStorage } from './lib/middleware'; -/** - * We use a custom entrypoint until we can move to opennext (https://github.com/opennextjs/opennextjs-cloudflare/issues/92). - * There is a bug in next-on-pages where headers can't be set on the response in the middleware for RSC requests (https://github.com/cloudflare/next-on-pages/issues/897). - */ -export default { +const exportedHandler = { async fetch(request, env, ctx) { const response = await withMiddlewareHeadersStorage(() => nextOnPagesHandler.fetch(request, env, ctx), @@ -15,4 +12,21 @@ export default { return response; }, -} as ExportedHandler<{ ASSETS: Fetcher }>; +} satisfies ExportedHandler<{ ASSETS: Fetcher }>; + +const dsn = process.env.SENTRY_DSN; + +/** + * We use a custom entrypoint until we can move to opennext (https://github.com/opennextjs/opennextjs-cloudflare/issues/92). + * There is a bug in next-on-pages where headers can't be set on the response in the middleware for RSC requests (https://github.com/cloudflare/next-on-pages/issues/897). + */ +export default dsn + ? withSentry( + () => ({ + dsn, + tracesSampleRate: 0, + }), + // @ts-ignore + exportedHandler, + ) + : exportedHandler; diff --git a/packages/gitbook/src/instrumentation.ts b/packages/gitbook/src/instrumentation.ts deleted file mode 100644 index 1a346fbf7..000000000 --- a/packages/gitbook/src/instrumentation.ts +++ /dev/null @@ -1,15 +0,0 @@ -import * as Sentry from '@sentry/nextjs'; -import { Instrumentation } from 'next'; - -export async function register() { - console.log('Instrumentation.register'); - await import('./sentry'); -} - -export const onRequestError: Instrumentation.onRequestError = async (...args) => { - console.log('Instrumentation.onRequestError'); - Sentry.captureRequestError(...args); - // There is an issue on Cloudflare that requires us to flush the events manually. - // https://github.com/getsentry/sentry-javascript/issues/14931#issuecomment-2577640023 - await Sentry.flush(); -}; diff --git a/packages/gitbook/src/middleware.ts b/packages/gitbook/src/middleware.ts index ea2997afd..b3a21cf52 100644 --- a/packages/gitbook/src/middleware.ts +++ b/packages/gitbook/src/middleware.ts @@ -97,9 +97,6 @@ export type LookupResult = PublishedContentWithCache & { * The middleware also takes care of persisting the visitor authentication state. */ export async function middleware(request: NextRequest) { - // Instrumentation is not loaded correctly... - await import('./sentry'); - const { url, mode } = getInputURL(request); setTag('url', url.toString()); diff --git a/packages/gitbook/src/sentry.ts b/packages/gitbook/src/sentry.ts deleted file mode 100644 index 1ec6456ad..000000000 --- a/packages/gitbook/src/sentry.ts +++ /dev/null @@ -1,11 +0,0 @@ -import * as Sentry from '@sentry/nextjs'; - -const dsn = process.env.SENTRY_DSN; -if (dsn) { - console.log('Initializing Sentry with DSN:', dsn); - Sentry.init({ - dsn, - // Disable tracing as it creates additional requests in an env where subrequests are limited. - tracesSampleRate: 0, - }); -}