// import * as os from "os";
// import { Config } from "../Config";
// import { NoderedUtil } from "@openiap/openflow-api";
// import { BasicTracerProvider, BatchSpanProcessor, ReadableSpan, SpanExporter } from "@opentelemetry/tracing";
// import { Meter, MeterProvider } from "@opentelemetry/metrics";
// import { Labels } from "@opentelemetry/api-metrics";
// import { CollectorMetricExporter, CollectorTraceExporter } from "@opentelemetry/exporter-collector-grpc";
// import { diag, DiagConsoleLogger, DiagLogLevel, HrTime, Span, setSpan } from "@opentelemetry/api";
// import { hrTime, hrTimeToMilliseconds } from "@opentelemetry/core";
// import { ValueRecorder, BaseObserver } from "@opentelemetry/api-metrics";
// import opentelemetry from "@opentelemetry/api"
// import { ExportResult } from '@opentelemetry/core';
// import { MetricExporter, MetricRecord } from '@opentelemetry/metrics';
// import { Logger } from "../Logger";
// import { i_otel } from "../commoninterfaces.js";
// // import { setSpan } from "@opentelemetry/api/build/src/trace/context-utils";
// export class MultiMetricExporter implements MetricExporter {
//     private exporters: MetricExporter[];
//     private isshutdown: boolean = false;
//     constructor(exporters: MetricExporter[]) {
//         this.exporters = exporters;
//     }
//     export(
//         record: MetricRecord[],
//         callback: (result: ExportResult) => void,
//     ): void {
//         this.exporters.forEach((exporter) => {
//             exporter.export(record, callback);
//         });
//     }
//     async shutdown(): Promise<void> {
//         this.isshutdown = true;
//         await Promise.all(this.exporters.map((e) => e.shutdown()));
//     }
// }
// export class MultiSpanExporter implements SpanExporter {
//     private exporters: SpanExporter[];
//     constructor(exporters: SpanExporter[]) {
//         this.exporters = exporters;
//     }
//     export(
//         spans: ReadableSpan[],
//         callback: (result: ExportResult) => void,
//     ): void {
//         this.exporters.forEach((exporter) => {
//             exporter.export(spans, callback);
//         });
//     }
//     async shutdown(): Promise<void> {
//         await Promise.all(this.exporters.map((e) => e.shutdown()));
//     }
// }
// export class otel implements i_otel {
//     public default_boundaries = [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 25, 50, 75, 100]
//     public static fakespan = {
//         context: () => undefined,
//         setAttribute: () => undefined,
//         setAttributes: () => undefined,
//         addEvent: () => undefined,
//         setStatus: () => undefined,
//         updateName: () => undefined,
//         end: () => undefined,
//         isRecording: () => undefined,
//         recordException: () => undefined,
//     };
//     public static instance: otel;
//     public traceprovider: BasicTracerProvider;
//     public meterprovider: MeterProvider;
//     public meter: Meter;
//     public defaultlabels: any = {};
//     private static nodejs_heap_size_used_bytes: BaseObserver;
//     private static nodejs_heap_size_total_bytes: BaseObserver;
//     private static perfTimeout: NodeJS.Timeout;
//     private static traceExporter: MultiSpanExporter;
//     private static metricExporter: MultiMetricExporter;
//     static configure(): otel {
//         let hasLicense: boolean = true;
//         try {
//             Logger.License.validate();
//         } catch (error) {
//             hasLicense = false;
//             // Logger.instanse.debug("otel", "configure", error.message ? error.message : error);
//             Logger.instanse.info("otel", "configure", error.message ? error.message : error);
//         }
//         if (otel.instance != null) return otel.instance;
//         otel.instance = new otel();
//         if (Config.otel_debug_log) {
//             diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);
//             // diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.INFO);
//         } else if (Config.otel_warn_log) {
//             diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.WARN);
//         } else if (Config.otel_err_log) {
//             diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.ERROR);
//         }
//         const metricExporters: any[] = [];
//         const spanExporters: any[] = [];
//         if (hasLicense) {
//             if (!NoderedUtil.IsNullEmpty(Config.otel_metric_url)) {
//                 metricExporters.push(new CollectorMetricExporter({
//                     url: Config.otel_metric_url
//                 }));
//                 Logger.instanse.info("otel", "configure", "Added local metric exporter for " + Config.otel_metric_url);
//             }
//             if (!NoderedUtil.IsNullEmpty(Config.otel_trace_url)) {
//                 spanExporters.push(new CollectorTraceExporter({
//                     url: Config.otel_trace_url
//                 }));
//                 Logger.instanse.info("otel", "configure", "Added local span exporter for " + Config.otel_trace_url);
//             }
//         } else if (!NoderedUtil.IsNullEmpty(Config.otel_metric_url) || !NoderedUtil.IsNullEmpty(Config.otel_trace_url)) {
//             Logger.instanse.warn("otel", "configure", "otel settings ignored due to missing license");
//         }
//         // https://github.com/open-telemetry/opentelemetry-js/issues/2271
//         if (Config.enable_analytics) {
//             Logger.instanse.verbose("otel", "configure", "analytics enabled, add exporters");
//             //     url: "https://exporter.stats.openiap.io/v1/metrics"
//             //     url: "https://exporter.stats.openiap.io/v1/trace"
//             // metricExporters.push(new CollectorMetricExporter({
//             //     url: "http://otel.stats.openiap.io:80"
//             // }));
//             // spanExporters.push(new CollectorTraceExporter({
//             //     url: "http://otel.stats.openiap.io:80"
//             // }));
//             metricExporters.push(new CollectorMetricExporter({
//                 url: "http://otel.openiap.io:80"
//             }));
//             spanExporters.push(new CollectorTraceExporter({
//                 url: "http://otel.openiap.io:80"
//             }));
//         } else {
//             Logger.instanse.warn("otel", "configure", "analytics disabled, please consider enabling this, to help us improve the product!");
//         }
//         Logger.instanse.verbose("otel", "configure", "setdefaultlabels");
//         otel.instance.setdefaultlabels();
//         if (spanExporters.length > 0) {
//             Logger.instanse.verbose("otel", "configure", "add span exportes");
//             this.traceExporter = new MultiSpanExporter(spanExporters);
//             otel.instance.traceprovider = new BasicTracerProvider();
//             otel.instance.traceprovider.addSpanProcessor(new BatchSpanProcessor(this.traceExporter, {
//                 maxExportBatchSize: 512,
//                 scheduledDelayMillis: Config.otel_trace_interval,
//             }));
//             otel.instance.traceprovider.resource.attributes['service.name'] = 'openflow';
//             // otel.instance.traceprovider.addSpanProcessor(new SimpleSpanProcessor(traceExporter));
//             Logger.instanse.verbose("otel", "configure", "analytics enabled, register span exportes");
//             otel.instance.traceprovider.register();
//         }
//         if (metricExporters.length > 0) {
//             Logger.instanse.verbose("otel", "configure", "add metric exportes");
//             this.metricExporter = new MultiMetricExporter(metricExporters);
//             otel.instance.meterprovider = new MeterProvider({
//                 exporter: this.metricExporter,
//                 interval: Config.otel_metric_interval
//             });
//             otel.instance.meterprovider.resource.attributes['service.name'] = 'openflow';
//             Logger.instanse.verbose("otel", "configure", "add memory meters for metric exportes");
//             otel.instance.meter = otel.instance.meterprovider.getMeter("openflow");
//             otel.nodejs_heap_size_used_bytes = otel.instance.meter.createUpDownSumObserver('nodejs_heap_size_used_bytes', {
//                 description: 'Process heap size used from Node.js in bytes'
//             });
//             otel.nodejs_heap_size_total_bytes = otel.instance.meter.createUpDownSumObserver('nodejs_heap_size_total_bytes', {
//                 description: 'Process heap size from Node.js in bytes'
//             });
//             if (otel.perfTimeout) clearInterval(otel.perfTimeout)
//             otel.perfTimeout = setInterval(() => {
//                 const memUsage = process.memoryUsage();
//                 otel.nodejs_heap_size_used_bytes.bind({ ...otel.instance.defaultlabels, domain: Config.domain }).update(memUsage.heapUsed);
//                 otel.nodejs_heap_size_total_bytes.bind({ ...otel.instance.defaultlabels, domain: Config.domain }).update(memUsage.heapTotal);
//             }, Config.otel_metric_interval)
//         }
//         return otel.instance;
//     }
//     async shutdown(): Promise<void> {
//         Logger.instanse.verbose("otel", "shutdown", "shutdown started");
//         if (otel.perfTimeout) clearInterval(otel.perfTimeout)
//         try {
//             // if (otel.instance.traceprovider) await otel.instance.traceprovider.shutdown();
//         } catch (error) {
//             Logger.instanse.error("otel", "shutdown", error);
//         }
//         try {
//             if (otel.instance.meterprovider) await otel.instance.meterprovider.shutdown();
//         } catch (error) {
//             Logger.instanse.error("otel", "shutdown", error);
//         }
//         try {
//             if (otel.traceExporter) await otel.traceExporter.shutdown();
//         } catch (error) {
//             Logger.instanse.error("otel", "shutdown", error);
//         }
//         try {
//             if (otel.metricExporter) await otel.metricExporter.shutdown();
//         } catch (error) {
//             Logger.instanse.error("otel", "shutdown", error);
//         }
//         Logger.instanse.verbose("otel", "shutdown", "shutdown complete");
//     }
//     startSpan(name: string): Span {
//         if (NoderedUtil.IsNullUndefinded(otel.instance)) return otel.fakespan as any;
//         if (NoderedUtil.IsNullUndefinded(otel.instance.traceprovider)) return otel.fakespan as any;
//         const span: Span = opentelemetry.trace.getTracer('default').startSpan(name);
//         span?.setAttributes(this.defaultlabels);
//         return span;
//     }
//     startSubSpan(name: string, parent: Span): Span {
//         // if (NoderedUtil.IsNullUndefinded(parent)) return null;
//         if (NoderedUtil.IsNullUndefinded(parent)) return this.startSpan(name);
//         const ctx = setSpan(opentelemetry.context.active(), parent);
//         const span = opentelemetry.trace.getTracer('default').startSpan(name, undefined, ctx);
//         Logger.instanse.silly("otel", "startSubSpan", (span as any).spanContext?.traceId + " " + (span as any).spanContext?.spanId + " " + name);
//         // span?.setAttributes(otel.defaultlabels);
//         return span;
//     }
//     endSpan(span: Span): void {
//         try {
//             if (!NoderedUtil.IsNullUndefinded(span)) span?.end();
//             // span = null;
//         } catch (error) {
//             Logger.instanse.error("otel", "endSpan", error)
//         }
//     }
//     startTimer(): HrTime {
//         return hrTime();
//     }
//     endTimer(startTime: HrTime, recorder: ValueRecorder, labels?: Labels) {
//         if (NoderedUtil.IsNullUndefinded(otel.instance)) return undefined;
//         if (NoderedUtil.IsNullUndefinded(recorder)) return undefined;
//         const performanceTimeOriginms = hrTimeToMilliseconds(startTime);
//         const Milliseconds = (hrTimeToMilliseconds(hrTime()) - performanceTimeOriginms)
//         const Seconds = Milliseconds / 1000;
//         recorder.bind({ ...this.defaultlabels, ...labels }).record(Seconds);
//         Logger.instanse.silly("otel", "endTimer", JSON.stringify(labels) + " " + Seconds)
//     }
//     setdefaultlabels() {
//         const hostname = (Config.getEnv("HOSTNAME", undefined) || os.hostname()) || "unknown";
//         var crypto = require('crypto');
//         this.defaultlabels["ofid"] = Logger.License.ofid(false);
//         Logger.instanse.info("otel", "setdefaultlabels", "ofid=" + Logger.License.ofid(false));
//         // const uniqueid = crypto.createHash('md5').update(Logger.License.ofid(false) + hostname).digest("hex");
//         // this.defaultlabels[uniqueid] = uniqueid;
//         if (!NoderedUtil.IsNullEmpty(hostname)) {
//             this.defaultlabels["hostname"] = hostname;
//         }
//     }
// }
//# sourceMappingURL=otel%20copy.js.map