@azure/monitor-opentelemetry
Version:
Azure Monitor OpenTelemetry (Node.js)
44 lines • 2.34 kB
JavaScript
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.patchOpenTelemetryInstrumentationEnable = patchOpenTelemetryInstrumentationEnable;
const types_js_1 = require("../types.js");
const index_js_1 = require("../shared/logging/index.js");
/**
* Patch OpenTelemetry Instrumentation enablement to update the statsbeat environment variable with the enabled instrumentations
* @internal
*/
function patchOpenTelemetryInstrumentationEnable() {
const emptyStatsbeatConfig = JSON.stringify({ instrumentation: 0, feature: 0 });
try {
require.resolve("@opentelemetry/instrumentation");
// eslint-disable-next-line @typescript-eslint/no-require-imports
const autoLoaderUtils = require("@opentelemetry/instrumentation/build/src/autoLoaderUtils");
const originalModuleDefinition = autoLoaderUtils.enableInstrumentations;
// Parses the enabled instrumentations and then ammends the statsbeat instrumentation environment variable
autoLoaderUtils.enableInstrumentations = function (instrumentations) {
try {
const statsbeatOptions = JSON.parse(process.env[types_js_1.AZURE_MONITOR_STATSBEAT_FEATURES] || emptyStatsbeatConfig);
let updatedStatsbeat = {};
for (let i = 0; i < instrumentations.length; i++) {
updatedStatsbeat = {
instrumentation: (statsbeatOptions.instrumentation |=
types_js_1.StatsbeatInstrumentationMap.get(instrumentations[i].instrumentationName) || 0),
feature: statsbeatOptions.feature,
};
}
process.env[types_js_1.AZURE_MONITOR_STATSBEAT_FEATURES] = JSON.stringify(updatedStatsbeat);
}
catch (e) {
index_js_1.Logger.getInstance().warn("Failed to parse the statsbeat environment variable");
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, prefer-rest-params
return originalModuleDefinition.apply(this, arguments);
};
}
catch (e) {
// Fail silently if the module is not found
}
}
//# sourceMappingURL=opentelemetryInstrumentationPatcher.js.map
;