"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HoneycombTelemetryAdapter = void 0;
const sdk_node_1 = require("@opentelemetry/sdk-node");
const exporter_trace_otlp_proto_1 = require("@opentelemetry/exporter-trace-otlp-proto");
const resources_1 = require("@opentelemetry/resources");
const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
const logger_1 = require("../logger");
/**
 * Honeycomb telemetry adapter.
 *
 * Configures the OpenTelemetry Node.js SDK with an OTLP exporter
 * pointed at Honeycomb. Once connected, all spans created by HotMesh
 * (workflow executions, activity calls, stream routing) are
 * automatically exported to Honeycomb.
 *
 * HotMesh uses `@opentelemetry/api` internally to create spans. This
 * adapter provides the TracerProvider + exporter that captures them.
 *
 * Usage:
 * ```typescript
 * import { telemetryRegistry } from './lib/telemetry';
 * import { HoneycombTelemetryAdapter } from './lib/telemetry/honeycomb';
 *
 * telemetryRegistry.register(new HoneycombTelemetryAdapter({
 *   apiKey: process.env.HONEYCOMB_API_KEY,
 *   serviceName: 'my-app',
 * }));
 * await telemetryRegistry.connect(); // before starting workers
 * ```
 */
class HoneycombTelemetryAdapter {
    constructor(options) {
        this.sdk = null;
        this.apiKey = options?.apiKey || process.env.HONEYCOMB_API_KEY || '';
        this.serviceName = options?.serviceName || 'long-tail';
        this.endpoint = options?.endpoint || 'https://api.honeycomb.io';
        this.traceUrl = options?.traceUrl || HoneycombTelemetryAdapter.deriveTraceUrl(this.serviceName);
    }
    /**
     * Auto-derive the Honeycomb trace URL from standard env vars.
     * Returns undefined if HONEYCOMB_TEAM or HONEYCOMB_ENVIRONMENT are not set.
     */
    static deriveTraceUrl(dataset) {
        const team = process.env.HONEYCOMB_TEAM;
        const env = process.env.HONEYCOMB_ENVIRONMENT;
        if (!team || !env)
            return undefined;
        return `https://ui.honeycomb.io/${team}/environments/${env}/datasets/${dataset}/trace?trace_id={traceId}`;
    }
    async connect() {
        if (!this.apiKey) {
            logger_1.loggerRegistry.warn('[telemetry] HONEYCOMB_API_KEY not set — skipping Honeycomb telemetry');
            return;
        }
        const exporter = new exporter_trace_otlp_proto_1.OTLPTraceExporter({
            url: `${this.endpoint}/v1/traces`,
            headers: {
                'x-honeycomb-team': this.apiKey,
            },
        });
        this.sdk = new sdk_node_1.NodeSDK({
            resource: (0, resources_1.resourceFromAttributes)({
                [semantic_conventions_1.ATTR_SERVICE_NAME]: this.serviceName,
            }),
            traceExporter: exporter,
        });
        this.sdk.start();
        logger_1.loggerRegistry.info(`[telemetry] Honeycomb connected (service: ${this.serviceName})`);
    }
    async disconnect() {
        if (!this.sdk)
            return;
        await this.sdk.shutdown();
        this.sdk = null;
        logger_1.loggerRegistry.info('[telemetry] Honeycomb disconnected');
    }
}
exports.HoneycombTelemetryAdapter = HoneycombTelemetryAdapter;
