@azure/monitor-opentelemetry
Version:
Azure Monitor OpenTelemetry (Node.js)
128 lines • 5.92 kB
JavaScript
"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.InternalConfig = void 0;
const resources_1 = require("@opentelemetry/resources");
const jsonConfig_js_1 = require("./jsonConfig.js");
const index_js_1 = require("./logging/index.js");
const resource_detector_azure_1 = require("@opentelemetry/resource-detector-azure");
/**
* Azure Monitor OpenTelemetry Client Configuration
*/
class InternalConfig {
set resource(resource) {
this._resource = this._resource.merge(resource);
}
/**
*Get OpenTelemetry Resource
*/
get resource() {
return this._resource;
}
/**
* Initializes a new instance of the AzureMonitorOpenTelemetryOptions class.
*/
constructor(options) {
this._resource = resources_1.Resource.empty();
// Default values
this.azureMonitorExporterOptions = {};
this.samplingRatio = 1;
this.enableLiveMetrics = true;
this.enableStandardMetrics = true;
this.enableTraceBasedSamplingForLogs = false;
this.enablePerformanceCounters = true;
this.instrumentationOptions = {
http: { enabled: true },
azureSdk: { enabled: false },
mongoDb: { enabled: false },
mySql: { enabled: false },
postgreSql: { enabled: false },
redis: { enabled: false },
redis4: { enabled: false },
};
this._setDefaultResource();
this.browserSdkLoaderOptions = {
enabled: false,
connectionString: "",
};
if (options) {
// 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;
this.browserSdkLoaderOptions = Object.assign(this.browserSdkLoaderOptions, options.browserSdkLoaderOptions);
this.enableLiveMetrics =
options.enableLiveMetrics !== undefined
? options.enableLiveMetrics
: this.enableLiveMetrics;
this.enableStandardMetrics =
options.enableStandardMetrics !== undefined
? options.enableStandardMetrics
: this.enableStandardMetrics;
this.enableTraceBasedSamplingForLogs =
options.enableTraceBasedSamplingForLogs !== undefined
? options.enableTraceBasedSamplingForLogs
: this.enableTraceBasedSamplingForLogs;
this.enablePerformanceCounters =
options.enablePerformanceCounters !== undefined
? options.enablePerformanceCounters
: this.enablePerformanceCounters;
}
// JSON configuration will take precedence over other settings
this._mergeConfig();
}
_mergeConfig() {
try {
const jsonConfig = jsonConfig_js_1.JsonConfig.getInstance();
this.samplingRatio =
jsonConfig.samplingRatio !== undefined ? jsonConfig.samplingRatio : this.samplingRatio;
this.browserSdkLoaderOptions = Object.assign(this.browserSdkLoaderOptions, jsonConfig.browserSdkLoaderOptions);
this.enableLiveMetrics =
jsonConfig.enableLiveMetrics !== undefined
? jsonConfig.enableLiveMetrics
: this.enableLiveMetrics;
this.enableStandardMetrics =
jsonConfig.enableStandardMetrics !== undefined
? jsonConfig.enableStandardMetrics
: this.enableStandardMetrics;
this.enableTraceBasedSamplingForLogs =
jsonConfig.enableTraceBasedSamplingForLogs !== undefined
? jsonConfig.enableTraceBasedSamplingForLogs
: this.enableTraceBasedSamplingForLogs;
this.azureMonitorExporterOptions = Object.assign(this.azureMonitorExporterOptions, jsonConfig.azureMonitorExporterOptions);
this.instrumentationOptions = Object.assign(this.instrumentationOptions, jsonConfig.instrumentationOptions);
}
catch (error) {
index_js_1.Logger.getInstance().error("Failed to load JSON config file values.", error);
}
}
_setDefaultResource() {
var _a;
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);
// Load resource attributes from Azure
const azureResource = (0, resources_1.detectResourcesSync)({
detectors: [resource_detector_azure_1.azureAppServiceDetector, resource_detector_azure_1.azureFunctionsDetector],
});
this._resource = resource.merge(azureResource);
const vmResource = (0, resources_1.detectResourcesSync)({
detectors: [resource_detector_azure_1.azureVmDetector],
});
if (vmResource.asyncAttributesPending) {
(_a = vmResource.waitForAsyncAttributes) === null || _a === void 0 ? void 0 : _a.call(vmResource).then(() => {
this._resource = this._resource.merge(vmResource);
return;
});
}
}
}
exports.InternalConfig = InternalConfig;
//# sourceMappingURL=config.js.map