diff --git a/src/instrumentation.ts b/src/instrumentation.ts index dcd08ba8f6cc0e..065e8c67713918 100644 --- a/src/instrumentation.ts +++ b/src/instrumentation.ts @@ -2,6 +2,43 @@ import * as Sentry from '@sentry/nextjs'; import {tracesSampler} from './tracesSampler'; +/** + * Filter for suppressing expected runtime MDX compilation errors. + * These occur when serverless functions attempt to compile MDX at runtime + * because source files are excluded from the bundle to stay under Vercel's 250MB limit. + * This is an infrastructure edge case that's already handled gracefully by returning 404. + * See: app/[[...path]]/page.tsx for graceful error handling. + */ +function isExpectedMdxRuntimeError(event: Sentry.ErrorEvent): boolean { + const exception = event.exception?.values?.[0]; + if (!exception) return false; + + const errorMessage = exception.value || ''; + const errorCode = (exception.mechanism?.data as Record)?.code || + (exception as Record).code; + + // Filter errors related to missing MDX files at runtime + const isMdxRuntimeError = + errorCode === 'MDX_RUNTIME_ERROR' || + (errorCode === 'ENOENT' && errorMessage.includes('Failed to find a valid source file')); + + // Filter errors from attempting to compile MDX when files aren't in bundle + const isMdxCompilationError = errorMessage.includes( + 'Failed to find a valid source file for slug' + ); + + return isMdxRuntimeError || isMdxCompilationError; +} + +function beforeSendEvent(event: Sentry.ErrorEvent) { + if (isExpectedMdxRuntimeError(event)) { + // Suppress this error from being reported to Sentry. + // The error is handled gracefully in page.tsx with notFound() response. + return null; + } + return event; +} + export function register() { if (process.env.NEXT_RUNTIME === 'nodejs') { Sentry.init({ @@ -12,6 +49,7 @@ export function register() { environment: process.env.NODE_ENV === 'development' ? 'development' : undefined, spotlight: process.env.NODE_ENV === 'development', integrations: [Sentry.consoleLoggingIntegration()], + beforeSendEvent, // Filter sensitive metric attributes (no PII in metrics) beforeSendMetric: metric => { @@ -36,6 +74,7 @@ export function register() { debug: false, environment: process.env.NODE_ENV === 'development' ? 'development' : undefined, integrations: [Sentry.consoleLoggingIntegration()], + beforeSendEvent, // Filter sensitive metric attributes (no PII in metrics) beforeSendMetric: metric => {