@sentry/nextjs
Version:
Official Sentry SDK for Next.js
73 lines (64 loc) • 2.18 kB
JavaScript
import { _optionalChain } from '@sentry/utils';
import { addTracingExtensions, continueTrace, startSpan, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, handleCallbackErrors, captureException } from '@sentry/core';
import { winterCGRequestToRequestData } from '@sentry/utils';
import { flushQueue } from './responseEnd.js';
/**
* Wraps a function on the edge runtime with error and performance monitoring.
*/
function withEdgeWrapping(
handler,
options,
) {
return async function ( ...args) {
addTracingExtensions();
const req = args[0];
let sentryTrace;
let baggage;
if (req instanceof Request) {
sentryTrace = req.headers.get('sentry-trace') || '';
baggage = req.headers.get('baggage');
}
const transactionContext = continueTrace({
sentryTrace,
baggage,
});
return startSpan(
{
...transactionContext,
name: options.spanDescription,
op: options.spanOp,
origin: 'auto.function.nextjs.withEdgeWrapping',
attributes: { [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route' },
metadata: {
// eslint-disable-next-line deprecation/deprecation
...transactionContext.metadata,
request: req instanceof Request ? winterCGRequestToRequestData(req) : undefined,
},
},
async span => {
const handlerResult = await handleCallbackErrors(
() => handler.apply(this, args),
error => {
captureException(error, {
mechanism: {
type: 'instrument',
handled: false,
data: {
function: options.mechanismFunctionName,
},
},
});
},
);
if (handlerResult instanceof Response) {
_optionalChain([span, 'optionalAccess', _ => _.setHttpStatus, 'call', _2 => _2(handlerResult.status)]);
} else {
_optionalChain([span, 'optionalAccess', _3 => _3.setStatus, 'call', _4 => _4('ok')]);
}
return handlerResult;
},
).finally(() => flushQueue());
};
}
export { withEdgeWrapping };
//# sourceMappingURL=edgeWrapperUtils.js.map