@sentry/nextjs
Version:
Official Sentry SDK for Next.js
82 lines (78 loc) • 3.74 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const attributes = require('@sentry/conventions/attributes');
const core = require('@sentry/core');
const nextSpanAttributes = require('../nextSpanAttributes.js');
const tunnelPathnameMatch = require('./tunnelPathnameMatch.js');
const spanAttributesWithLogicAttached = require('../span-attributes-with-logic-attached.js');
const globalWithInjectedValues = core.GLOBAL_OBJ;
function dropMiddlewareTunnelRequests(span, attrs) {
if (core.getClient()?.getOptions()?.enableOpenTelemetrySetup === false) {
return;
}
const isMiddleware = attrs?.[nextSpanAttributes.ATTR_NEXT_SPAN_TYPE] === "Middleware.execute" || attrs?.[nextSpanAttributes.ATTR_NEXT_SPAN_TYPE] === "Next.request";
const isFetchSpan = attrs?.[attributes.SENTRY_ORIGIN] === "auto.http.node_fetch";
const isBaseServerHandleRequest = attrs?.[nextSpanAttributes.ATTR_NEXT_SPAN_TYPE] === "BaseServer.handleRequest";
if (!isMiddleware && !isFetchSpan && !isBaseServerHandleRequest) {
return;
}
const isTunnel = isTunnelRouteSpan(attrs || {});
const isSentry = isSentryRequestSpan(attrs || {});
if (isTunnel || isSentry) {
span.setAttribute(spanAttributesWithLogicAttached.TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION, true);
}
}
function isSentryRequestSpan(attrs) {
const httpUrl = attrs[attributes.URL_FULL];
if (!httpUrl) {
return false;
}
return core.isSentryRequestUrl(httpUrl.toString(), core.getClient());
}
function isTunnelRouteSpan(spanAttributes) {
const tunnelPath = globalWithInjectedValues._sentryRewritesTunnelPath || process.env._sentryRewritesTunnelPath;
if (!tunnelPath) {
return false;
}
const target = spanAttributes[attributes.URL_PATH] ?? spanAttributes[attributes.HTTP_TARGET];
if (typeof target === "string") {
const pathname = target.split("?")[0] || "";
return tunnelPathnameMatch.isPathnameUnderSentryTunnelRoute(pathname, tunnelPath);
}
return false;
}
function getTunnelRouteIgnoreSpans() {
const tunnelPath = globalWithInjectedValues._sentryRewritesTunnelPath || process.env._sentryRewritesTunnelPath;
if (!tunnelPath) {
return [];
}
const tunnelRouteMode = (globalWithInjectedValues._sentryTunnelRouteMode || process.env._sentryTunnelRouteMode) === "handler" ? "handler" : "rewrite";
const tunnelRequestIgnoreSpan = {
attributes: {
[nextSpanAttributes.ATTR_NEXT_SPAN_TYPE]: /^(?:BaseServer\.handleRequest|Middleware\.execute)$/,
// Next.js sets only `http.target` at span start.
// eslint-disable-next-line typescript/no-deprecated
[attributes.HTTP_TARGET]: tunnelPathnameMatch.getTunnelRouteRequestPattern(tunnelPath, tunnelRouteMode)
}
};
const proxyRootSpanIgnoreSpan = {
attributes: {
[nextSpanAttributes.ATTR_NEXT_SPAN_TYPE]: /^Next\.request$/,
// eslint-disable-next-line typescript/no-deprecated
[attributes.HTTP_TARGET]: new RegExp(`^${core.escapeStringForRegex(tunnelPath)}/?$`)
}
};
if (tunnelRouteMode !== "handler") {
return [tunnelRequestIgnoreSpan, proxyRootSpanIgnoreSpan];
}
const tunnelRouteHandlerIgnoreSpan = {
attributes: {
[nextSpanAttributes.ATTR_NEXT_SPAN_TYPE]: /^AppRouteRouteHandlers\.runHandler$/,
[nextSpanAttributes.ATTR_NEXT_ROUTE]: new RegExp(`^${core.escapeStringForRegex(tunnelPathnameMatch.TUNNEL_ROUTE_HANDLER_PATH)}(?:/route)?$`)
}
};
return [tunnelRequestIgnoreSpan, proxyRootSpanIgnoreSpan, tunnelRouteHandlerIgnoreSpan];
}
exports.dropMiddlewareTunnelRequests = dropMiddlewareTunnelRequests;
exports.getTunnelRouteIgnoreSpans = getTunnelRouteIgnoreSpans;
exports.isTunnelRouteSpan = isTunnelRouteSpan;
//# sourceMappingURL=dropMiddlewareTunnelRequests.js.map