UNPKG

applicationinsights

Version:

Microsoft Application Insights module for Node.js

102 lines 5.45 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. Object.defineProperty(exports, "__esModule", { value: true }); exports.ApplicationInsightsConfig = void 0; const api_1 = require("@opentelemetry/api"); const resources_1 = require("@opentelemetry/resources"); const jsonConfig_1 = require("./jsonConfig"); const logLevelParser_1 = require("../util/logLevelParser"); const loggingLevel = "APPLICATIONINSIGHTS_INSTRUMENTATION_LOGGING_LEVEL"; class ApplicationInsightsConfig { constructor(options) { // Default values this.otlpLogExporterConfig = {}; this.otlpMetricExporterConfig = {}; this.otlpTraceExporterConfig = {}; this.enableAutoCollectPerformance = true; this.enableAutoCollectExceptions = true; this.azureMonitorExporterOptions = {}; this.samplingRatio = 1; this.instrumentationOptions = { http: { enabled: true }, azureSdk: { enabled: false }, mongoDb: { enabled: false }, mySql: { enabled: false }, postgreSql: { enabled: false }, redis: { enabled: false }, redis4: { enabled: false }, console: { enabled: false }, bunyan: { enabled: false }, winston: { enabled: false }, }; this._resource = this._getDefaultResource(); // Merge JSON configuration file if available // Check for explicitly passed options when instantiating client if (options) { if (typeof (options.enableAutoCollectExceptions) === "boolean") { this.enableAutoCollectExceptions = options.enableAutoCollectExceptions; } if (typeof (options.enableAutoCollectPerformance) === "boolean") { this.enableAutoCollectPerformance = options.enableAutoCollectPerformance; } this.otlpTraceExporterConfig = Object.assign(this.otlpTraceExporterConfig, options.otlpTraceExporterConfig); this.otlpMetricExporterConfig = Object.assign(this.otlpMetricExporterConfig, options.otlpMetricExporterConfig); this.otlpLogExporterConfig = Object.assign(this.otlpLogExporterConfig, options.otlpLogExporterConfig); // Merge default with provided options this.azureMonitorExporterOptions = Object.assign(this.azureMonitorExporterOptions, options.azureMonitorExporterOptions); this.instrumentationOptions = Object.assign(this.instrumentationOptions, options.instrumentationOptions); this.resource = Object.assign(this.resource, options.resource); this.samplingRatio = options.samplingRatio !== undefined ? options.samplingRatio : this.samplingRatio; // Set console logging level from env var if (process.env[loggingLevel]) { this.instrumentationOptions = Object.assign(Object.assign({}, this.instrumentationOptions), { console: Object.assign(Object.assign({}, this.instrumentationOptions.console), { logSendingLevel: (0, logLevelParser_1.logLevelParser)(process.env[loggingLevel]) }) }); } this._mergeConfig(); } } set resource(resource) { this._resource = this._resource.merge(resource); } /** *Get OpenTelemetry Resource */ get resource() { return this._resource; } _mergeConfig() { try { const jsonConfig = jsonConfig_1.JsonConfig.getInstance(); this.enableAutoCollectPerformance = jsonConfig.enableAutoCollectPerformance !== undefined ? jsonConfig.enableAutoCollectPerformance : this.enableAutoCollectPerformance; this.enableAutoCollectExceptions = jsonConfig.enableAutoCollectExceptions !== undefined ? jsonConfig.enableAutoCollectExceptions : this.enableAutoCollectExceptions; this.otlpTraceExporterConfig = Object.assign(this.otlpTraceExporterConfig, jsonConfig.otlpTraceExporterConfig); this.otlpMetricExporterConfig = Object.assign(this.otlpMetricExporterConfig, jsonConfig.otlpMetricExporterConfig); this.otlpLogExporterConfig = Object.assign(this.otlpLogExporterConfig, jsonConfig.otlpLogExporterConfig); this.samplingRatio = jsonConfig.samplingRatio !== undefined ? jsonConfig.samplingRatio : this.samplingRatio; this.azureMonitorExporterOptions = Object.assign(this.azureMonitorExporterOptions, jsonConfig.azureMonitorExporterOptions); this.instrumentationOptions = Object.assign(this.instrumentationOptions, jsonConfig.instrumentationOptions); } catch (error) { api_1.diag.error("Failed to load JSON config file values.", error); } } _getDefaultResource() { let resource = resources_1.Resource.default(); // Load resource attributes from env const detectResourceConfig = { detectors: [resources_1.envDetectorSync], }; const envResource = (0, resources_1.detectResourcesSync)(detectResourceConfig); resource = resource.merge(envResource); return resource; } } exports.ApplicationInsightsConfig = ApplicationInsightsConfig; //# sourceMappingURL=config.js.map