@sentry/nextjs
Version:
Official Sentry SDK for Next.js
67 lines (64 loc) • 3.5 kB
JavaScript
import { context } from '@opentelemetry/api';
import { HTTP_REQUEST_METHOD, HTTP_METHOD, HTTP_ROUTE } from '@sentry/conventions/attributes';
import { spanToJSON, getRootSpan, getIsolationScope, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, getCapturedScopesOnSpan, getCurrentScope, setCapturedScopesOnSpan } from '@sentry/core';
import { getScopesFromContext } from '@sentry/opentelemetry';
import { ATTR_NEXT_ROUTE, ATTR_NEXT_SPAN_TYPE, ATTR_NEXT_SPAN_NAME } from '../common/nextSpanAttributes.js';
import { addHeadersAsAttributes } from '../common/utils/addHeadersAsAttributes.js';
import { dropMiddlewareTunnelRequests } from '../common/utils/dropMiddlewareTunnelRequests.js';
import { maybeEnhanceServerComponentSpanName } from '../common/utils/tracingUtils.js';
import { maybeStartCronCheckIn } from './vercelCronsMonitoring.js';
import { maybeEnrichQueueConsumerSpan, maybeEnrichQueueProducerSpan } from './vercelQueuesMonitoring.js';
function handleOnSpanStart(span) {
const spanAttributes = spanToJSON(span).data;
const rootSpan = getRootSpan(span);
const rootSpanAttributes = spanToJSON(rootSpan).data;
const isRootSpan = span === rootSpan;
dropMiddlewareTunnelRequests(span, spanAttributes);
if (typeof spanAttributes?.[ATTR_NEXT_ROUTE] === "string") {
if (
// eslint-disable-next-line typescript/no-deprecated
(rootSpanAttributes?.[HTTP_REQUEST_METHOD] || rootSpanAttributes?.[HTTP_METHOD]) && !rootSpanAttributes?.[HTTP_ROUTE]
) {
const route = spanAttributes[ATTR_NEXT_ROUTE].replace(/\/route$/, "");
rootSpan.updateName(route);
rootSpan.setAttribute(HTTP_ROUTE, route);
rootSpan.setAttribute(ATTR_NEXT_ROUTE, route);
const method = rootSpanAttributes?.[HTTP_REQUEST_METHOD] || rootSpanAttributes?.[HTTP_METHOD];
if (typeof method === "string") {
getIsolationScope().setTransactionName(`${method} ${route}`);
}
maybeStartCronCheckIn(rootSpan, route);
maybeEnrichQueueConsumerSpan(rootSpan);
}
}
if (spanAttributes?.[ATTR_NEXT_SPAN_TYPE] === "Middleware.execute") {
const middlewareName = spanAttributes[ATTR_NEXT_SPAN_NAME];
if (typeof middlewareName === "string") {
rootSpan.updateName(middlewareName);
rootSpan.setAttribute(HTTP_ROUTE, middlewareName);
rootSpan.setAttribute(ATTR_NEXT_SPAN_NAME, middlewareName);
}
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, "auto");
}
if (spanAttributes?.[ATTR_NEXT_SPAN_TYPE] !== void 0) {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, "auto");
}
if (isRootSpan) {
const headers = getIsolationScope().getScopeData().sdkProcessingMetadata?.normalizedRequest?.headers;
addHeadersAsAttributes(headers, rootSpan);
}
if ((spanAttributes?.[ATTR_NEXT_SPAN_TYPE] === "BaseServer.handleRequest" || spanAttributes?.[ATTR_NEXT_SPAN_TYPE] === "Middleware.execute") && isRootSpan) {
const scopes = getCapturedScopesOnSpan(span);
const isolationScope = (scopes.isolationScope || getIsolationScope()).clone();
const scope = scopes.scope || getCurrentScope();
const currentScopesPointer = getScopesFromContext(context.active());
if (currentScopesPointer) {
currentScopesPointer.isolationScope = isolationScope;
}
setCapturedScopesOnSpan(span, scope, isolationScope);
}
maybeEnhanceServerComponentSpanName(span, spanAttributes, rootSpanAttributes);
maybeEnrichQueueProducerSpan(span);
}
export { handleOnSpanStart };
//# sourceMappingURL=handleOnSpanStart.js.map