@sentry/nextjs
Version:
Official Sentry SDK for Next.js
145 lines (142 loc) • 7.52 kB
JavaScript
import { context } from '@opentelemetry/api';
import { registerSpanErrorInstrumentation, GLOBAL_OBJ, applySdkMetadata, spanToJSON, getRootSpan, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, getCapturedScopesOnSpan, getIsolationScope, getCurrentScope, setCapturedScopesOnSpan, getGlobalScope } from '@sentry/core';
import { getScopesFromContext } from '@sentry/opentelemetry';
import { getDefaultIntegrations, init as init$1 } from '@sentry/vercel-edge';
export * from '@sentry/vercel-edge';
import { DEBUG_BUILD } from '../common/debug-build.js';
import { ATTR_NEXT_SPAN_TYPE } from '../common/nextSpanAttributes.js';
import { TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION } from '../common/span-attributes-with-logic-attached.js';
import { addHeadersAsAttributes } from '../common/utils/addHeadersAsAttributes.js';
import { dropMiddlewareTunnelRequests } from '../common/utils/dropMiddlewareTunnelRequests.js';
import { isBuild } from '../common/utils/isBuild.js';
import { isCloudflareWaitUntilAvailable, waitUntil, flushSafelyWithTimeout } from '../common/utils/responseEnd.js';
import { setUrlProcessingMetadata } from '../common/utils/setUrlProcessingMetadata.js';
import { distDirRewriteFramesIntegration } from './distDirRewriteFramesIntegration.js';
import { enhanceMiddlewareRootSpan } from '../common/enhanceMiddlewareRootSpan.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 { captureUnderscoreErrorException } from '../common/pages-router-instrumentation/_error.js';
export { pinoIntegration } from '../common/pinoIntegrationShim.js';
export { startInactiveSpan, startSpan, startSpanManual } from '../common/utils/nextSpan.js';
export { wrapApiHandlerWithSentry } from './wrapApiHandlerWithSentry.js';
const globalWithInjectedValues = GLOBAL_OBJ;
function init(options = {}) {
registerSpanErrorInstrumentation();
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 `withSentryConfig({ disableLogger: true })` / `webpack.treeshake.removeDebugLogging: true`). Set that option to `false` to see Sentry debug output."
);
}
const customDefaultIntegrations = getDefaultIntegrations(options);
const distDirName = process.env._sentryRewriteFramesDistDir || globalWithInjectedValues._sentryRewriteFramesDistDir;
if (distDirName) {
customDefaultIntegrations.push(distDirRewriteFramesIntegration({ distDirName }));
}
const isRunningOnCloudflare = 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: { [TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION]: true } }
];
opts.ignoreSpans = [...opts.ignoreSpans || [], ...nextjsIgnoreSpans];
if (isRunningOnCloudflare) {
applySdkMetadata(opts, "nextjs", ["nextjs", "cloudflare"]);
} else {
applySdkMetadata(opts, "nextjs", ["nextjs", "vercel-edge"]);
}
const client = init$1(opts);
client?.on("spanStart", (span) => {
const spanAttributes = spanToJSON(span).data;
const rootSpan = getRootSpan(span);
const isRootSpan = span === rootSpan;
dropMiddlewareTunnelRequests(span, spanAttributes);
if (spanAttributes?.["next.span_type"] !== void 0) {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, "auto");
}
if (spanAttributes?.["next.span_type"] === "Middleware.execute") {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, "http.server.middleware");
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, "url");
}
if (spanAttributes?.[ATTR_NEXT_SPAN_TYPE] === "BaseServer.handleRequest" && isRootSpan) {
const scopes = getCapturedScopesOnSpan(span);
const isolationScope = (scopes.isolationScope || getIsolationScope()).clone();
const scope = scopes.scope || getCurrentScope();
const currentScopesPointer = getScopesFromContext(context.active());
if (currentScopesPointer) {
currentScopesPointer.isolationScope = isolationScope;
}
setCapturedScopesOnSpan(span, scope, isolationScope);
}
if (isRootSpan) {
const headers = getIsolationScope().getScopeData().sdkProcessingMetadata?.normalizedRequest?.headers;
addHeadersAsAttributes(headers, rootSpan);
}
});
client?.on("preprocessEvent", (event) => {
if (event.type === "transaction" && event.contexts?.trace?.data) {
enhanceMiddlewareRootSpan({
attributes: event.contexts.trace.data,
getName: () => event.transaction,
setName: (name) => {
event.transaction = name;
},
setOp: (op) => {
event.contexts.trace.op = op;
}
});
}
setUrlProcessingMetadata(event);
});
client?.on("processSegmentSpan", (span) => {
const attributes = span.attributes ?? (span.attributes = {});
enhanceMiddlewareRootSpan({
attributes,
getName: () => span.name,
setName: (name) => {
span.name = name;
},
setOp: (op) => {
attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP] = op;
}
});
});
client?.on("spanEnd", (span) => {
if (span === getRootSpan(span)) {
waitUntil(flushSafelyWithTimeout());
}
});
try {
if (process.turbopack) {
getGlobalScope().setTag("turbopack", true);
getGlobalScope().setAttribute("turbopack", true);
}
} catch {
}
}
function withSentryConfig(exportedUserNextConfig) {
return exportedUserNextConfig;
}
export { init, withSentryConfig };
//# sourceMappingURL=index.js.map