Repository: microsoft/ApplicationInsights-node.js (ts)

Issue:
Unable to Disable Logging by Setting samplingRate to Zero
In certain scenarios, it is useful to completely suppress logging by setting the `samplingRate` to zero. However, the current code prevents this by taking the default value when `samplingRate` is set to zero, as shown in the following segment:

https://github.com/microsoft/ApplicationInsights-node.js/blob/main/src/shared/configuration/config.ts#L112

To address this, we need to modify the code to allow samplingRate to be set to zero without reverting to the default value. This change will provide greater flexibility in controlling logging behavior.


Pull request:
Update Sampling Percentage to Accept 0 as Valid
* Previously setting the sampling percentage as 0 would actually set sampling to 1 as 0 is falsy and would revert to the default value of the sampling percentage.

Resolves #1336 

Patch:
diff --git a/src/shared/configuration/config.ts b/src/shared/configuration/config.ts
index 440b1e5..b88bbb3 100644
--- a/src/shared/configuration/config.ts
+++ b/src/shared/configuration/config.ts
@@ -109,7 +109,7 @@ export class ApplicationInsightsConfig {
                 options.instrumentationOptions
             );
             this.resource = Object.assign(this.resource, options.resource);
-            this.samplingRatio = options.samplingRatio || this.samplingRatio;
+            this.samplingRatio = options.samplingRatio !== undefined ? options.samplingRatio : this.samplingRatio;
 
             // Set console logging level from env var
             if (process.env[loggingLevel]) { 
diff --git a/src/shim/shim-config.ts b/src/shim/shim-config.ts
index 4de17b7..4e2c978 100644
--- a/src/shim/shim-config.ts
+++ b/src/shim/shim-config.ts
@@ -151,8 +151,12 @@ class Config implements IConfig {
             ...options.instrumentationOptions,
             console: { enabled: false },
         };
-        if (this.samplingPercentage) {
-            options.samplingRatio = this.samplingPercentage / 100;
+        const samplingRatio = this.samplingPercentage / 100;
+        if (samplingRatio >= 0 && samplingRatio <= 1) {
+            options.samplingRatio = samplingRatio;
+        } else {
+            options.samplingRatio = 1;
+            this._configWarnings.push("Sampling percentage should be between 0 and 100. Defaulting to 100.");
         }
         options.instrumentationOptions = {
             ...options.instrumentationOptions,


Test patch:
diff --git a/test/unitTests/shim/config.tests.ts b/test/unitTests/shim/config.tests.ts
index ea12d0f..ffba204 100644
--- a/test/unitTests/shim/config.tests.ts
+++ b/test/unitTests/shim/config.tests.ts
@@ -102,6 +102,15 @@ describe("shim/configuration/config", () => {
             assert.equal(options.azureMonitorExporterOptions.disableOfflineStorage, false, "wrong disableOfflineStorage");
         });
 
+        it("should initialize zero sampling percentage", () => {
+            const config = new Config(connectionString);
+            config.samplingPercentage = 0;
+
+            let options = config.parseConfig();
+
+            assert.equal(options.samplingRatio, 0, "wrong samplingRatio");
+        });
+
         it("should activate DEBUG internal logger", () => {
             const env = <{ [id: string]: string }>{};
             process.env = env;
@@ -201,6 +210,14 @@ describe("shim/configuration/config", () => {
             assert.equal(process.env["APPLICATION_INSIGHTS_NO_STANDARD_METRICS"], "disable");
         });
 
+        it("should warn if an invalid sampling percentage is passed in", () => {
+            const config = new Config(connectionString);
+            const warnings = config["_configWarnings"];
+            config.samplingPercentage = 101;
+            config.parseConfig();
+            assert.ok(checkWarnings("Sampling percentage should be between 0 and 100. Defaulting to 100.", warnings), "warning was not raised");
+        });
+
         describe("#Shim unsupported messages", () => {
             it("should warn if disableAppInsights is set", () => {
                 const config = new Config(connectionString);