import { withIsolationScope, captureException } from '@sentry/core'; import { flushAndDispose } from '../../flush.js'; import { ensureInstrumented } from '../../instrument.js'; import { getFinalOptions } from '../../options.js'; import { addCloudResourceContext } from '../../scope-utils.js'; import { init } from '../../sdk.js'; import { instrumentContext } from '../../utils/instrumentContext.js'; import { instrumentEnv } from './instrumentEnv.js'; function wrapTailHandler(options, context, fn) { return withIsolationScope(async (isolationScope) => { const waitUntil = context.waitUntil.bind(context); const client = init({ ...options, ctx: context }); isolationScope.setClient(client); addCloudResourceContext(isolationScope); try { return await fn(); } catch (e) { captureException(e, { mechanism: { handled: false, type: "auto.faas.cloudflare.tail" } }); throw e; } finally { waitUntil(flushAndDispose(client)); } }); } function instrumentExportedHandlerTail(handler, optionsCallback) { if (!("tail" in handler) || typeof handler.tail !== "function") { return; } handler.tail = ensureInstrumented( handler.tail, (original) => new Proxy(original, { apply(target, thisArg, args) { const [, env, ctx] = args; const context = instrumentContext(ctx); const options = getFinalOptions(optionsCallback(env), env); args[1] = instrumentEnv(env, options); args[2] = context; return wrapTailHandler(options, context, () => target.apply(thisArg, args)); } }) ); } function instrumentWorkerEntrypointTail(instance, options, context) { if (!instance.tail) { return; } const original = instance.tail.bind(instance); instance.tail = new Proxy(original, { apply(target, thisArg, args) { return wrapTailHandler(options, context, () => Reflect.apply(target, thisArg, args)); } }); } export { instrumentExportedHandlerTail, instrumentWorkerEntrypointTail }; //# sourceMappingURL=instrumentTail.js.map