@sentry/nextjs
Version:
Official Sentry SDK for Next.js
87 lines (84 loc) • 3.57 kB
JavaScript
import { HTTP_ROUTE } from '@sentry/conventions/attributes';
import { isObjectLike, Scope, getClient, getActiveSpan, getRootSpan, spanToJSON, GLOBAL_OBJ, debug, startNewTrace, SEMANTIC_ATTRIBUTE_SENTRY_OP } from '@sentry/core';
import { DEBUG_BUILD } from '../debug-build.js';
import { ATTR_NEXT_SEGMENT, ATTR_NEXT_SPAN_TYPE, ATTR_NEXT_SPAN_NAME } from '../nextSpanAttributes.js';
import { TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION } from '../span-attributes-with-logic-attached.js';
const PAGE_SEGMENT = "__PAGE__";
const commonIsolationScopeMap = /* @__PURE__ */ new WeakMap();
function commonObjectToIsolationScope(commonObject) {
if (isObjectLike(commonObject)) {
const memoIsolationScope = commonIsolationScopeMap.get(commonObject);
if (memoIsolationScope) {
return memoIsolationScope;
} else {
const newIsolationScope = new Scope();
commonIsolationScopeMap.set(commonObject, newIsolationScope);
return newIsolationScope;
}
} else {
return new Scope();
}
}
let nextjsEscapedAsyncStorage;
function escapeNextjsTracing(cb) {
const MaybeGlobalAsyncLocalStorage = GLOBAL_OBJ.AsyncLocalStorage;
if (!MaybeGlobalAsyncLocalStorage) {
DEBUG_BUILD && debug.warn(
"Tried to register AsyncLocalStorage async context strategy in a runtime that doesn't support AsyncLocalStorage."
);
return cb();
}
if (!nextjsEscapedAsyncStorage) {
nextjsEscapedAsyncStorage = new MaybeGlobalAsyncLocalStorage();
}
if (nextjsEscapedAsyncStorage.getStore()) {
return cb();
} else {
return startNewTrace(() => {
return nextjsEscapedAsyncStorage.run(true, () => {
return cb();
});
});
}
}
function dropNextjsRootContext() {
if (getClient()?.getOptions()?.skipOpenTelemetrySetup) {
return;
}
const nextJsOwnedSpan = getActiveSpan();
if (nextJsOwnedSpan) {
const rootSpan = getRootSpan(nextJsOwnedSpan);
const rootSpanAttributes = spanToJSON(rootSpan).data;
if (rootSpanAttributes?.["next.span_type"]) {
getRootSpan(nextJsOwnedSpan)?.setAttribute(TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION, true);
}
}
}
function isResolveSegmentSpan(spanAttributes) {
return spanAttributes[ATTR_NEXT_SPAN_TYPE] === "NextNodeServer.getLayoutOrPageModule" && spanAttributes[ATTR_NEXT_SPAN_NAME] === "resolve segment modules" && typeof spanAttributes[ATTR_NEXT_SEGMENT] === "string";
}
function getEnhancedResolveSegmentSpanName({ segment, route }) {
if (segment === PAGE_SEGMENT) {
return `resolve page server component "${route}"`;
}
if (segment === "") {
return "resolve root layout server component";
}
return `resolve layout server component "${segment}"`;
}
function maybeEnhanceServerComponentSpanName(activeSpan, spanAttributes, rootSpanAttributes) {
if (!isResolveSegmentSpan(spanAttributes)) {
return;
}
const segment = spanAttributes[ATTR_NEXT_SEGMENT];
const route = rootSpanAttributes[HTTP_ROUTE];
const enhancedName = getEnhancedResolveSegmentSpanName({ segment, route: typeof route === "string" ? route : "" });
activeSpan.updateName(enhancedName);
activeSpan.setAttributes({
"sentry.nextjs.ssr.function.type": segment === PAGE_SEGMENT ? "Page" : "Layout",
"sentry.nextjs.ssr.function.route": route
});
activeSpan.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, "function.nextjs");
}
export { commonObjectToIsolationScope, dropNextjsRootContext, escapeNextjsTracing, getEnhancedResolveSegmentSpanName, isResolveSegmentSpan, maybeEnhanceServerComponentSpanName };
//# sourceMappingURL=tracingUtils.js.map