// engine/observability/tracer.ts — Active tracer singleton + lifecycle (SPEC-165)
// Provides the global Tracer instance. Defaults to NoopTracer until configured.
import { NoopTracer } from './noop-tracer.js';
import { OtelTracer } from './otel-tracer.js';
// ---------------------------------------------------------------------------
// Singleton
// ---------------------------------------------------------------------------
let _tracer = new NoopTracer();
/**
 * Returns the active global tracer.
 * Defaults to NoopTracer — zero overhead when not configured.
 */
export function getTracer() {
    return _tracer;
}
/**
 * Initializes the global tracer from an OtelConfig.
 * If enabled=false or no endpoint, installs NoopTracer.
 * If enabled=true, attempts to build an OtelTracer (requires @opentelemetry/api).
 * Falls back to NoopTracer if the package is not installed.
 *
 * This function is async because OtelTracer performs a dynamic import.
 */
export async function initTracer(config) {
    if (!config.enabled || !config.endpoint) {
        _tracer = new NoopTracer();
        return;
    }
    _tracer = await OtelTracer.create(config);
}
/**
 * Replaces the active tracer with the provided instance.
 * Primarily for testing — allows injecting a custom or spy tracer.
 */
export function setTracer(tracer) {
    _tracer = tracer;
}
/**
 * Resets the global tracer to NoopTracer.
 * Call in test afterEach/teardown to avoid state leakage.
 */
export function resetTracer() {
    _tracer = new NoopTracer();
}
//# sourceMappingURL=tracer.js.map