import { otelEnabled, otelLogsEnabled, otelMetricsEnabled, otelTracesEnabled } from "../../../otel/src/enabled.js";
import { setupOtelDiagnosticLogger } from "./otel-diag-logger.js";
import { logs } from "@opentelemetry/api-logs";
import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentations-node";
import { resourceFromAttributes } from "@opentelemetry/resources";
import { NodeSDK } from "@opentelemetry/sdk-node";
import { ATTR_SERVICE_VERSION } from "@opentelemetry/semantic-conventions";
//#region ../zero-cache/src/server/otel-start.ts
var OtelManager = class OtelManager {
	static #instance;
	#started = false;
	constructor() {}
	static getInstance() {
		if (!OtelManager.#instance) OtelManager.#instance = new OtelManager();
		return OtelManager.#instance;
	}
	startOtelAuto(lc) {
		if (this.#started || !otelEnabled()) return;
		this.#started = true;
		const otelLogLevel = process.env.OTEL_LOG_LEVEL;
		delete process.env.OTEL_LOG_LEVEL;
		process.env.OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION ??= "base2_exponential_bucket_histogram";
		const resource = resourceFromAttributes({ [ATTR_SERVICE_VERSION]: process.env.ZERO_SERVER_VERSION ?? "unknown" });
		process.env.OTEL_EXPORTER_OTLP_PROTOCOL ??= "http/json";
		process.env.OTEL_METRICS_EXPORTER ??= otelMetricsEnabled() ? "otlp" : "none";
		process.env.OTEL_TRACES_EXPORTER ??= otelTracesEnabled() ? "otlp" : "none";
		process.env.OTEL_LOGS_EXPORTER ??= otelLogsEnabled() ? "otlp" : "none";
		const sdk = new NodeSDK({
			resource,
			autoDetectResources: true,
			instrumentations: process.env.OTEL_NODE_ENABLED_INSTRUMENTATIONS || process.env.OTEL_NODE_DISABLED_INSTRUMENTATIONS ? [getNodeAutoInstrumentations()] : []
		});
		try {
			sdk.start();
		} finally {
			if (otelLogLevel) process.env.OTEL_LOG_LEVEL = otelLogLevel;
		}
		setupOtelDiagnosticLogger(lc, true);
		logs.getLogger("zero-cache").emit({
			severityText: "INFO",
			body: "OpenTelemetry SDK started successfully"
		});
	}
};
var startOtelAuto = (lc) => OtelManager.getInstance().startOtelAuto(lc);
//#endregion
export { startOtelAuto };

//# sourceMappingURL=otel-start.js.map