"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OtelState = void 0;
exports.isTracingInstalled = isTracingInstalled;
exports.getTracer = getTracer;
exports.getActiveSpan = getActiveSpan;
exports.getTraceContext = getTraceContext;
exports.disableActiveTrace = disableActiveTrace;
exports.withRemoteSpan = withRemoteSpan;
exports.withSpan = withSpan;
exports.withRootSpan = withRootSpan;
exports.setSpanAttributes = setSpanAttributes;
function __assignType(fn, args) {
    fn.__type = args;
    return fn;
}
const api_1 = require("@opentelemetry/api");
const types_1 = require("util/types");
exports.OtelState = {
    tracer: undefined,
    prometheusExporter: undefined,
    get installed() {
        return exports.OtelState.tracer !== undefined;
    }
};
function isTracingInstalled() {
    return exports.OtelState.installed;
}
isTracingInstalled.__type = ['isTracingInstalled', 'P"/!'];
function getTracer() {
    return exports.OtelState.tracer;
}
getTracer.__type = ['getTracer', 'P"/!'];
function getActiveSpan() {
    return api_1.trace.getActiveSpan();
}
getActiveSpan.__type = ['getActiveSpan', 'P"/!'];
function getTraceContext() {
    return getActiveSpan()?.spanContext();
}
getTraceContext.__type = ['getTraceContext', 'P"/!'];
function disableActiveTrace() {
    const ctx = getTraceContext();
    if (ctx) {
        ctx.traceFlags = 0;
    }
}
disableActiveTrace.__type = ['disableActiveTrace', 'P"/!'];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isError(e) {
    return e instanceof Error || (0, types_1.isNativeError)(e);
}
isError.__type = ['e', 'isError', 'P"2!!/"'];
async function runInSpan(span, fn) {
    try {
        const result = await fn();
        span.setStatus({ code: api_1.SpanStatusCode.OK });
        return result;
    }
    catch (err) {
        span.setStatus({
            code: api_1.SpanStatusCode.ERROR,
            message: isError(err) ? err.message : String(err)
        });
        throw err;
    }
    finally {
        span.end();
    }
}
runInSpan.__type = ['Span', 'span', '', 'fn', 'runInSpan', 'P"w!2"P"`/#2$"`/%'];
const __ΩSpanInfo = ['traceId', 'spanId', 'traceFlags', 'traceparent', 'SpanInfo', 'PP&4!&4"\'4#8MP&4$M-Jw%y'];
function withRemoteSpan(name, spanInfo, attrs, fn) {
    if (!exports.OtelState.tracer)
        return fn();
    if (!spanInfo)
        return withSpan(name, attrs, fn);
    let traceId;
    let spanId;
    let traceFlags;
    if ('traceparent' in spanInfo) {
        const parts = spanInfo.traceparent.split('-');
        traceId = parts[1];
        spanId = parts[2];
    }
    else {
        traceId = spanInfo.traceId;
        spanId = spanInfo.spanId;
        traceFlags = spanInfo.traceFlags;
    }
    const parentContext = api_1.trace.setSpanContext(api_1.ROOT_CONTEXT, {
        traceId,
        spanId,
        traceFlags: traceFlags ?? 1 /* sample by default */,
        isRemote: true
    });
    return exports.OtelState.tracer.startActiveSpan(name, { attributes: attrs, kind: api_1.SpanKind.SERVER }, parentContext, __assignType(span => runInSpan(span, fn), ['span', '', 'P"2!"/"']));
}
withRemoteSpan.__type = ['name', () => __ΩSpanInfo, 'spanInfo', 'Attributes', 'attrs', '', 'fn', 'withRemoteSpan', 'P&2!n"2#P"w$-J2%P"`/&2\'"`/('];
withSpan.__type = ['name', '', 'fn', 'withSpan', 'P&2!P"`/"2#"`/$'];
withSpan.__type = ['name', 'Attributes', 'attrs', '', 'fn', 'withSpan', 'P&2!P"w"-J2#P"`/$2%"`/&'];
function withSpan(name, attrsOrFn, fn) {
    const resolvedAttrs = typeof attrsOrFn === 'object' ? attrsOrFn : undefined;
    const resolvedFn = fn ?? attrsOrFn;
    if (!exports.OtelState.tracer)
        return resolvedFn();
    return exports.OtelState.tracer.startActiveSpan(name, { attributes: resolvedAttrs }, __assignType(span => runInSpan(span, resolvedFn), ['span', '', 'P"2!"/"']));
}
withSpan.__type = ['name', 'Attributes', '', 'attrsOrFn', 'fn', 'withSpan', 'P&2!P"w"P"/#-J2$P"`/#2%8"`/&'];
withRootSpan.__type = ['name', '', 'fn', 'withRootSpan', 'P&2!P"`/"2#"`/$'];
withRootSpan.__type = ['name', 'Attributes', 'attrs', '', 'fn', 'withRootSpan', 'P&2!P"w"-J2#P"`/$2%"`/&'];
function withRootSpan(name, attrsOrFn, fn) {
    const resolvedAttrs = typeof attrsOrFn === 'object' ? attrsOrFn : undefined;
    const resolvedFn = fn ?? attrsOrFn;
    if (!exports.OtelState.tracer)
        return resolvedFn();
    return exports.OtelState.tracer.startActiveSpan(name, { attributes: resolvedAttrs }, api_1.ROOT_CONTEXT, __assignType(span => runInSpan(span, resolvedFn), ['span', '', 'P"2!"/"']));
}
withRootSpan.__type = ['name', 'Attributes', '', 'attrsOrFn', 'fn', 'withRootSpan', 'P&2!P"w"P"/#-J2$P"`/#2%8"`/&'];
function setSpanAttributes(attributes) {
    if (!exports.OtelState.installed)
        return;
    getActiveSpan()?.setAttributes(attributes);
}
setSpanAttributes.__type = ['Attributes', 'attributes', 'setSpanAttributes', 'P"w!2""/#'];
//# sourceMappingURL=helpers.js.map