Skip to content

Commit

Permalink
change cfg sync mode (#2333)
Browse files Browse the repository at this point in the history
  • Loading branch information
Karlie-777 committed Apr 22, 2024
1 parent 50d893a commit f256575
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 7 deletions.
1 change: 1 addition & 0 deletions AISKU/Tests/Unit/src/applicationinsights.e2e.tests.ts
Expand Up @@ -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");
Expand Down
5 changes: 3 additions & 2 deletions AISKU/src/AISku.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -92,7 +92,8 @@ const defaultConfigValues: IConfigDefaults<IConfiguration & IConfig> = {
),
extensionConfig: cfgDfMerge<{[key: string]: any}>({
["AppInsightsCfgSyncPlugin"]: cfgDfMerge<ICfgSyncConfig>({
cfgUrl: CONFIG_ENDPOINT_URL
cfgUrl: CONFIG_ENDPOINT_URL,
syncMode: ICfgSyncMode.Receive
})
})
};
Expand Down
@@ -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";
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
});

}
}

Expand Down
Expand Up @@ -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 ++;
Expand Down

0 comments on commit f256575

Please sign in to comment.