autotel
Version:
Write Once, Observe Anywhere
118 lines (116 loc) • 3.83 kB
JavaScript
import { requireModule } from './chunk-33WTKH7X.js';
import { trace, context, SpanStatusCode } from '@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 = 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 trace.getActiveSpan() !== void 0;
}
function getTracer(name, version) {
return trace.getTracer(name, version);
}
function getActiveSpan() {
return trace.getActiveSpan();
}
function getActiveContext() {
try {
const { getActiveContextWithBaggage } = requireModule("./trace-context");
return getActiveContextWithBaggage();
} catch {
return context.active();
}
}
function runWithSpan(span, fn) {
const ctx = trace.setSpan(context.active(), span);
return 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: SpanStatusCode.ERROR });
} else {
span.setStatus({ code: 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;
}
export { createDeterministicTraceId, enrichWithTraceContext, finalizeSpan, flattenMetadata, getActiveContext, getActiveSpan, getTraceContext, getTracer, isTracing, resolveTraceUrl, runWithSpan, setSpanName };
//# sourceMappingURL=chunk-B3ZHLLMP.js.map
//# sourceMappingURL=chunk-B3ZHLLMP.js.map