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
98 lines
3.7 KiB
Plaintext
98 lines
3.7 KiB
Plaintext
import { httpRequestToRequestData, getCurrentScope, getIsolationScope, getActiveSpan, getRootSpan, getTraceData, captureException } from '@sentry/core';
|
|
import { TRANSACTION_ATTR_SENTRY_ROUTE_BACKFILL } from '../span-attributes-with-logic-attached.js';
|
|
|
|
/**
|
|
* Wraps a function that potentially throws. If it does, the error is passed to `captureException` and rethrown.
|
|
*
|
|
* Note: This function turns the wrapped function into an asynchronous one.
|
|
*/
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
function withErrorInstrumentation(
|
|
origFunction,
|
|
) {
|
|
return async function ( ...origFunctionArguments) {
|
|
try {
|
|
return await origFunction.apply(this, origFunctionArguments);
|
|
} catch (e) {
|
|
// TODO: Extract error logic from `withSentry` in here or create a new wrapper with said logic or something like that.
|
|
captureException(e, {
|
|
// TODO: check if origFunction.name actually returns the correct name or minified garbage
|
|
// in this case, we can add another argument to this wrapper with the respective function name
|
|
mechanism: { handled: false, type: 'auto.function.nextjs.wrapped', data: { function: origFunction.name } },
|
|
});
|
|
throw e;
|
|
}
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Calls a server-side data fetching function (that takes a `req` and `res` object in its context) with tracing
|
|
* instrumentation. A transaction will be created for the incoming request (if it doesn't already exist) in addition to
|
|
* a span for the wrapped data fetching function.
|
|
*
|
|
* All of the above happens in an isolated domain, meaning all thrown errors will be associated with the correct span.
|
|
*
|
|
* @param origDataFetcher The data fetching method to call.
|
|
* @param origFunctionArguments The arguments to call the data fetching method with.
|
|
* @param req The data fetching function's request object.
|
|
* @param res The data fetching function's response object.
|
|
* @param options Options providing details for the created transaction and span.
|
|
* @returns what the data fetching method call returned.
|
|
*/
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
function withTracedServerSideDataFetcher(
|
|
origDataFetcher,
|
|
req,
|
|
res,
|
|
options
|
|
|
|
,
|
|
) {
|
|
return async function (
|
|
|
|
...args
|
|
) {
|
|
const normalizedRequest = httpRequestToRequestData(req);
|
|
getCurrentScope().setTransactionName(`${options.dataFetchingMethodName} (${options.dataFetcherRouteName})`);
|
|
getIsolationScope().setSDKProcessingMetadata({ normalizedRequest });
|
|
|
|
const span = getActiveSpan();
|
|
|
|
// Only set the route backfill if the span is not for /_error
|
|
if (span && options.requestedRouteName !== '/_error') {
|
|
const root = getRootSpan(span);
|
|
root.setAttribute(TRANSACTION_ATTR_SENTRY_ROUTE_BACKFILL, options.requestedRouteName);
|
|
}
|
|
|
|
const { 'sentry-trace': sentryTrace, baggage } = getTraceData();
|
|
|
|
return {
|
|
sentryTrace: sentryTrace,
|
|
baggage: baggage,
|
|
data: await origDataFetcher.apply(this, args),
|
|
};
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Call a data fetcher and trace it. Only traces the function if there is an active transaction on the scope.
|
|
*
|
|
* We only do the following until we move transaction creation into this function: When called, the wrapped function
|
|
* will also update the name of the active transaction with a parameterized route provided via the `options` argument.
|
|
*/
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
async function callDataFetcherTraced(
|
|
origFunction,
|
|
origFunctionArgs,
|
|
) {
|
|
try {
|
|
return await origFunction(...origFunctionArgs);
|
|
} catch (e) {
|
|
captureException(e, { mechanism: { handled: false, type: 'auto.function.nextjs.data_fetcher' } });
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
export { callDataFetcherTraced, withErrorInstrumentation, withTracedServerSideDataFetcher };
|
|
//# sourceMappingURL=wrapperUtils.js.map
|