@sentry/nextjs
Version:
Official Sentry SDK for Next.js
45 lines (42 loc) • 2.24 kB
JavaScript
import { HTTP_METHOD, HTTP_REQUEST_METHOD, HTTP_TARGET, HTTP_ROUTE } from '@sentry/conventions/attributes';
import { SEMANTIC_ATTRIBUTE_SENTRY_OP, stripUrlQueryAndFragment, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } 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';
function enhanceHandleRequestRootSpan(span) {
const { attributes } = span;
if (attributes[ATTR_NEXT_SPAN_TYPE] !== "BaseServer.handleRequest") {
return;
}
attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP] = "http.server";
span.setOp("http.server");
const currentName = span.getName();
if (currentName) {
span.setName(stripUrlQueryAndFragment(currentName));
}
const method = attributes[HTTP_METHOD] ?? attributes[HTTP_REQUEST_METHOD];
const target = 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[SEMANTIC_ATTRIBUTE_SENTRY_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[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("http.server.middleware");
}
if (span.getName() === "GET /_error" && typeof target === "string") {
span.setName(`${typeof method === "string" ? `${method} ` : ""}${target}`);
}
}
export { enhanceHandleRequestRootSpan };
//# sourceMappingURL=enhanceHandleRequestRootSpan.js.map