UNPKG

@sentry/nextjs

Version:
187 lines (182 loc) • 11.6 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const core = require('@sentry/core'); const vercelEdge = require('@sentry/vercel-edge'); const debugBuild = require('../common/debug-build.js'); const nextSpanAttributes = require('../common/nextSpanAttributes.js'); const spanAttributesWithLogicAttached = require('../common/span-attributes-with-logic-attached.js'); const addHeadersAsAttributes = require('../common/utils/addHeadersAsAttributes.js'); const backfillHttpResponseStatusCode = require('../common/utils/backfillHttpResponseStatusCode.js'); const backfillHttpServerStatus = require('../common/utils/backfillHttpServerStatus.js'); const liveRootSpanAdapter = require('../common/utils/liveRootSpanAdapter.js'); const dropMiddlewareTunnelRequests = require('../common/utils/dropMiddlewareTunnelRequests.js'); const forkIsolationScopeForRootSpan = require('../common/utils/forkIsolationScopeForRootSpan.js'); const getNormalizedRequestFromAttributes = require('../common/utils/getNormalizedRequestFromAttributes.js'); const isBuild = require('../common/utils/isBuild.js'); const responseEnd = require('../common/utils/responseEnd.js'); const setUrlProcessingMetadata = require('../common/utils/setUrlProcessingMetadata.js'); const distDirRewriteFramesIntegration = require('./distDirRewriteFramesIntegration.js'); const enhanceMiddlewareRootSpan = require('../common/enhanceMiddlewareRootSpan.js'); const enhanceRunHandlerRootSpan = require('./enhanceRunHandlerRootSpan.js'); const attributes = require('@sentry/conventions/attributes'); const op = require('@sentry/conventions/op'); const wrapGetStaticPropsWithSentry = require('../common/pages-router-instrumentation/wrapGetStaticPropsWithSentry.js'); const wrapGetInitialPropsWithSentry = require('../common/pages-router-instrumentation/wrapGetInitialPropsWithSentry.js'); const wrapAppGetInitialPropsWithSentry = require('../common/pages-router-instrumentation/wrapAppGetInitialPropsWithSentry.js'); const wrapDocumentGetInitialPropsWithSentry = require('../common/pages-router-instrumentation/wrapDocumentGetInitialPropsWithSentry.js'); const wrapErrorGetInitialPropsWithSentry = require('../common/pages-router-instrumentation/wrapErrorGetInitialPropsWithSentry.js'); const wrapGetServerSidePropsWithSentry = require('../common/pages-router-instrumentation/wrapGetServerSidePropsWithSentry.js'); const wrapServerComponentWithSentry = require('../common/wrapServerComponentWithSentry.js'); const wrapRouteHandlerWithSentry = require('../common/wrapRouteHandlerWithSentry.js'); const wrapApiHandlerWithSentryVercelCrons = require('../common/pages-router-instrumentation/wrapApiHandlerWithSentryVercelCrons.js'); const wrapMiddlewareWithSentry = require('../common/wrapMiddlewareWithSentry.js'); const wrapPageComponentWithSentry = require('../common/pages-router-instrumentation/wrapPageComponentWithSentry.js'); const wrapGenerationFunctionWithSentry = require('../common/wrapGenerationFunctionWithSentry.js'); const withServerActionInstrumentation = require('../common/withServerActionInstrumentation.js'); const captureRequestError = require('../common/captureRequestError.js'); const _error = require('../common/pages-router-instrumentation/_error.js'); const pinoIntegrationShim = require('../common/pinoIntegrationShim.js'); const vercelAIIntegrationShim = require('../common/vercelAIIntegrationShim.js'); const nextSpan = require('../common/utils/nextSpan.js'); const wrapApiHandlerWithSentry = require('./wrapApiHandlerWithSentry.js'); const globalWithInjectedValues = core.GLOBAL_OBJ; function init(options = {}) { core.registerSpanErrorInstrumentation(); if (isBuild.isBuild()) { return; } if (!debugBuild.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 = vercelEdge.getDefaultIntegrations(); const distDirName = process.env._sentryRewriteFramesDistDir || globalWithInjectedValues._sentryRewriteFramesDistDir; if (distDirName) { customDefaultIntegrations.push(distDirRewriteFramesIntegration.distDirRewriteFramesIntegration({ distDirName })); } const isRunningOnCloudflare = responseEnd.isCloudflareWaitUntilAvailable(); const opts = { defaultIntegrations: customDefaultIntegrations, environment: options.environment || process.env.SENTRY_ENVIRONMENT, release: process.env._sentryRelease || globalWithInjectedValues._sentryRelease, ...options, // Override runtime to 'cloudflare' when running on OpenNext/Cloudflare ...isRunningOnCloudflare && { runtime: { name: "cloudflare" } } }; const nextjsIgnoreSpans = [ // (set in `dropMiddlewareTunnelRequests` during `spanStart`) { attributes: { [spanAttributesWithLogicAttached.TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION]: true } }, ...dropMiddlewareTunnelRequests.getTunnelRouteIgnoreSpans() ]; opts.ignoreSpans = [...opts.ignoreSpans || [], ...nextjsIgnoreSpans]; try { if (process.turbopack) { core.getGlobalScope().setTag("turbopack", true); core.getGlobalScope().setAttribute("turbopack", true); } } catch { } if (responseEnd.isAsyncContextOwnedByCloudflare()) { responseEnd.setCloudflareWorkerRelease(opts.release); debugBuild.DEBUG_BUILD && core.debug.log( "The client of `withSentry` handles this Worker, so `init` creates no client and only applies `release`. Set the other options in `withSentry`." ); return; } if (isRunningOnCloudflare) { core.applySdkMetadata(opts, "nextjs", ["nextjs", "cloudflare"]); } else { core.applySdkMetadata(opts, "nextjs", ["nextjs", "vercel-edge"]); } const client = vercelEdge.init(opts); client.on("beforeSampling", ({ spanAttributes }) => { const spanType = spanAttributes[nextSpanAttributes.ATTR_NEXT_SPAN_TYPE]; if (spanType !== "Middleware.execute" && spanType !== "BaseServer.handleRequest" && spanType !== "Next.request") { return; } const isolationScope = core.getIsolationScope(); isolationScope.setSDKProcessingMetadata({ normalizedRequest: void 0 }); const normalizedRequest = getNormalizedRequestFromAttributes.getNormalizedRequestFromAttributes(spanAttributes); if (normalizedRequest) { isolationScope.setSDKProcessingMetadata({ normalizedRequest }); } }); client.on("spanStart", (span) => { const spanAttributes = core.spanToJSON(span).attributes; const rootSpan = core.getRootSpan(span); const isRootSpan = span === rootSpan; dropMiddlewareTunnelRequests.dropMiddlewareTunnelRequests(span, spanAttributes); if (isRootSpan && spanAttributes?.[nextSpanAttributes.ATTR_NEXT_SPAN_TYPE] === "BaseServer.handleRequest" && core.hasSpanStreamingEnabled(client)) { const method = spanAttributes[attributes.HTTP_REQUEST_METHOD] ?? spanAttributes[attributes.HTTP_METHOD]; liveRootSpanAdapter.createLiveRootSpanAdapter(span).setName( (typeof method === "string" ? method.toUpperCase() : "") || core.HTTP_SPAN_NAME_FALLBACK ); } if (spanAttributes?.[nextSpanAttributes.ATTR_NEXT_SPAN_TYPE] !== void 0) { span.setAttribute(attributes.SENTRY_ORIGIN, "auto"); span.setAttribute(attributes.SENTRY_KIND, "server"); } if (spanAttributes?.[nextSpanAttributes.ATTR_NEXT_SPAN_TYPE] === "Middleware.execute") { span.setAttribute(attributes.SENTRY_OP, op.MIDDLEWARE); span.setAttribute(attributes.SENTRY_SEGMENT_NAME_SOURCE, "url"); } if (spanAttributes?.[nextSpanAttributes.ATTR_NEXT_SPAN_TYPE] === "Node.runHandler" && String(spanAttributes?.[nextSpanAttributes.ATTR_NEXT_SPAN_NAME]).startsWith(spanAttributesWithLogicAttached.ATTR_NEXT_PAGES_API_ROUTE_TYPE)) { span.setAttribute(attributes.SENTRY_OP, op.HTTP_SERVER); span.setAttribute(attributes.SENTRY_SEGMENT_NAME_SOURCE, "route"); } if (spanAttributes?.[nextSpanAttributes.ATTR_NEXT_SPAN_TYPE] === "BaseServer.handleRequest") { span.setAttribute(attributes.SENTRY_OP, op.HTTP_SERVER); } forkIsolationScopeForRootSpan.maybeForkIsolationScopeForRootSpan(span, spanAttributes); if (isRootSpan) { const headers = core.getIsolationScope().getScopeData().sdkProcessingMetadata?.normalizedRequest?.headers; addHeadersAsAttributes.addHeadersAsAttributes(headers, rootSpan); } }); client.on("preprocessEvent", (event) => { setUrlProcessingMetadata.setUrlProcessingMetadata(event); }); client.on("spanEnd", (span) => { if (span !== core.getRootSpan(span)) { return; } const mutableRootSpan = liveRootSpanAdapter.createLiveRootSpanAdapter(span); enhanceMiddlewareRootSpan.enhanceMiddlewareRootSpan(mutableRootSpan); enhanceRunHandlerRootSpan.enhanceRunHandlerRootSpan(mutableRootSpan); backfillHttpResponseStatusCode.backfillHttpResponseStatusCode(mutableRootSpan.attributes); backfillHttpServerStatus.backfillHttpServerStatus(span); responseEnd.waitUntil(responseEnd.flushSafelyWithTimeout()); }); } exports.wrapGetStaticPropsWithSentry = wrapGetStaticPropsWithSentry.wrapGetStaticPropsWithSentry; exports.wrapGetInitialPropsWithSentry = wrapGetInitialPropsWithSentry.wrapGetInitialPropsWithSentry; exports.wrapAppGetInitialPropsWithSentry = wrapAppGetInitialPropsWithSentry.wrapAppGetInitialPropsWithSentry; exports.wrapDocumentGetInitialPropsWithSentry = wrapDocumentGetInitialPropsWithSentry.wrapDocumentGetInitialPropsWithSentry; exports.wrapErrorGetInitialPropsWithSentry = wrapErrorGetInitialPropsWithSentry.wrapErrorGetInitialPropsWithSentry; exports.wrapGetServerSidePropsWithSentry = wrapGetServerSidePropsWithSentry.wrapGetServerSidePropsWithSentry; exports.wrapServerComponentWithSentry = wrapServerComponentWithSentry.wrapServerComponentWithSentry; exports.wrapRouteHandlerWithSentry = wrapRouteHandlerWithSentry.wrapRouteHandlerWithSentry; exports.wrapApiHandlerWithSentryVercelCrons = wrapApiHandlerWithSentryVercelCrons.wrapApiHandlerWithSentryVercelCrons; exports.wrapMiddlewareWithSentry = wrapMiddlewareWithSentry.wrapMiddlewareWithSentry; exports.wrapPageComponentWithSentry = wrapPageComponentWithSentry.wrapPageComponentWithSentry; exports.wrapGenerationFunctionWithSentry = wrapGenerationFunctionWithSentry.wrapGenerationFunctionWithSentry; exports.withServerActionInstrumentation = withServerActionInstrumentation.withServerActionInstrumentation; exports.captureRequestError = captureRequestError.captureRequestError; exports.captureUnderscoreErrorException = _error.captureUnderscoreErrorException; exports.pinoIntegration = pinoIntegrationShim.pinoIntegration; exports.vercelAIIntegration = vercelAIIntegrationShim.vercelAIIntegration; exports.startInactiveSpan = nextSpan.startInactiveSpan; exports.startSpan = nextSpan.startSpan; exports.startSpanManual = nextSpan.startSpanManual; exports.wrapApiHandlerWithSentry = wrapApiHandlerWithSentry.wrapApiHandlerWithSentry; exports.init = init; Object.prototype.hasOwnProperty.call(vercelEdge, '__proto__') && !Object.prototype.hasOwnProperty.call(exports, '__proto__') && Object.defineProperty(exports, '__proto__', { enumerable: true, value: vercelEdge['__proto__'] }); Object.keys(vercelEdge).forEach(function (k) { if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) exports[k] = vercelEdge[k]; }); //# sourceMappingURL=index.js.map