import { startSpan, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_OP } from '@sentry/core'; const ORIGIN = "auto.faas.cloudflare.queue"; function startPublishSpan(options, callback) { const { bindingName, bodySize, messageCount } = options; return startSpan( { op: "queue.publish", name: `send ${bindingName}`, attributes: { "messaging.system": "cloudflare", "messaging.destination.name": bindingName, "messaging.operation.type": "send", "messaging.operation.name": "send", ...messageCount !== void 0 && { "messaging.batch.message_count": messageCount }, "messaging.message.body.size": bodySize, [SEMANTIC_ATTRIBUTE_SENTRY_OP]: "queue.publish", [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: ORIGIN } }, callback ); } function getBodySize(body) { if (body == null) { return void 0; } if (typeof body === "string") { return new TextEncoder().encode(body).byteLength; } if (body instanceof ArrayBuffer) { return body.byteLength; } if (ArrayBuffer.isView(body)) { return body.byteLength; } try { return new TextEncoder().encode(JSON.stringify(body)).byteLength; } catch { return void 0; } } function instrumentQueueProducer(queue, bindingName) { return new Proxy(queue, { get(target, prop, receiver) { if (prop === "send") { const original = Reflect.get(target, prop, receiver); return function(message, options) { return startPublishSpan( { bindingName, bodySize: getBodySize(message) }, () => Reflect.apply(original, target, [message, options]) ); }; } if (prop === "sendBatch") { const original = Reflect.get(target, prop, receiver); return function(messages, options) { const messageArray = Array.from(messages); const totalBodySize = messageArray.reduce((acc, m) => { const size = getBodySize(m.body); if (size === void 0) { return acc; } return (acc ?? 0) + size; }, void 0); return startPublishSpan( { bindingName, bodySize: totalBodySize, messageCount: messageArray.length }, () => Reflect.apply(original, target, [messageArray, options]) ); }; } return Reflect.get(target, prop, receiver); } }); } export { instrumentQueueProducer }; //# sourceMappingURL=instrumentQueueProducer.js.map