(options: NodeOptions)
| 102 | /** Inits the Sentry NextJS SDK on node. */ |
| 103 | // eslint-disable-next-line complexity |
| 104 | export function init(options: NodeOptions): NodeClient | undefined { |
| 105 | prepareSafeIdGeneratorContext(); |
| 106 | if (isBuild()) { |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | if (!DEBUG_BUILD && options.debug) { |
| 111 | // eslint-disable-next-line no-console |
| 112 | console.warn( |
| 113 | '[@sentry/nextjs] You have enabled `debug: true`, but Sentry debug logging was removed from your bundle (likely via `withSentryConfig({ disableLogger: true })` / `webpack.treeshake.removeDebugLogging: true`). Set that option to `false` to see Sentry debug output.', |
| 114 | ); |
| 115 | } |
| 116 | |
| 117 | const customDefaultIntegrations = getDefaultIntegrations(options) |
| 118 | .filter(integration => integration.name !== 'Http') |
| 119 | .concat( |
| 120 | // We are using the HTTP integration without instrumenting incoming HTTP requests because Next.js does that by itself. |
| 121 | httpIntegration({ |
| 122 | disableIncomingRequestSpans: true, |
| 123 | }), |
| 124 | ); |
| 125 | |
| 126 | // Turn off Next.js' own fetch instrumentation (only when we manage OTEL) |
| 127 | // https://github.com/lforst/nextjs-fork/blob/1994fd186defda77ad971c36dc3163db263c993f/packages/next/src/server/lib/patch-fetch.ts#L245 |
| 128 | // Enable with custom OTel setup: https://github.com/getsentry/sentry-javascript/issues/17581 |
| 129 | if (!options.skipOpenTelemetrySetup) { |
| 130 | process.env.NEXT_OTEL_FETCH_DISABLED = '1'; |
| 131 | } |
| 132 | |
| 133 | // This value is injected at build time, based on the output directory specified in the build config. Though a default |
| 134 | // is set there, we set it here as well, just in case something has gone wrong with the injection. |
| 135 | const distDirName = process.env._sentryRewriteFramesDistDir || globalWithInjectedValues._sentryRewriteFramesDistDir; |
| 136 | if (distDirName) { |
| 137 | customDefaultIntegrations.push(distDirRewriteFramesIntegration({ distDirName })); |
| 138 | } |
| 139 | |
| 140 | // Detect if running on OpenNext/Cloudflare and get runtime config |
| 141 | const cloudflareConfig = getCloudflareRuntimeConfig(); |
| 142 | |
| 143 | const opts: NodeOptions = { |
| 144 | environment: options.environment || process.env.SENTRY_ENVIRONMENT || getVercelEnv(false) || process.env.NODE_ENV, |
| 145 | release: process.env._sentryRelease || globalWithInjectedValues._sentryRelease, |
| 146 | defaultIntegrations: customDefaultIntegrations, |
| 147 | ...options, |
| 148 | // Override runtime to 'cloudflare' when running on OpenNext/Cloudflare |
| 149 | ...cloudflareConfig, |
| 150 | }; |
| 151 | |
| 152 | const nextjsIgnoreSpans: NonNullable<NodeOptions['ignoreSpans']> = [ |
| 153 | // Static assets (matches `_next/static` anywhere in the name to handle custom basePath) |
| 154 | /^GET (\/.*)?\/_next\/static\//, |
| 155 | // Dev source-map fetch endpoints |
| 156 | /\/__nextjs_original-stack-frame/, |
| 157 | // Pages router /404 |
| 158 | /^\/404$/, |
| 159 | // App router /404 and /_not-found segments (any HTTP method) |
| 160 | /^(GET|HEAD|POST|PUT|DELETE|CONNECT|OPTIONS|TRACE|PATCH) \/(404|_not-found)$/, |
| 161 | // Next.js 13 root transactions named "NextServer.getRequestHandler" containing useless tracing |
no test coverage detected