Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Main][Task]27749889: Change AISKU Sync Mode to Receive #2333

Merged
merged 2 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AISKU/Tests/Unit/src/applicationinsights.e2e.tests.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change the AISKU default receive mode to "receive" (will call "updateCfg" to update current core.config), previous one is "broadcast" (which means only send events out but not receiving any incoming changes)

})
})
};
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

previous design: any configs from cdn, they will be directly applied to current core.config (it was designed to use blkCdnCfg to control if we need to apply cdn changes or not).
Current:
So add an extra layer, even if blkCdnCfg is set to false, any config values defined in nonOverrideConfigs will not be replaced by cdn configs.

//cfg && _setCfg(cfg, isAutoSync);
}
} else {
_retryCnt ++;
Expand Down
Loading