import { DisableTelemetry, OpenTelemetryExporterOtlpEndpoint, OpenTelemetryExporterOtlpHeaders, } from "../../Config";
import { ZoneContextManager } from "@opentelemetry/context-zone";
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
import { registerInstrumentations } from "@opentelemetry/instrumentation";
import { FetchInstrumentation } from "@opentelemetry/instrumentation-fetch";
import { XMLHttpRequestInstrumentation } from "@opentelemetry/instrumentation-xml-http-request";
import { Resource } from "@opentelemetry/resources";
import { BatchSpanProcessor, WebTracerProvider, } from "@opentelemetry/sdk-trace-web";
import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
import URL from "../../../Types/API/URL";
/*
 * Mutable, module-level bag of attributes stamped onto every browser span.
 * Populated by `Telemetry.setGlobalAttributes` once the signed-in user and
 * the project being viewed are known (RUM context).
 */
const globalSpanAttributes = {};
/**
 * Stamps the global RUM attributes (userId, projectId, ...) onto each span at
 * creation, mirroring the server-side ContextSpanProcessor.
 */
class GlobalAttributeSpanProcessor {
    onStart(span) {
        for (const key in globalSpanAttributes) {
            const value = globalSpanAttributes[key];
            if (value) {
                span.setAttribute(key, value);
            }
        }
    }
    onEnd() {
        // no-op
    }
    forceFlush() {
        return Promise.resolve();
    }
    shutdown() {
        return Promise.resolve();
    }
}
export default class Telemetry {
    /**
     * Set RUM attributes that should be attached to every browser span from now
     * on (e.g. userId, projectId). Safe to call before or after `init`; empty
     * values are ignored.
     */
    static setGlobalAttributes(attributes) {
        for (const key in attributes) {
            const value = attributes[key];
            if (value) {
                globalSpanAttributes[key] = value;
            }
        }
    }
    static init(data) {
        if (DisableTelemetry) {
            return;
        }
        const hasHeaders = Object.keys(OpenTelemetryExporterOtlpHeaders).length > 0;
        if (OpenTelemetryExporterOtlpEndpoint && hasHeaders) {
            const providerConfig = {
                resource: new Resource({
                    [ATTR_SERVICE_NAME]: data.serviceName,
                }),
            };
            const provider = new WebTracerProvider(providerConfig);
            const traceExporter = new OTLPTraceExporter({
                url: URL.fromString((OpenTelemetryExporterOtlpEndpoint === null || OpenTelemetryExporterOtlpEndpoint === void 0 ? void 0 : OpenTelemetryExporterOtlpEndpoint.toString()) + "/v1/traces").toString(),
                headers: OpenTelemetryExporterOtlpHeaders,
            });
            const webTraceExporter = traceExporter;
            provider.addSpanProcessor(new BatchSpanProcessor(webTraceExporter));
            // Stamp global RUM attributes (userId, projectId, ...) onto every span.
            provider.addSpanProcessor(new GlobalAttributeSpanProcessor());
            provider.register({
                contextManager: new ZoneContextManager(),
            });
            registerInstrumentations({
                instrumentations: [
                    new FetchInstrumentation(),
                    new XMLHttpRequestInstrumentation(),
                ],
            });
        }
    }
}
//# sourceMappingURL=Telemetry.js.map