@azure/monitor-opentelemetry
Version:
Azure Monitor OpenTelemetry (Node.js)
124 lines • 5.65 kB
JavaScript
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { Resource, detectResourcesSync, envDetectorSync } from "@opentelemetry/resources";
import { JsonConfig } from "./jsonConfig.js";
import { Logger } from "./logging/index.js";
import { azureAppServiceDetector, azureFunctionsDetector, azureVmDetector, } from "@opentelemetry/resource-detector-azure";
/**
* Azure Monitor OpenTelemetry Client Configuration
*/
export 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 = 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.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) {
Logger.getInstance().error("Failed to load JSON config file values.", error);
}
}
_setDefaultResource() {
var _a;
let resource = Resource.default();
// Load resource attributes from env
const detectResourceConfig = {
detectors: [envDetectorSync],
};
const envResource = detectResourcesSync(detectResourceConfig);
resource = resource.merge(envResource);
// Load resource attributes from Azure
const azureResource = detectResourcesSync({
detectors: [azureAppServiceDetector, azureFunctionsDetector],
});
this._resource = resource.merge(azureResource);
const vmResource = detectResourcesSync({
detectors: [azureVmDetector],
});
if (vmResource.asyncAttributesPending) {
(_a = vmResource.waitForAsyncAttributes) === null || _a === void 0 ? void 0 : _a.call(vmResource).then(() => {
this._resource = this._resource.merge(vmResource);
return;
});
}
}
}
//# sourceMappingURL=config.js.map