import { captureException } from '@sentry/core'; import { setAsyncLocalStorageAsyncContextStrategy } from './async.js'; import { ensureInstrumented } from './instrument.js'; import { instrumentEnv } from './instrumentations/worker/instrumentEnv.js'; import { getFinalOptions } from './options.js'; import { wrapRequestHandler } from './request.js'; import { instrumentContext } from './utils/instrumentContext.js'; import { extractRpcMeta } from './utils/rpcMeta.js'; import { getEffectiveRpcPropagation } from './utils/rpcOptions.js'; import { wrapMethodWithSentry } from './wrapMethodWithSentry.js'; function instrumentDurableObjectWithSentry(optionsCallback, DurableObjectClass) { return new Proxy(DurableObjectClass, { construct(target, [ctx, env]) { setAsyncLocalStorageAsyncContextStrategy(); const context = instrumentContext(ctx); const options = getFinalOptions(optionsCallback(env), env); const instrumentedEnv = instrumentEnv(env, options); const obj = new target(context, instrumentedEnv); if (obj.fetch && typeof obj.fetch === "function") { obj.fetch = ensureInstrumented( obj.fetch, (original) => new Proxy(original, { apply(target2, thisArg, args) { return wrapRequestHandler({ options, request: args[0], context }, () => { return Reflect.apply(target2, thisArg, args); }); } }) ); } if (obj.alarm && typeof obj.alarm === "function") { obj.alarm = wrapMethodWithSentry( { options, context, spanName: "alarm", spanOp: "function", startNewTrace: true }, obj.alarm ); } if (obj.webSocketMessage && typeof obj.webSocketMessage === "function") { obj.webSocketMessage = wrapMethodWithSentry( { options, context, spanName: "webSocketMessage" }, obj.webSocketMessage ); } if (obj.webSocketClose && typeof obj.webSocketClose === "function") { obj.webSocketClose = wrapMethodWithSentry({ options, context, spanName: "webSocketClose" }, obj.webSocketClose); } if (obj.webSocketError && typeof obj.webSocketError === "function") { obj.webSocketError = wrapMethodWithSentry( { options, context, spanName: "webSocketError" }, obj.webSocketError, (_, error) => captureException(error, { mechanism: { type: "auto.faas.cloudflare.durable_object_websocket", handled: false } }) ); } const rpcPropagation = getEffectiveRpcPropagation(options); if (!rpcPropagation) { return obj; } const instrumentPrototypeMethods = Array.isArray(options.instrumentPrototypeMethods) ? options.instrumentPrototypeMethods : void 0; const allowSet = instrumentPrototypeMethods ? new Set(instrumentPrototypeMethods) : null; const alwaysTrace = options.enableRpcTracePropagation === void 0; const methodCache = /* @__PURE__ */ new Map(); return new Proxy(obj, { get(proxyTarget, prop, receiver) { const value = Reflect.get(proxyTarget, prop, receiver); if (typeof prop !== "string" || typeof value !== "function" || prop === "constructor") { return value; } const cached = methodCache.get(prop); if (cached) { return cached; } const boundMethod = value.bind(proxyTarget); if (prop in Object.prototype || Object.prototype.hasOwnProperty.call(proxyTarget, prop) || allowSet && !allowSet.has(prop)) { methodCache.set(prop, boundMethod); return boundMethod; } const tracedMethod = wrapMethodWithSentry( { options, context, spanName: prop, spanOp: "rpc" }, boundMethod, void 0, true ); if (alwaysTrace) { methodCache.set(prop, tracedMethod); return tracedMethod; } const wrappedMethod = ((...args) => { const { rpcMeta } = extractRpcMeta(args); return rpcMeta ? tracedMethod(...args) : boundMethod(...args); }); methodCache.set(prop, wrappedMethod); return wrappedMethod; } }); } }); } export { instrumentDurableObjectWithSentry }; //# sourceMappingURL=durableobject.js.map