UNPKG

@vtex/diagnostics-nodejs

Version:

Diagnostics library for Node.js applications

104 lines 4.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OTLPExporter = void 0; const tslib_1 = require("tslib"); const exporter_logs_otlp_grpc_1 = require("@opentelemetry/exporter-logs-otlp-grpc"); const exporter_trace_otlp_grpc_1 = require("@opentelemetry/exporter-trace-otlp-grpc"); const exporter_metrics_otlp_grpc_1 = require("@opentelemetry/exporter-metrics-otlp-grpc"); const sdk_logs_1 = require("@opentelemetry/sdk-logs"); const grpc = tslib_1.__importStar(require("@grpc/grpc-js")); const sdk_metrics_1 = require("@opentelemetry/sdk-metrics"); const exporters_1 = require("../types/exporters"); class OTLPExporter { constructor(config) { this.config = config; } async initialize() { this.config.validate(); switch (this.config.telemetryType) { case exporters_1.TelemetryType.METRICS: await this.initializeMetrics(); break; case exporters_1.TelemetryType.LOGS: await this.initializeLogs(); break; case exporters_1.TelemetryType.TRACES: await this.initializeTraces(); break; default: throw new Error(`Unsupported telemetry type: ${this.config.telemetryType}`); } } getCredentials() { const endpoint = this.config.options.endpoint || 'localhost:4317'; if (endpoint.includes('localhost') || endpoint.includes('127.0.0.1') || this.config.options.insecure) { return grpc.credentials.createInsecure(); } return grpc.credentials.createSsl(); } async initializeMetrics() { this.metricExporterInstance = new exporter_metrics_otlp_grpc_1.OTLPMetricExporter({ url: this.config.options.endpoint || 'localhost:4317', credentials: this.getCredentials(), compression: this.config.options.compression || 'gzip', timeoutMillis: (this.config.options.timeoutSeconds || 10) * 1000, }); this.metricReaderInstance = new sdk_metrics_1.PeriodicExportingMetricReader({ exporter: this.metricExporterInstance, exportIntervalMillis: (this.config.options.interval || 10) * 1000, exportTimeoutMillis: (this.config.options.timeoutSeconds || 10) * 1000, }); } async initializeLogs() { this.logExporterInstance = new exporter_logs_otlp_grpc_1.OTLPLogExporter({ url: this.config.options.endpoint || 'localhost:4317', credentials: this.getCredentials(), compression: this.config.options.compression || 'gzip', timeoutMillis: (this.config.options.timeoutSeconds || 10) * 1000, }); this.logProcessorInstance = new sdk_logs_1.BatchLogRecordProcessor(this.logExporterInstance, { maxExportBatchSize: 10, scheduledDelayMillis: 500, exportTimeoutMillis: (this.config.options.timeoutSeconds || 10) * 1000, maxQueueSize: 1000 }); } async initializeTraces() { this.traceExporterInstance = new exporter_trace_otlp_grpc_1.OTLPTraceExporter({ url: this.config.options.endpoint || 'localhost:4317', credentials: this.getCredentials(), compression: this.config.options.compression || 'gzip', timeoutMillis: (this.config.options.timeoutSeconds || 10) * 1000, }); } logExporter() { return this.logExporterInstance; } metricReader() { return this.metricReaderInstance; } logProcessor() { return this.logProcessorInstance; } traceExporter() { if (!this.traceExporterInstance) { throw new Error('Trace exporter not initialized. Call initialize() first.'); } return this.traceExporterInstance; } async forceFlush() { if (this.logProcessorInstance) { await this.logProcessorInstance.forceFlush(); } } async shutdown() { try { await this.forceFlush(); } catch (error) { } await this.metricReaderInstance?.shutdown(); await this.logProcessorInstance?.shutdown(); } } exports.OTLPExporter = OTLPExporter; //# sourceMappingURL=otlp.js.map