UNPKG

applicationinsights

Version:
108 lines 4.04 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. Object.defineProperty(exports, "__esModule", { value: true }); exports.StatsbeatFeaturesManager = void 0; const types_1 = require("../../types"); const types_2 = require("../../shim/types"); /** * Utility class to manage statsbeat features using bitmap flags */ class StatsbeatFeaturesManager { /** * Get the singleton instance of StatsbeatFeaturesManager */ static getInstance() { if (!StatsbeatFeaturesManager.instance) { StatsbeatFeaturesManager.instance = new StatsbeatFeaturesManager(); } return StatsbeatFeaturesManager.instance; } /** * Get the current statsbeat features configuration from environment variable */ getCurrentConfig() { const envValue = process.env[types_1.AZURE_MONITOR_STATSBEAT_FEATURES]; if (envValue) { try { return JSON.parse(envValue); } catch (error) { // If parsing fails, return default values return { instrumentation: types_2.StatsbeatInstrumentation.NONE, feature: types_2.StatsbeatFeature.SHIM }; } } return { instrumentation: types_2.StatsbeatInstrumentation.NONE, feature: types_2.StatsbeatFeature.SHIM }; } /** * Set the statsbeat features environment variable with updated configuration */ setConfig(config) { process.env[types_1.AZURE_MONITOR_STATSBEAT_FEATURES] = JSON.stringify(config); } /** * Enable a specific statsbeat feature by setting the corresponding bit */ enableFeature(feature) { const currentConfig = this.getCurrentConfig(); currentConfig.feature |= feature; // Use bitwise OR to set the bit this.setConfig(currentConfig); } /** * Disable a specific statsbeat feature by clearing the corresponding bit */ disableFeature(feature) { const currentConfig = this.getCurrentConfig(); currentConfig.feature &= ~feature; // Use bitwise AND with NOT to clear the bit this.setConfig(currentConfig); } /** * Check if a specific statsbeat feature is enabled */ isFeatureEnabled(feature) { const currentConfig = this.getCurrentConfig(); return (currentConfig.feature & feature) !== 0; } /** * Enable a specific statsbeat instrumentation by setting the corresponding bit */ enableInstrumentation(instrumentation) { const currentConfig = this.getCurrentConfig(); currentConfig.instrumentation |= instrumentation; // Use bitwise OR to set the bit this.setConfig(currentConfig); } /** * Disable a specific statsbeat instrumentation by clearing the corresponding bit */ disableInstrumentation(instrumentation) { const currentConfig = this.getCurrentConfig(); currentConfig.instrumentation &= ~instrumentation; // Use bitwise AND with NOT to clear the bit this.setConfig(currentConfig); } /** * Check if a specific statsbeat instrumentation is enabled */ isInstrumentationEnabled(instrumentation) { const currentConfig = this.getCurrentConfig(); return (currentConfig.instrumentation & instrumentation) !== 0; } /** * Initialize the statsbeat features environment variable with default values if not set */ initialize() { if (!process.env[types_1.AZURE_MONITOR_STATSBEAT_FEATURES]) { this.setConfig({ instrumentation: types_2.StatsbeatInstrumentation.NONE, feature: types_2.StatsbeatFeature.SHIM }); } } } exports.StatsbeatFeaturesManager = StatsbeatFeaturesManager; //# sourceMappingURL=statsbeatFeaturesManager.js.map