@sentry/nextjs
Version:
Official Sentry SDK for Next.js
50 lines (47 loc) • 2.59 kB
JavaScript
import { SENTRY_ORIGIN, SENTRY_OP, HTTP_REQUEST_METHOD, HTTP_METHOD, URL_PATH, HTTP_TARGET, HTTP_ROUTE, SENTRY_SEGMENT_NAME_SOURCE } from '@sentry/conventions/attributes';
import { HTTP_SERVER, MIDDLEWARE } from '@sentry/conventions/op';
import { stripUrlQueryAndFragment } from '@sentry/core';
import { ATTR_NEXT_SPAN_TYPE, ATTR_NEXT_ROUTE, ATTR_NEXT_SPAN_NAME } from '../common/nextSpanAttributes.js';
import { TRANSACTION_ATTR_SENTRY_ROUTE_BACKFILL } from '../common/span-attributes-with-logic-attached.js';
import { backfillHttpResponseStatusCode } from '../common/utils/backfillHttpResponseStatusCode.js';
function enhanceHandleRequestRootSpan(span) {
const { attributes } = span;
if (attributes[ATTR_NEXT_SPAN_TYPE] !== "BaseServer.handleRequest" && attributes[SENTRY_ORIGIN] !== "auto.http.cloudflare") {
return;
}
attributes[SENTRY_OP] = HTTP_SERVER;
span.setOp(HTTP_SERVER);
backfillHttpResponseStatusCode(attributes);
const currentName = span.getName();
if (currentName) {
span.setName(stripUrlQueryAndFragment(currentName));
}
const method = attributes[HTTP_REQUEST_METHOD] ?? attributes[HTTP_METHOD];
const target = attributes[URL_PATH] ?? attributes[HTTP_TARGET];
const route = attributes[HTTP_ROUTE] || attributes[ATTR_NEXT_ROUTE];
const spanName = attributes[ATTR_NEXT_SPAN_NAME];
if (typeof method === "string" && typeof route === "string" && !route.startsWith("middleware")) {
const cleanRoute = route.replace(/\/route$/, "");
span.setName(`${method} ${cleanRoute}`);
attributes[SENTRY_SEGMENT_NAME_SOURCE] = "route";
attributes[HTTP_ROUTE] = cleanRoute;
attributes[ATTR_NEXT_ROUTE] = cleanRoute;
}
const routeBackfill = attributes[TRANSACTION_ATTR_SENTRY_ROUTE_BACKFILL];
if (typeof routeBackfill === "string" && span.getName() !== "GET /_app") {
span.setName(`${typeof method === "string" ? method : "GET"} ${routeBackfill}`);
attributes[SENTRY_SEGMENT_NAME_SOURCE] = "route";
attributes[HTTP_ROUTE] = attributes[HTTP_ROUTE] ?? routeBackfill;
}
const middlewareMatch = typeof spanName === "string" && spanName.match(/^middleware (GET|POST|PUT|DELETE|PATCH|HEAD|OPTIONS)/);
if (middlewareMatch) {
span.setName(`middleware ${middlewareMatch[1]}`);
span.setOp(MIDDLEWARE);
attributes[SENTRY_SEGMENT_NAME_SOURCE] = "route";
}
if (span.getName() === "GET /_error" && typeof target === "string") {
span.setName(`${typeof method === "string" ? `${method} ` : ""}${target}`);
}
}
export { enhanceHandleRequestRootSpan };
//# sourceMappingURL=enhanceHandleRequestRootSpan.js.map