autotel-cloudflare
Version:
The #1 OpenTelemetry package for Cloudflare Workers - complete bindings coverage, native CF OTel integration, advanced sampling
71 lines (68 loc) • 1.65 kB
JavaScript
import { f as member } from "./values-BC6rdpGG.js";
//#region src/bindings/common.ts
/**
* Common instrumentation utilities
*/
/**
* Promise tracker for waitUntil
*/
var PromiseTracker = class {
promises = [];
track(promise) {
this.promises.push(promise);
}
async wait() {
await Promise.allSettled(this.promises);
}
};
/**
* Proxy ExecutionContext to track waitUntil promises
*/
function proxyExecutionContext(ctx) {
const tracker = new PromiseTracker();
return {
ctx: new Proxy(ctx, { get(target, prop) {
if (prop === "waitUntil") return (promise) => {
tracker.track(promise);
return target.waitUntil(promise);
};
return member(target, prop);
} }),
tracker
};
}
/**
* Helper to wrap/unwrap proxied objects
*/
const unwrapSymbol = Symbol("unwrap");
function isWrapped(item) {
return item && !!item[unwrapSymbol];
}
function unwrap(item) {
if (item && isWrapped(item)) return item[unwrapSymbol];
else return item;
}
/**
* Set attribute only if value is defined and non-null
*/
function setAttr(span, key, value) {
if (value !== void 0 && value !== null) span.setAttribute(key, value);
}
function wrap(item, handler) {
const proxy = new Proxy(item, handler);
Object.defineProperty(proxy, unwrapSymbol, {
value: item,
writable: false,
enumerable: false,
configurable: false
});
return proxy;
}
//#endregion
//#region src/exception.ts
/** The caught value, in a form a span can record. */
function toException(cause) {
return cause instanceof Error ? cause : String(cause);
}
//#endregion
export { unwrap as a, setAttr as i, isWrapped as n, wrap as o, proxyExecutionContext as r, toException as t };