UNPKG

@azure/monitor-opentelemetry

Version:
74 lines 3.03 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. Object.defineProperty(exports, "__esModule", { value: true }); exports.JsonConfig = void 0; const tslib_1 = require("tslib"); /* eslint-disable no-underscore-dangle*/ const fs = tslib_1.__importStar(require("node:fs")); const path = tslib_1.__importStar(require("node:path")); const index_js_1 = require("./logging/index.js"); const module_js_1 = require("./module.js"); const ENV_CONFIGURATION_FILE = "APPLICATIONINSIGHTS_CONFIGURATION_FILE"; const ENV_CONTENT = "APPLICATIONINSIGHTS_CONFIGURATION_CONTENT"; /** * Azure Monitor OpenTelemetry Client Configuration through JSON File * @internal */ class JsonConfig { /** Get Singleton instance */ static getInstance() { if (!JsonConfig._instance) { JsonConfig._instance = new JsonConfig(); } return JsonConfig._instance; } /** * Initializes a new instance of the JsonConfig class. */ constructor() { let jsonString = ""; this._tempDir = ""; 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((0, module_js_1.dirName)(), "../../../"); // Root of folder (__dirname = ../dist-esm/src) this._tempDir = path.join(rootPath, configFileName); // default const configFile = process.env[ENV_CONFIGURATION_FILE]; if (configFile) { if (path.isAbsolute(configFile)) { this._tempDir = configFile; } else { this._tempDir = path.join(rootPath, configFile); // Relative path to applicationinsights folder } } try { jsonString = fs.readFileSync(this._tempDir, "utf8"); } catch (err) { index_js_1.Logger.getInstance().info("Failed to read JSON config file: ", err); } } try { const jsonConfig = JSON.parse(jsonString); this.azureMonitorExporterOptions = jsonConfig.azureMonitorExporterOptions; this.samplingRatio = jsonConfig.samplingRatio; this.instrumentationOptions = jsonConfig.instrumentationOptions; this.browserSdkLoaderOptions = jsonConfig.browserSdkLoaderOptions; this.enableLiveMetrics = jsonConfig.enableLiveMetrics; this.enableStandardMetrics = jsonConfig.enableStandardMetrics; this.enableTraceBasedSamplingForLogs = jsonConfig.enableTraceBasedSamplingForLogs; } catch (err) { index_js_1.Logger.getInstance().info("Missing or invalid JSON config file: ", err); } } } exports.JsonConfig = JsonConfig; //# sourceMappingURL=jsonConfig.js.map