Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
88 lines
4.2 KiB
Plaintext
88 lines
4.2 KiB
Plaintext
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
|
const api = require('@opentelemetry/api');
|
|
const semanticConventions = require('@opentelemetry/semantic-conventions');
|
|
const core = require('@sentry/core');
|
|
const opentelemetry = require('@sentry/opentelemetry');
|
|
const nextSpanAttributes = require('../common/nextSpanAttributes.js');
|
|
const addHeadersAsAttributes = require('../common/utils/addHeadersAsAttributes.js');
|
|
const dropMiddlewareTunnelRequests = require('../common/utils/dropMiddlewareTunnelRequests.js');
|
|
const tracingUtils = require('../common/utils/tracingUtils.js');
|
|
const vercelCronsMonitoring = require('./vercelCronsMonitoring.js');
|
|
|
|
/**
|
|
* Handles the on span start event for Next.js spans.
|
|
* This function is used to enhance the span with additional information such as the route, the method, the headers, etc.
|
|
* It is called for every span that is started by Next.js.
|
|
* @param span The span that is starting.
|
|
*/
|
|
function handleOnSpanStart(span) {
|
|
const spanAttributes = core.spanToJSON(span).data;
|
|
const rootSpan = core.getRootSpan(span);
|
|
const rootSpanAttributes = core.spanToJSON(rootSpan).data;
|
|
const isRootSpan = span === rootSpan;
|
|
|
|
dropMiddlewareTunnelRequests.dropMiddlewareTunnelRequests(span, spanAttributes);
|
|
|
|
// What we do in this glorious piece of code, is hoist any information about parameterized routes from spans emitted
|
|
// by Next.js via the `next.route` attribute, up to the transaction by setting the http.route attribute.
|
|
if (typeof spanAttributes?.[nextSpanAttributes.ATTR_NEXT_ROUTE] === 'string') {
|
|
// Only hoist the http.route attribute if the transaction doesn't already have it
|
|
if (
|
|
// eslint-disable-next-line deprecation/deprecation
|
|
(rootSpanAttributes?.[semanticConventions.ATTR_HTTP_REQUEST_METHOD] || rootSpanAttributes?.[semanticConventions.SEMATTRS_HTTP_METHOD]) &&
|
|
!rootSpanAttributes?.[semanticConventions.ATTR_HTTP_ROUTE]
|
|
) {
|
|
const route = spanAttributes[nextSpanAttributes.ATTR_NEXT_ROUTE].replace(/\/route$/, '');
|
|
rootSpan.updateName(route);
|
|
rootSpan.setAttribute(semanticConventions.ATTR_HTTP_ROUTE, route);
|
|
// Preserving the original attribute despite internally not depending on it
|
|
rootSpan.setAttribute(nextSpanAttributes.ATTR_NEXT_ROUTE, route);
|
|
|
|
// Check if this is a Vercel cron request and start a check-in
|
|
vercelCronsMonitoring.maybeStartCronCheckIn(rootSpan, route);
|
|
}
|
|
}
|
|
|
|
if (spanAttributes?.[nextSpanAttributes.ATTR_NEXT_SPAN_TYPE] === 'Middleware.execute') {
|
|
const middlewareName = spanAttributes[nextSpanAttributes.ATTR_NEXT_SPAN_NAME];
|
|
if (typeof middlewareName === 'string') {
|
|
rootSpan.updateName(middlewareName);
|
|
rootSpan.setAttribute(semanticConventions.ATTR_HTTP_ROUTE, middlewareName);
|
|
rootSpan.setAttribute(nextSpanAttributes.ATTR_NEXT_SPAN_NAME, middlewareName);
|
|
}
|
|
span.setAttribute(core.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto');
|
|
}
|
|
|
|
// We want to skip span data inference for any spans generated by Next.js. Reason being that Next.js emits spans
|
|
// with patterns (e.g. http.server spans) that will produce confusing data.
|
|
if (spanAttributes?.[nextSpanAttributes.ATTR_NEXT_SPAN_TYPE] !== undefined) {
|
|
span.setAttribute(core.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto');
|
|
}
|
|
|
|
if (isRootSpan) {
|
|
const headers = core.getIsolationScope().getScopeData().sdkProcessingMetadata?.normalizedRequest?.headers;
|
|
addHeadersAsAttributes.addHeadersAsAttributes(headers, rootSpan);
|
|
}
|
|
|
|
// We want to fork the isolation scope for incoming requests
|
|
if (spanAttributes?.[nextSpanAttributes.ATTR_NEXT_SPAN_TYPE] === 'BaseServer.handleRequest' && isRootSpan) {
|
|
const scopes = core.getCapturedScopesOnSpan(span);
|
|
|
|
const isolationScope = (scopes.isolationScope || core.getIsolationScope()).clone();
|
|
const scope = scopes.scope || core.getCurrentScope();
|
|
|
|
const currentScopesPointer = opentelemetry.getScopesFromContext(api.context.active());
|
|
if (currentScopesPointer) {
|
|
currentScopesPointer.isolationScope = isolationScope;
|
|
}
|
|
|
|
core.setCapturedScopesOnSpan(span, scope, isolationScope);
|
|
}
|
|
|
|
tracingUtils.maybeEnhanceServerComponentSpanName(span, spanAttributes, rootSpanAttributes);
|
|
}
|
|
|
|
exports.handleOnSpanStart = handleOnSpanStart;
|
|
//# sourceMappingURL=handleOnSpanStart.js.map
|