From cd826ce38365367e7004771bc62b1bee945e4e08 Mon Sep 17 00:00:00 2001 From: Jackson Weber Date: Wed, 5 Jun 2024 16:37:06 -0700 Subject: [PATCH] Update to send warning if sampling percentage is set incorrectly in the app insights v3. --- src/shared/configuration/config.ts | 3 --- src/shim/shim-config.ts | 8 ++++++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/shared/configuration/config.ts b/src/shared/configuration/config.ts index 90d137cd..b88bbb32 100644 --- a/src/shared/configuration/config.ts +++ b/src/shared/configuration/config.ts @@ -109,9 +109,6 @@ export class ApplicationInsightsConfig { options.instrumentationOptions ); this.resource = Object.assign(this.resource, options.resource); - if (typeof(options.samplingRatio) === "number") { - this.samplingRatio = options.samplingRatio; - } this.samplingRatio = options.samplingRatio !== undefined ? options.samplingRatio : this.samplingRatio; // Set console logging level from env var diff --git a/src/shim/shim-config.ts b/src/shim/shim-config.ts index eca8d03e..efbef798 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 !== undefined) { - 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,