From f2565757ebf26f95e3b5ce4e16341188b36139ae Mon Sep 17 00:00:00 2001 From: Karlie-777 <79606506+Karlie-777@users.noreply.github.com> Date: Mon, 22 Apr 2024 16:29:05 -0700 Subject: [PATCH] change cfg sync mode (#2333) --- .../Unit/src/applicationinsights.e2e.tests.ts | 1 + AISKU/src/AISku.ts | 5 +- .../Tests/Unit/src/cfgsyncplugin.tests.ts | 126 +++++++++++++++++- .../src/CfgSyncPlugin.ts | 4 +- 4 files changed, 129 insertions(+), 7 deletions(-) diff --git a/AISKU/Tests/Unit/src/applicationinsights.e2e.tests.ts b/AISKU/Tests/Unit/src/applicationinsights.e2e.tests.ts index be0c36b4e..80d2f8454 100644 --- a/AISKU/Tests/Unit/src/applicationinsights.e2e.tests.ts +++ b/AISKU/Tests/Unit/src/applicationinsights.e2e.tests.ts @@ -271,6 +271,7 @@ export class ApplicationInsightsTests extends AITestClass { ai.config.extensionConfig = ai.config.extensionConfig || {}; let extConfig = ai.config.extensionConfig["AppInsightsCfgSyncPlugin"]; Assert.equal(extConfig.cfgUrl, CONFIG_ENDPOINT_URL, "default cdn endpoint should be set"); + Assert.equal(extConfig.syncMode, 2, "default mode should be set to receive"); let featureOptIn = config.featureOptIn || {}; Assert.equal(featureOptIn["iKeyUsage"].mode, FeatureOptInMode.enable, "ikey message should be turned on"); diff --git a/AISKU/src/AISku.ts b/AISKU/src/AISku.ts index 5491b23cc..6b91f2700 100644 --- a/AISKU/src/AISku.ts +++ b/AISKU/src/AISku.ts @@ -4,7 +4,7 @@ import dynamicProto from "@microsoft/dynamicproto-js"; import { AnalyticsPlugin, ApplicationInsights } from "@microsoft/applicationinsights-analytics-js"; -import { CfgSyncPlugin, ICfgSyncConfig } from "@microsoft/applicationinsights-cfgsync-js"; +import { CfgSyncPlugin, ICfgSyncConfig, ICfgSyncMode } from "@microsoft/applicationinsights-cfgsync-js"; import { Sender } from "@microsoft/applicationinsights-channel-js"; import { AnalyticsPluginIdentifier, DEFAULT_BREEZE_PATH, IAutoExceptionTelemetry, IConfig, IDependencyTelemetry, IEventTelemetry, @@ -92,7 +92,8 @@ const defaultConfigValues: IConfigDefaults = { ), extensionConfig: cfgDfMerge<{[key: string]: any}>({ ["AppInsightsCfgSyncPlugin"]: cfgDfMerge({ - cfgUrl: CONFIG_ENDPOINT_URL + cfgUrl: CONFIG_ENDPOINT_URL, + syncMode: ICfgSyncMode.Receive }) }) }; diff --git a/extensions/applicationinsights-cfgsync-js/Tests/Unit/src/cfgsyncplugin.tests.ts b/extensions/applicationinsights-cfgsync-js/Tests/Unit/src/cfgsyncplugin.tests.ts index 15a68abfe..8889f88a1 100644 --- a/extensions/applicationinsights-cfgsync-js/Tests/Unit/src/cfgsyncplugin.tests.ts +++ b/extensions/applicationinsights-cfgsync-js/Tests/Unit/src/cfgsyncplugin.tests.ts @@ -1,5 +1,5 @@ import { Assert, AITestClass, IFetchArgs, PollingAssert } from "@microsoft/ai-test-framework"; -import { AppInsightsCore, IAppInsightsCore, IPlugin, ITelemetryItem, getGlobal, getGlobalInst } from "@microsoft/applicationinsights-core-js"; +import { AppInsightsCore, FeatureOptInMode, IAppInsightsCore, IPlugin, ITelemetryItem, getGlobal, getGlobalInst } from "@microsoft/applicationinsights-core-js"; import { IConfiguration } from "@microsoft/applicationinsights-core-js"; import { CfgSyncPlugin } from "../../../../applicationinsights-cfgsync-js/src/applicationinsights-cfgsync-js"; import { ICfgSyncConfig, ICfgSyncMode, NonOverrideCfg } from "../../../src/Interfaces/ICfgSyncConfig"; @@ -10,7 +10,7 @@ import { createSyncPromise } from "@nevware21/ts-async"; export class CfgSyncPluginTests extends AITestClass { private core: AppInsightsCore; - private _config: IConfiguration; + private _config: IConfiguration & IConfig; private mainInst: CfgSyncPlugin; private identifier: string; private _channel: ChannelPlugin; @@ -271,7 +271,7 @@ export class CfgSyncPluginTests extends AITestClass { let fetchStub = this._context["fetchStub"]; let patchEvnSpy = this._context["patchEvnSpy"]; let config = { - instrumentationKey:"testIkey", + //instrumentationKey:"testIkey", // should not be override enableAjaxPerfTracking: true } as IConfiguration & IConfig; if (fetchStub.called && patchEvnSpy.called) { @@ -330,7 +330,7 @@ export class CfgSyncPluginTests extends AITestClass { let fetchStub = this._context["fetchStub"]; let patchEvnSpy = this._context["patchEvnSpy"]; let config = { - instrumentationKey:"testIkey", + //instrumentationKey:"testIkey", // should not be override enableAjaxPerfTracking: true } as IConfiguration & IConfig; if (fetchStub.called && patchEvnSpy.called) { @@ -524,6 +524,124 @@ export class CfgSyncPluginTests extends AITestClass { }, "response received", 60, 100) as any) }); + this.testCaseAsync({ + name: "CfgSyncPlugin: Test with current cfgSync CDN v1", + stepDelay: 10, + useFakeTimers: true, + useFakeServer: true, + steps: [ () => { + + let doc = getGlobal(); + this.onDone(() => { + this.core.unload(false); + }); + let fetchStub = this.sandbox.spy((doc as any), "fetch"); + + this._config.featureOptIn = {["iKeyUsage"]: {mode: FeatureOptInMode.enable}, ["CdnUsage"]:{mode: FeatureOptInMode.disable}} + this._config.throttleMgrCfg = { + ["109"]: {disabled: true}, + ["106"]: {disabled: true}, + ["110"]: {disabled: true} + } + this._config.extensionConfig = { [this.identifier]: { + syncMode: ICfgSyncMode.Receive, + cfgUrl: "https://js.monitor.azure.com/scripts/b/ai.config.1.cfg.json", + scheduleFetchTimeout: 10000 + }}; + this._context["fetchStub"] = fetchStub; + this.core.initialize(this._config, [this._channel]); + + }].concat(PollingAssert.createPollingAssert(() => { + let fetchStub = this._context["fetchStub"]; + Assert.equal(fetchStub.callCount, 1, "fetch is should called once"); + if (fetchStub.called) { + return true; + } + return false; + }, "wait for fetch response", 60, 100) as any).concat(PollingAssert.createPollingAssert(() => { + let coreConfig = this.core.config as IConfig & IConfiguration; + let featureOptIn = coreConfig.featureOptIn || {}; + let throttleMgrConfig = coreConfig.throttleMgrCfg || {}; + let ikeyOptIn = featureOptIn["iKeyUsage"]; + let defaultIkey = throttleMgrConfig["109"]; + let defaultEnabled = !defaultIkey.disabled; + let onConfig = ikeyOptIn.onCfg; + let offConfig = ikeyOptIn.onCfg; + if (onConfig && offConfig && defaultEnabled) { + let ikeyMsg = throttleMgrConfig["106"]; + Assert.equal(ikeyMsg.disabled, false, "ikey msg should be enabled"); + let otherMsg = throttleMgrConfig["110"]; + Assert.equal(otherMsg.disabled, true, "other msg should be disabled"); + let cdnOptIn = featureOptIn["CdnUsage"]; + Assert.equal(cdnOptIn.mode, 2, "cdn feature optin should be disabled"); + Assert.equal(ikeyOptIn.mode, 3, "ikey feature optin should be enabled"); + return true; + } + return false; + }, "wait for core config update", 60, 100) as any) + }); + + this.testCaseAsync({ + name: "CfgSyncPlugin: NonOverride values should not be changed Test with current cfgSync CDN v1", + stepDelay: 10, + useFakeTimers: true, + useFakeServer: true, + steps: [ () => { + + let doc = getGlobal(); + this.onDone(() => { + this.core.unload(false); + }); + let fetchStub = this.sandbox.spy((doc as any), "fetch"); + this._config.throttleMgrCfg = {}; + this._config.featureOptIn = {["iKeyUsage"]: {mode: FeatureOptInMode.enable}, ["CdnUsage"]:{mode: FeatureOptInMode.disable}} + this._config.throttleMgrCfg = { + ["109"]: {disabled: true}, + ["106"]: {disabled: true}, + ["110"]: {disabled: true} + } + + this._config.extensionConfig = { [this.identifier]: { + syncMode: ICfgSyncMode.Receive, + cfgUrl: "https://js.monitor.azure.com/scripts/b/ai.config.1.cfg.json", + scheduleFetchTimeout: 10000, + nonOverrideConfigs: {throttleMgrCfg: true} + + } as ICfgSyncConfig}; + this._context["fetchStub"] = fetchStub; + this.core.initialize(this._config, [this._channel]); + + }].concat(PollingAssert.createPollingAssert(() => { + let fetchStub = this._context["fetchStub"]; + Assert.equal(fetchStub.callCount, 1, "fetch is should called once"); + if (fetchStub.called) { + return true; + } + return false; + }, "wait for fetch response", 60, 100) as any).concat(PollingAssert.createPollingAssert(() => { + let coreConfig = this.core.config as IConfig & IConfiguration; + let featureOptIn = coreConfig.featureOptIn || {}; + let throttleMgrConfig = coreConfig.throttleMgrCfg || {}; + let ikeyOptIn = featureOptIn["iKeyUsage"]; + + let onConfig = ikeyOptIn.onCfg; + let offConfig = ikeyOptIn.onCfg; + if (onConfig && offConfig) { + let defaultIkey = throttleMgrConfig["109"]; + Assert.equal(defaultIkey.disabled, true, "ikey msg should be disbaled"); + let ikeyMsg = throttleMgrConfig["106"]; + Assert.equal(ikeyMsg.disabled, true, "ikey msg should be disabled"); + let otherMsg = throttleMgrConfig["110"]; + Assert.equal(otherMsg.disabled, true, "other msg should be disabled"); + let cdnOptIn = featureOptIn["CdnUsage"]; + Assert.equal(cdnOptIn.mode, 2, "cdn feature optin should be disabled"); + Assert.equal(ikeyOptIn.mode, 3, "ikey feature optin should be enabled"); + return true; + } + return false; + }, "wait for core config update", 60, 100) as any) + }); + } } diff --git a/extensions/applicationinsights-cfgsync-js/src/CfgSyncPlugin.ts b/extensions/applicationinsights-cfgsync-js/src/CfgSyncPlugin.ts index d73a5cbe1..3461638f1 100644 --- a/extensions/applicationinsights-cfgsync-js/src/CfgSyncPlugin.ts +++ b/extensions/applicationinsights-cfgsync-js/src/CfgSyncPlugin.ts @@ -317,7 +317,9 @@ export class CfgSyncPlugin extends BaseTelemetryPlugin implements ICfgSyncPlugin if (JSON) { let cdnCfg = JSON.parse(response); //comments are not allowed let cfg = applyCdnfeatureCfg(cdnCfg, _self.core); - cfg && _setCfg(cfg, isAutoSync); + let newCfg = cfg && isPlainObject(cfg) && _replaceTartgetByKeys(cfg); + newCfg && _setCfg(newCfg, isAutoSync); + //cfg && _setCfg(cfg, isAutoSync); } } else { _retryCnt ++;