UNPKG

@sentry/nextjs

Version:
150 lines (147 loc) • 7.81 kB
import { GLOBAL_OBJ, getVercelEnv, debug, _INTERNAL_getActiveClient, applySdkMetadata, getGlobalScope } from '@sentry/core'; import { getDefaultIntegrations, httpIntegration, init as init$1 } from '@sentry/node'; export * from '@sentry/node'; export { pinoIntegration, vercelAIIntegration } from '@sentry/node'; import { DEBUG_BUILD } from '../common/debug-build.js'; import { devErrorSymbolicationEventProcessor } from '../common/devErrorSymbolicationEventProcessor.js'; import { dropReactControlFlowErrorsEventProcessor } from '../common/utils/dropReactControlFlowErrors.js'; import { getTunnelRouteIgnoreSpans } from '../common/utils/dropMiddlewareTunnelRequests.js'; import { isBuild } from '../common/utils/isBuild.js'; import { isAsyncContextOwnedByCloudflare, setCloudflareWorkerRelease, isAsyncContextOwnedByNextjsCloudflare, isCloudflareWaitUntilAvailable } from '../common/utils/responseEnd.js'; import { distDirRewriteFramesIntegration } from './distDirRewriteFramesIntegration.js'; import { registerNextSentryPropagator } from './nextSentryPropagator.js'; export { NextSentryPropagator } from './nextSentryPropagator.js'; import { NEXTJS_SERVER_IGNORE_SPANS, addNextjsServerSpanHooks } from './serverSpanHooks.js'; import { prepareSafeIdGeneratorContext } from './prepareSafeIdGeneratorContext.js'; import { nextjsUseCacheIntegration } from './useCacheInstrumentation.js'; export { captureUnderscoreErrorException } from '../common/pages-router-instrumentation/_error.js'; export { startInactiveSpan, startSpan, startSpanManual } from '../common/utils/nextSpan.js'; export { wrapGetStaticPropsWithSentry } from '../common/pages-router-instrumentation/wrapGetStaticPropsWithSentry.js'; export { wrapGetInitialPropsWithSentry } from '../common/pages-router-instrumentation/wrapGetInitialPropsWithSentry.js'; export { wrapAppGetInitialPropsWithSentry } from '../common/pages-router-instrumentation/wrapAppGetInitialPropsWithSentry.js'; export { wrapDocumentGetInitialPropsWithSentry } from '../common/pages-router-instrumentation/wrapDocumentGetInitialPropsWithSentry.js'; export { wrapErrorGetInitialPropsWithSentry } from '../common/pages-router-instrumentation/wrapErrorGetInitialPropsWithSentry.js'; export { wrapGetServerSidePropsWithSentry } from '../common/pages-router-instrumentation/wrapGetServerSidePropsWithSentry.js'; export { wrapServerComponentWithSentry } from '../common/wrapServerComponentWithSentry.js'; export { wrapRouteHandlerWithSentry } from '../common/wrapRouteHandlerWithSentry.js'; export { wrapApiHandlerWithSentryVercelCrons } from '../common/pages-router-instrumentation/wrapApiHandlerWithSentryVercelCrons.js'; export { wrapMiddlewareWithSentry } from '../common/wrapMiddlewareWithSentry.js'; export { wrapPageComponentWithSentry } from '../common/pages-router-instrumentation/wrapPageComponentWithSentry.js'; export { wrapGenerationFunctionWithSentry } from '../common/wrapGenerationFunctionWithSentry.js'; export { withServerActionInstrumentation } from '../common/withServerActionInstrumentation.js'; export { captureRequestError } from '../common/captureRequestError.js'; export { wrapApiHandlerWithSentry } from '../common/pages-router-instrumentation/wrapApiHandlerWithSentry.js'; export { markPrerendersOnTracerProvider } from './prerenderContext.js'; const globalWithInjectedValues = GLOBAL_OBJ; prepareSafeIdGeneratorContext(); const ErrorBoundary = (props) => { if (!props.children) { return null; } if (typeof props.children === "function") { return props.children(); } return props.children; }; function createReduxEnhancer() { return (createStore) => createStore; } function withErrorBoundary(WrappedComponent) { return WrappedComponent; } function showReportDialog() { return; } function getCloudflareRuntimeConfig() { if (isCloudflareWaitUntilAvailable()) { return { runtime: { name: "cloudflare" } }; } return void 0; } function init(options) { prepareSafeIdGeneratorContext(); if (isBuild()) { return; } if (!DEBUG_BUILD && options.debug) { console.warn( "[@sentry/nextjs] You have enabled `debug: true`, but Sentry debug logging was removed from your bundle (likely via `bundleSizeOptimizations.excludeDebugStatements: true` or `webpack.treeshake.removeDebugLogging: true`). Set that option to `false` to see Sentry debug output." ); } const customDefaultIntegrations = getDefaultIntegrations(options).filter((integration) => integration.name !== "Http").concat( // We are using the HTTP integration without instrumenting incoming HTTP requests because Next.js does that by itself. httpIntegration({ disableIncomingRequestSpans: true }) ); if (options.enableOpenTelemetrySetup ?? true) { process.env.NEXT_OTEL_FETCH_DISABLED = "1"; } const distDirName = process.env._sentryRewriteFramesDistDir || globalWithInjectedValues._sentryRewriteFramesDistDir; if (distDirName) { customDefaultIntegrations.push(distDirRewriteFramesIntegration({ distDirName })); } customDefaultIntegrations.push(nextjsUseCacheIntegration()); const cloudflareConfig = getCloudflareRuntimeConfig(); const opts = { environment: options.environment || process.env.SENTRY_ENVIRONMENT || getVercelEnv() || process.env.NODE_ENV, release: process.env._sentryRelease || globalWithInjectedValues._sentryRelease, defaultIntegrations: customDefaultIntegrations, // Next.js emits its own OpenTelemetry spans, so it defaults to registering the Sentry tracer // provider (unlike most Node-based SDKs). A user-provided value still overrides this via `...options`. enableOpenTelemetrySetup: true, ...options, // Override runtime to 'cloudflare' when running on OpenNext/Cloudflare ...cloudflareConfig }; opts.ignoreSpans = [...opts.ignoreSpans || [], ...NEXTJS_SERVER_IGNORE_SPANS, ...getTunnelRouteIgnoreSpans()]; if (DEBUG_BUILD && opts.debug) { debug.enable(); } DEBUG_BUILD && debug.log("Initializing SDK..."); const isOwnedByCloudflare = isAsyncContextOwnedByCloudflare(); const existingClient = _INTERNAL_getActiveClient(); if (existingClient && !isOwnedByCloudflare) { DEBUG_BUILD && debug.log("SDK already initialized"); return existingClient; } applySdkMetadata(opts, "nextjs", ["nextjs", cloudflareConfig ? "cloudflare" : "node"]); const client = isOwnedByCloudflare ? void 0 : init$1(opts); if (client) { addNextjsServerSpanHooks(client); } else if (isOwnedByCloudflare) { setCloudflareWorkerRelease(opts.release); } if (client?.traceProvider) { registerNextSentryPropagator(client.traceProvider); } let eventProcessorTarget = client; if (isOwnedByCloudflare) { const globalScope = getGlobalScope(); const isAdded = globalScope.getScopeData().eventProcessors.some((processor) => processor.id === "DropReactControlFlowErrors"); eventProcessorTarget = isAdded ? void 0 : globalScope; } if (!isAsyncContextOwnedByNextjsCloudflare()) { eventProcessorTarget?.addEventProcessor(dropReactControlFlowErrorsEventProcessor); } if (process.env.NODE_ENV === "development") { eventProcessorTarget?.addEventProcessor(devErrorSymbolicationEventProcessor); } try { if (process.turbopack) { getGlobalScope().setTag("turbopack", true); getGlobalScope().setAttribute("turbopack", true); } } catch { } if (isOwnedByCloudflare) { DEBUG_BUILD && debug.log( "The client of `withSentry` handles this Worker, so `init` creates no client and only applies `release`. Set the other options in `withSentry`." ); } else { DEBUG_BUILD && debug.log("SDK successfully initialized"); } return client; } export { ErrorBoundary, createReduxEnhancer, init, showReportDialog, withErrorBoundary }; //# sourceMappingURL=index.js.map