UNPKG

@sentry/nextjs

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