UNPKG

@vtex/api

Version:
49 lines (48 loc) 1.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getLogClient = void 0; const diagnostics_nodejs_1 = require("@vtex/diagnostics-nodejs"); const telemetry_1 = require("../telemetry"); let logClient; let isInitializing = false; let initPromise = undefined; async function getLogClient(account, workspace, appName) { if (logClient) { return logClient; } if (initPromise) { return initPromise; } isInitializing = true; initPromise = initializeClient(account, workspace, appName); return initPromise; } exports.getLogClient = getLogClient; async function initializeClient(account, workspace, appName) { try { const telemetryClient = await (0, telemetry_1.getTelemetryClient)(); const logsConfig = diagnostics_nodejs_1.Exporters.CreateLogsExporterConfig({ endpoint: process.env.OTEL_EXPORTER_OTLP_ENDPOINT, path: process.env.OTEL_EXPORTER_OTLP_PATH || '/v1/logs', protocol: 'http', interval: 5, timeoutSeconds: 5, headers: { 'Content-Type': 'application/json' }, }); const logsExporter = diagnostics_nodejs_1.Exporters.CreateExporter(logsConfig, 'otlp'); await logsExporter.initialize(); const clientKey = `${account}-${workspace}-${appName}`; logClient = await telemetryClient.newLogsClient({ exporter: logsExporter, loggerName: `node-vtex-api-${clientKey}`, }); return logClient; } catch (error) { console.error('Failed to initialize logs client:', error); throw error; } finally { isInitializing = false; } }