UNPKG

autotel

Version:
131 lines (128 loc) 4.17 kB
'use strict'; var chunkYS6C2YJE_cjs = require('./chunk-YS6C2YJE.cjs'); var api = require('@opentelemetry/api'); var spanNameMap = /* @__PURE__ */ new WeakMap(); function setSpanName(span, name) { spanNameMap.set(span, name); } function hexToDecimal(hex) { return BigInt("0x" + hex).toString(10); } function getTraceContext() { const span = api.trace.getActiveSpan(); if (!span) return null; const spanContext = span.spanContext(); const traceId = spanContext.traceId; const spanId = spanContext.spanId; const spanName = spanNameMap.get(span); const traceIdLower64 = traceId.slice(-16); const ddTraceId = hexToDecimal(traceIdLower64); const ddSpanId = hexToDecimal(spanId); return { traceId, spanId, correlationId: traceId.slice(0, 16), ...spanName && { "code.function": spanName }, // Datadog-specific fields for log-trace correlation "dd.trace_id": ddTraceId, "dd.span_id": ddSpanId }; } function enrichWithTraceContext(obj) { const context2 = getTraceContext(); return context2 ? { ...obj, ...context2 } : obj; } function isTracing() { return api.trace.getActiveSpan() !== void 0; } function getTracer(name, version) { return api.trace.getTracer(name, version); } function getActiveSpan() { return api.trace.getActiveSpan(); } function getActiveContext() { try { const { getActiveContextWithBaggage } = chunkYS6C2YJE_cjs.requireModule("./trace-context"); return getActiveContextWithBaggage(); } catch { return api.context.active(); } } function runWithSpan(span, fn) { const ctx = api.trace.setSpan(api.context.active(), span); return api.context.with(ctx, fn); } function finalizeSpan(span, error) { if (error) { if (error instanceof Error) { span.recordException(error); } else { span.recordException(new Error(String(error))); } span.setStatus({ code: api.SpanStatusCode.ERROR }); } else { span.setStatus({ code: api.SpanStatusCode.OK }); } span.end(); } async function createDeterministicTraceId(seed) { const encoder = new TextEncoder(); const data = encoder.encode(seed); const hashBuffer = await crypto.subtle.digest("SHA-256", data); const hashArray = new Uint8Array(hashBuffer); return [...hashArray].map((byte) => byte.toString(16).padStart(2, "0")).join("").slice(0, 32); } function resolveTraceUrl(template, traceId) { const t = template ?? process.env.OTEL_TRACE_URL_TEMPLATE; if (!t) return void 0; return t.replace(/\{traceId\}/g, traceId); } function flattenMetadata(metadata, prefix = "metadata") { const flattened = {}; const seen = /* @__PURE__ */ new WeakSet(); function flatten(obj, currentPrefix) { for (const [key, value] of Object.entries(obj)) { if (value == null) continue; const attributeKey = `${currentPrefix}.${key}`; if (typeof value === "string") { flattened[attributeKey] = value; continue; } if (typeof value === "number" || typeof value === "boolean") { flattened[attributeKey] = String(value); continue; } if (typeof value === "object" && value !== null && value.constructor === Object) { if (seen.has(value)) { flattened[attributeKey] = "<circular-reference>"; continue; } seen.add(value); flatten(value, attributeKey); continue; } try { flattened[attributeKey] = JSON.stringify(value); } catch { flattened[attributeKey] = "<serialization-failed>"; } } } flatten(metadata, prefix); return flattened; } exports.createDeterministicTraceId = createDeterministicTraceId; exports.enrichWithTraceContext = enrichWithTraceContext; exports.finalizeSpan = finalizeSpan; exports.flattenMetadata = flattenMetadata; exports.getActiveContext = getActiveContext; exports.getActiveSpan = getActiveSpan; exports.getTraceContext = getTraceContext; exports.getTracer = getTracer; exports.isTracing = isTracing; exports.resolveTraceUrl = resolveTraceUrl; exports.runWithSpan = runWithSpan; exports.setSpanName = setSpanName; //# sourceMappingURL=chunk-GML3FBOT.cjs.map //# sourceMappingURL=chunk-GML3FBOT.cjs.map