UNPKG

applicationinsights

Version:

Microsoft Application Insights module for Node.js

66 lines 2.76 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. Object.defineProperty(exports, "__esModule", { value: true }); exports.JsonConfig = void 0; const fs = require("fs"); const path = require("path"); const api_1 = require("@opentelemetry/api"); const ENV_CONFIGURATION_FILE = "APPLICATIONINSIGHTS_CONFIGURATION_FILE"; const ENV_CONTENT = "APPLICATIONINSIGHTS_CONFIGURATION_CONTENT"; class JsonConfig { constructor() { this._loadJsonFile(); } static getInstance() { if (!JsonConfig._instance) { JsonConfig._instance = new JsonConfig(); } return JsonConfig._instance; } _loadJsonFile() { let jsonString = ""; const contentJsonConfig = process.env[ENV_CONTENT]; // JSON string added directly in env variable if (contentJsonConfig) { jsonString = contentJsonConfig; } // JSON file else { const configFileName = "applicationinsights.json"; const rootPath = path.join(__dirname, "../../../"); // Root of folder (__dirname = ../dist-esm/src) let tempDir = path.join(rootPath, configFileName); // default const configFile = process.env[ENV_CONFIGURATION_FILE]; if (configFile) { if (path.isAbsolute(configFile)) { tempDir = configFile; } else { tempDir = path.join(rootPath, configFile); // Relative path to applicationinsights folder } } try { jsonString = fs.readFileSync(tempDir, "utf8"); } catch (err) { api_1.diag.info("Failed to read JSON config file: ", err); } } try { const jsonConfig = JSON.parse(jsonString); this.enableAutoCollectExceptions = jsonConfig.enableAutoCollectExceptions; this.otlpLogExporterConfig = jsonConfig.otlpLogExporterConfig; this.otlpMetricExporterConfig = jsonConfig.otlpMetricExporterConfig; this.otlpTraceExporterConfig = jsonConfig.otlpTraceExporterConfig; this.enableAutoCollectPerformance = jsonConfig.enableAutoCollectPerformance; this.azureMonitorExporterOptions = jsonConfig.azureMonitorExporterOptions; this.samplingRatio = jsonConfig.samplingRatio; this.instrumentationOptions = jsonConfig.instrumentationOptions; } catch (err) { api_1.diag.info("Missing or invalid JSON config file: ", err); } } } exports.JsonConfig = JsonConfig; //# sourceMappingURL=jsonConfig.js.map