Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const core = require('@sentry/core'); const flush = require('./flush.js'); const instrument = require('./instrument.js'); const sdk = require('./sdk.js'); const rpcMeta = require('./utils/rpcMeta.js'); const traceLinks = require('./utils/traceLinks.js'); function resolveOriginalStorage(context, thisArg) { if (thisArg && typeof thisArg === "object" && "ctx" in thisArg) { const doCtx = thisArg.ctx; if (doCtx?.originalStorage) { return doCtx.originalStorage; } } if (context && "originalStorage" in context && context.originalStorage) { return context.originalStorage; } return void 0; } function wrapMethodWithSentry(wrapperOptions, handler, callback, noMark) { return instrument.ensureInstrumented( handler, (original) => new Proxy(original, { apply(target, thisArg, rawArgs) { const { startNewTrace } = wrapperOptions; let args = rawArgs; let rpcMeta$1; if (wrapperOptions.spanOp === "rpc") { const extracted = rpcMeta.extractRpcMeta(rawArgs); args = extracted.args; rpcMeta$1 = extracted.rpcMeta; } const currentClient = core.getClient(); const sentryWithScope = startNewTrace ? core.withIsolationScope : currentClient ? core.withScope : core.withIsolationScope; const wrappedFunction = (scope) => { const context = wrapperOptions.context; const waitUntil = context?.waitUntil?.bind?.(context); const storage = resolveOriginalStorage(context, thisArg); let scopeClient = scope.getClient(); if (startNewTrace || !scopeClient?.getTransport()) { const client = sdk.init({ ...wrapperOptions.options, ctx: context }); scope.setClient(client); scopeClient = client; } const clientToDispose = scopeClient; const methodName = wrapperOptions.spanName || "unknown"; const teardown = async () => { if (startNewTrace && storage) { traceLinks.storeSpanContext(storage, methodName); } await flush.flushAndDispose(clientToDispose); }; const onFulfilled = (res) => { waitUntil?.(teardown()); return res; }; const onRejected = (e) => { core.captureException(e, { mechanism: { type: "auto.faas.cloudflare.durable_object", handled: false } }); waitUntil?.(teardown()); throw e; }; if (!wrapperOptions.spanName) { try { if (callback) { callback(...args); } const result = Reflect.apply(target, thisArg, args); if (core.isThenable(result)) { return result.then(onFulfilled, onRejected); } else { return onFulfilled(result); } } catch (e) { return onRejected(e); } } const attributes = wrapperOptions.spanOp ? { [core.SEMANTIC_ATTRIBUTE_SENTRY_OP]: wrapperOptions.spanOp, [core.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: "auto.faas.cloudflare.durable_object" } : {}; const executeSpan = () => { return core.startSpan({ name: methodName, attributes }, (span) => { if (startNewTrace && storage) { const storedContext = traceLinks.getStoredSpanContext(storage, methodName); if (storedContext) { span.addLinks(traceLinks.buildSpanLinks(storedContext)); const sampledFlag = storedContext.sampled ? "1" : "0"; span.setAttribute( "sentry.previous_trace", `${storedContext.traceId}-${storedContext.spanId}-${sampledFlag}` ); } } try { const result = Reflect.apply(target, thisArg, args); if (core.isThenable(result)) { return result.then(onFulfilled, onRejected); } else { return onFulfilled(result); } } catch (e) { return onRejected(e); } }); }; if (rpcMeta$1) { return core.continueTrace( { sentryTrace: rpcMeta$1["sentry-trace"] || "", baggage: rpcMeta$1.baggage || "" }, executeSpan ); } if (startNewTrace) { return core.startNewTrace(() => executeSpan()); } return executeSpan(); }; return sentryWithScope(wrappedFunction); } }), noMark ); } exports.wrapMethodWithSentry = wrapMethodWithSentry; //# sourceMappingURL=wrapMethodWithSentry.js.map