UNPKG

@sentry/nextjs

Version:
116 lines (112 loc) • 5.27 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const core = require('@sentry/core'); const responseEnd = require('./utils/responseEnd.js'); const debugBuild = require('./debug-build.js'); const nextNavigationErrorUtils = require('./nextNavigationErrorUtils.js'); const attributes = require('@sentry/conventions/attributes'); const op = require('@sentry/conventions/op'); function withServerActionInstrumentation(...args) { if (typeof args[1] === "function") { const [serverActionName, callback] = args; return withServerActionInstrumentationImplementation(serverActionName, {}, callback); } else { const [serverActionName, options, callback] = args; return withServerActionInstrumentationImplementation(serverActionName, options, callback); } } async function withServerActionInstrumentationImplementation(serverActionName, options, callback) { return core.withIsolationScope(async (isolationScope) => { const shouldRecordResponse = core.getClient()?.getDataCollectionOptions().httpBodies.includes("outgoingResponse"); let sentryTraceHeader; let baggageHeader; const fullHeadersObject = {}; try { const awaitedHeaders = await options.headers; sentryTraceHeader = awaitedHeaders?.get("sentry-trace") ?? void 0; baggageHeader = awaitedHeaders?.get("baggage"); awaitedHeaders?.forEach((value, key) => { fullHeadersObject[key] = value; }); } catch { debugBuild.DEBUG_BUILD && core.debug.warn( "Sentry wasn't able to extract the tracing headers for a server action. Will not trace this request." ); } isolationScope.setTransactionName(`serverAction/${serverActionName}`); isolationScope.setSDKProcessingMetadata({ normalizedRequest: { headers: fullHeadersObject } }); const continueTraceIfNoActiveSpan = core.getActiveSpan() ? (_opts, callback2) => callback2() : core.continueTrace; return continueTraceIfNoActiveSpan( { sentryTrace: sentryTraceHeader, baggage: baggageHeader }, async () => { try { const client = core.getClient(); const description = `serverAction/${serverActionName}`; return await core.startSpan( { name: client && core.hasSpanStreamingEnabled(client) ? serverActionName : description, // oxlint-disable-next-line typescript/no-deprecated forceTransaction: true, attributes: { [attributes.SENTRY_KIND]: "server", [attributes.SENTRY_OP]: op.FUNCTION, // Overriding span description inference in Relay, because we can't express the serverAction/ prefix // as a sentry-conventions span name rule for `function` spans. [attributes.SENTRY_DESCRIPTION]: description, [attributes.CODE_FUNCTION_NAME]: serverActionName, [attributes.SENTRY_SEGMENT_NAME_SOURCE]: "route", [attributes.SENTRY_ORIGIN]: "auto.function.nextjs.server_action" } }, async (span) => { const result = await core.handleCallbackErrors(callback, (error) => { const authInterruptStatusCode = nextNavigationErrorUtils.getAuthInterruptStatusCode(error); if (nextNavigationErrorUtils.isNotFoundNavigationError(error)) { span.setStatus({ code: core.SPAN_STATUS_ERROR, message: "not_found" }); } else if (authInterruptStatusCode) { span.setStatus(core.getSpanStatusFromHttpCode(authInterruptStatusCode)); } else if (nextNavigationErrorUtils.isRedirectNavigationError(error)) { span.setStatus({ code: core.SPAN_STATUS_OK }); span.end(); } else if (nextNavigationErrorUtils.isPrerenderControlFlowError(error)) { span.setStatus({ code: core.SPAN_STATUS_OK }); span.end(); } else { span.setStatus({ code: core.SPAN_STATUS_ERROR, message: "internal_error" }); core.captureException(error, { mechanism: { handled: false, type: "auto.function.nextjs.server_action" } }); } }); if (options.recordResponse !== void 0 ? options.recordResponse : shouldRecordResponse) { core.getIsolationScope().setExtra("server_action_result", result); } if (options.formData) { options.formData.forEach((value, key) => { core.getIsolationScope().setExtra( `server_action_form_data.${key}`, typeof value === "string" ? value : "[non-string value]" ); }); } return result; } ); } finally { responseEnd.waitUntil(responseEnd.flushSafelyWithTimeout()); } } ); }); } exports.withServerActionInstrumentation = withServerActionInstrumentation; //# sourceMappingURL=withServerActionInstrumentation.js.map