UNPKG

effect

Version:

The missing standard library for TypeScript, for writing production-grade software.

131 lines (129 loc) 4.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.tracerTag = exports.spanTag = exports.nativeTracer = exports.make = exports.externalSpan = exports.addSpanStackTrace = exports.TracerTypeId = exports.NativeSpan = exports.DisablePropagation = void 0; var Context = _interopRequireWildcard(require("../Context.js")); var _Function = require("../Function.js"); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } /** * @since 2.0.0 */ /** @internal */ const TracerTypeId = exports.TracerTypeId = /*#__PURE__*/Symbol.for("effect/Tracer"); /** @internal */ const make = options => ({ [TracerTypeId]: TracerTypeId, ...options }); /** @internal */ exports.make = make; const tracerTag = exports.tracerTag = /*#__PURE__*/Context.GenericTag("effect/Tracer"); /** @internal */ const spanTag = exports.spanTag = /*#__PURE__*/Context.GenericTag("effect/ParentSpan"); const randomHexString = /*#__PURE__*/function () { const characters = "abcdef0123456789"; const charactersLength = characters.length; return function (length) { let result = ""; for (let i = 0; i < length; i++) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); } return result; }; }(); /** @internal */ class NativeSpan { name; parent; context; links; startTime; kind; _tag = "Span"; spanId; traceId = "native"; sampled = true; status; attributes; events = []; constructor(name, parent, context, links, startTime, kind) { this.name = name; this.parent = parent; this.context = context; this.links = links; this.startTime = startTime; this.kind = kind; this.status = { _tag: "Started", startTime }; this.attributes = new Map(); this.traceId = parent._tag === "Some" ? parent.value.traceId : randomHexString(32); this.spanId = randomHexString(16); } end(endTime, exit) { this.status = { _tag: "Ended", endTime, exit, startTime: this.status.startTime }; } attribute(key, value) { this.attributes.set(key, value); } event(name, startTime, attributes) { this.events.push([name, startTime, attributes ?? {}]); } } /** @internal */ exports.NativeSpan = NativeSpan; const nativeTracer = exports.nativeTracer = /*#__PURE__*/make({ span: (name, parent, context, links, startTime, kind) => new NativeSpan(name, parent, context, links, startTime, kind), context: f => f() }); /** @internal */ const externalSpan = options => ({ _tag: "ExternalSpan", spanId: options.spanId, traceId: options.traceId, sampled: options.sampled ?? true, context: options.context ?? Context.empty() }); /** @internal */ exports.externalSpan = externalSpan; const addSpanStackTrace = options => { if (options?.captureStackTrace === false) { return options; } else if (options?.captureStackTrace !== undefined && typeof options.captureStackTrace !== "boolean") { return options; } const limit = Error.stackTraceLimit; Error.stackTraceLimit = 3; const traceError = new Error(); Error.stackTraceLimit = limit; let cache = false; return { ...options, captureStackTrace: () => { if (cache !== false) { return cache; } if (traceError.stack !== undefined) { const stack = traceError.stack.split("\n"); if (stack[3] !== undefined) { cache = stack[3].trim(); return cache; } } } }; }; /** @internal */ exports.addSpanStackTrace = addSpanStackTrace; const DisablePropagation = exports.DisablePropagation = /*#__PURE__*/Context.Reference()("effect/Tracer/DisablePropagation", { defaultValue: _Function.constFalse }); //# sourceMappingURL=tracer.js.map