Skip to content

Commit

Permalink
[Main][Task]25716927: Change default CfgSync values to turn on the ik…
Browse files Browse the repository at this point in the history
…ey deprecation message (#2325)

* add cfg cdn defaults

* update
  • Loading branch information
Karlie-777 committed Apr 11, 2024
1 parent 2a1c332 commit db6f36c
Show file tree
Hide file tree
Showing 11 changed files with 388 additions and 178 deletions.
3 changes: 2 additions & 1 deletion AISKU/Tests/Unit/src/CdnThrottle.tests.ts
Expand Up @@ -10,6 +10,7 @@ import { CfgSyncPlugin, ICfgSyncConfig, ICfgSyncMode } from '@microsoft/applicat
import { createSyncPromise } from '@nevware21/ts-async';
import { ICfgSyncCdnConfig } from '@microsoft/applicationinsights-cfgsync-js/src/Interfaces/ICfgSyncCdnConfig';


const TestInstrumentationKey = 'b7170927-2d1c-44f1-acec-59f4e1751c11';

const default_throttle_config = {
Expand Down Expand Up @@ -181,6 +182,7 @@ export class CdnThrottle extends AITestClass {
this.init = new ApplicationInsights({
config: {
instrumentationKey: TestInstrumentationKey,
featureOptIn : {["iKeyUsage"]: {mode: FeatureOptInMode.disable}},
extensionConfig : {["AppInsightsCfgSyncPlugin"] : {
syncMode: ICfgSyncMode.Receive,
cfgUrl: "testurl"
Expand All @@ -190,7 +192,6 @@ export class CdnThrottle extends AITestClass {
this.init.loadAppInsights();
this._ai = this.init;
}].concat(PollingAssert.createPollingAssert(() => {

if (this.fetchStub.called){
let core = this._ai['core'];
let _logger = core.logger;
Expand Down
28 changes: 24 additions & 4 deletions AISKU/Tests/Unit/src/SnippetInitialization.Tests.ts
Expand Up @@ -66,7 +66,12 @@ function getSnippetConfig(sessionPrefix: string, addSampling: boolean = false) {
namePrefix: `sessionPrefix`,
enableCorsCorrelation: true,
distributedTracingMode: DistributedTracingModes.AI_AND_W3C,
samplingPercentage: addSampling ? 50 : undefined
samplingPercentage: addSampling ? 50 : undefined,
extensionConfig: {
["AppInsightsCfgSyncPlugin"]: {
cfgUrl: ""
}
}
} as IConfig
};
};
Expand All @@ -84,7 +89,12 @@ function getSnippetConfigConnectionString(sessionPrefix: string) {
disableExceptionTracking: false,
namePrefix: `sessionPrefix`,
enableCorsCorrelation: true,
distributedTracingMode: DistributedTracingModes.AI_AND_W3C
distributedTracingMode: DistributedTracingModes.AI_AND_W3C,
extensionConfig: {
["AppInsightsCfgSyncPlugin"]: {
cfgUrl: ""
}
}
} as IConfig
};
};
Expand All @@ -102,7 +112,12 @@ function getSnippetConfigWrongConnectionString(sessionPrefix: string) {
disableExceptionTracking: false,
namePrefix: `sessionPrefix`,
enableCorsCorrelation: true,
distributedTracingMode: DistributedTracingModes.AI_AND_W3C
distributedTracingMode: DistributedTracingModes.AI_AND_W3C,
extensionConfig: {
["AppInsightsCfgSyncPlugin"]: {
cfgUrl: ""
}
}
} as IConfig
};
};
Expand All @@ -120,7 +135,12 @@ function getSnippetConfigNotSetConnectionString(sessionPrefix: string) {
disableExceptionTracking: false,
namePrefix: `sessionPrefix`,
enableCorsCorrelation: true,
distributedTracingMode: DistributedTracingModes.AI_AND_W3C
distributedTracingMode: DistributedTracingModes.AI_AND_W3C,
extensionConfig: {
["AppInsightsCfgSyncPlugin"]: {
cfgUrl: ""
}
}
} as IConfig
};
};
Expand Down
22 changes: 16 additions & 6 deletions AISKU/Tests/Unit/src/ThrottleSentMessage.tests.ts
Expand Up @@ -33,7 +33,7 @@ export class ThrottleSentMessage extends AITestClass {
}

public _getTestConfig() {
let config: IConfiguration | IConfig = {
let config: IConfiguration & IConfig = {
instrumentationKey: TestInstrumentationKey,
disableAjaxTracking: false,
disableFetchTracking: false,
Expand All @@ -44,7 +44,12 @@ export class ThrottleSentMessage extends AITestClass {
enableCorsCorrelation: true,
samplingPercentage: 50,
convertUndefined: "test-value",
disablePageUnloadEvents: [ "beforeunload" ]
disablePageUnloadEvents: [ "beforeunload" ],
extensionConfig: {
["AppInsightsCfgSyncPlugin"]: {
cfgUrl: ""
}
}
};
return config;
}
Expand Down Expand Up @@ -186,14 +191,19 @@ export class ThrottleSentMessage extends AITestClass {
useFakeTimers: true,
test: () => {
let loggingSpy = this.sandbox.stub(this._logger, 'throwInternal');

Assert.equal(true, this._ai.appInsights.core.isInitialized(),
'Core is initialized');
let config = this.getAi.config;
let config = this._getTestConfig();
config.throttleMgrCfg= {[_eInternalMessageId.InstrumentationKeyDeprecation]:tconfig, [_eInternalMessageId.DefaultThrottleMsgKey]:tconfig};
config.featureOptIn = {["iKeyUsage"]: {mode: FeatureOptInMode.disable}}
let init = new ApplicationInsights({
config: config
});

let ai = init.loadAppInsights();
Assert.equal(true, ai.appInsights.core.isInitialized(),'Core is initialized');
this.clock.tick(12); // wait enough time for negative test
Assert.equal(loggingSpy.callCount, 0);

ai.unload(false);
}
});
}
Expand Down
7 changes: 6 additions & 1 deletion AISKU/Tests/Unit/src/applicationinsights.e2e.fetch.tests.ts
Expand Up @@ -26,7 +26,12 @@ export class ApplicationInsightsFetchTests extends ApplicationInsightsTests {
distributedTracingMode: DistributedTracingModes.AI_AND_W3C,
samplingPercentage: 50,
convertUndefined: "test-value",
disablePageUnloadEvents: [ "beforeunload" ]
disablePageUnloadEvents: [ "beforeunload" ],
extensionConfig: {
["AppInsightsCfgSyncPlugin"]: {
cfgUrl: ""
}
}
};

return config;
Expand Down
50 changes: 48 additions & 2 deletions AISKU/Tests/Unit/src/applicationinsights.e2e.tests.ts
Expand Up @@ -3,8 +3,9 @@ import { SinonSpy } from 'sinon';
import { ApplicationInsights } from '../../../src/applicationinsights-web'
import { Sender } from '@microsoft/applicationinsights-channel-js';
import { IDependencyTelemetry, ContextTagKeys, Event, Trace, Exception, Metric, PageView, PageViewPerformance, RemoteDependencyData, DistributedTracingModes, RequestHeaders, IAutoExceptionTelemetry, BreezeChannelIdentifier, IConfig } from '@microsoft/applicationinsights-common';
import { ITelemetryItem, getGlobal, newId, dumpObj, BaseTelemetryPlugin, IProcessTelemetryContext, __getRegisteredEvents, arrForEach, IConfiguration } from "@microsoft/applicationinsights-core-js";
import { ITelemetryItem, getGlobal, newId, dumpObj, BaseTelemetryPlugin, IProcessTelemetryContext, __getRegisteredEvents, arrForEach, IConfiguration, FeatureOptInMode } from "@microsoft/applicationinsights-core-js";
import { TelemetryContext } from '@microsoft/applicationinsights-properties-js';
import { CONFIG_ENDPOINT_URL } from '../../../src/InternalConstants';


export class ApplicationInsightsTests extends AITestClass {
Expand Down Expand Up @@ -62,7 +63,12 @@ export class ApplicationInsightsTests extends AITestClass {
distributedTracingMode: DistributedTracingModes.AI_AND_W3C,
samplingPercentage: 50,
convertUndefined: "test-value",
disablePageUnloadEvents: [ "beforeunload" ]
disablePageUnloadEvents: [ "beforeunload" ],
extensionConfig: {
["AppInsightsCfgSyncPlugin"]: {
cfgUrl: ""
}
}
};

return config;
Expand Down Expand Up @@ -240,6 +246,46 @@ export class ApplicationInsightsTests extends AITestClass {
handler.rm();
}
});

this.testCase({
name: "CfgSync DynamicConfigTests: Prod CDN is Fetched and feature is turned on/off as expected",
useFakeTimers: true,
test: () => {
let fetchcalled = 0;
let overrideFetchFn = (url: string, oncomplete: any, isAutoSync?: boolean) => {
fetchcalled ++;
Assert.equal(url, CONFIG_ENDPOINT_URL, "fetch should be called with prod cdn");
};
let config = {
instrumentationKey: "testIKey",
extensionConfig:{
["AppInsightsCfgSyncPlugin"]: {
overrideFetchFn: overrideFetchFn
}

}
} as IConfiguration & IConfig;
let ai = new ApplicationInsights({config: config});
ai.loadAppInsights();

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");

let featureOptIn = config.featureOptIn || {};
Assert.equal(featureOptIn["iKeyUsage"].mode, FeatureOptInMode.enable, "ikey message should be turned on");

Assert.equal(fetchcalled, 1, "fetch should be called once");
config.extensionConfig = config.extensionConfig || {};
let expectedTimeout = 2000000000;
config.extensionConfig["AppInsightsCfgSyncPlugin"].scheduleFetchTimeout = expectedTimeout;
this.clock.tick(1);

extConfig = ai.config.extensionConfig["AppInsightsCfgSyncPlugin"];
Assert.equal(extConfig.scheduleFetchTimeout, expectedTimeout, "timeout should be changes dynamically");
ai.unload(false);
}
});
}

public addCDNOverrideTests(): void {
Expand Down
4 changes: 4 additions & 0 deletions AISKU/Tests/Unit/src/sender.e2e.tests.ts
Expand Up @@ -36,7 +36,11 @@ export class SenderE2ETests extends AITestClass {
'AppInsightsChannelPlugin': {
maxBatchInterval: 2000,
maxBatchSizeInBytes: 10*1024*1024 // 10 MB
},
["AppInsightsCfgSyncPlugin"]: {
cfgUrl: ""
}

}
},
queue: [],
Expand Down
4 changes: 4 additions & 0 deletions AISKU/Tests/Unit/src/validate.e2e.tests.ts
Expand Up @@ -30,7 +30,11 @@ export class ValidateE2ETests extends AITestClass {
extensionConfig: {
'AppInsightsChannelPlugin': {
maxBatchInterval: 500
},
["AppInsightsCfgSyncPlugin"]: {
cfgUrl: ""
}

}
},
queue: [],
Expand Down
17 changes: 11 additions & 6 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 } from "@microsoft/applicationinsights-cfgsync-js";
import { CfgSyncPlugin, ICfgSyncConfig } from "@microsoft/applicationinsights-cfgsync-js";
import { Sender } from "@microsoft/applicationinsights-channel-js";
import {
AnalyticsPluginIdentifier, DEFAULT_BREEZE_PATH, IAutoExceptionTelemetry, IConfig, IDependencyTelemetry, IEventTelemetry,
Expand All @@ -30,8 +30,8 @@ import { IPromise, createPromise } from "@nevware21/ts-async";
import { arrForEach, arrIndexOf, objDefine, objForEachKey, strIndexOf, throwUnsupported } from "@nevware21/ts-utils";
import { IApplicationInsights } from "./IApplicationInsights";
import {
STR_ADD_TELEMETRY_INITIALIZER, STR_CLEAR_AUTHENTICATED_USER_CONTEXT, STR_EVT_NAMESPACE, STR_GET_COOKIE_MGR, STR_GET_PLUGIN,
STR_POLL_INTERNAL_LOGS, STR_SET_AUTHENTICATED_USER_CONTEXT, STR_SNIPPET, STR_START_TRACK_EVENT, STR_START_TRACK_PAGE,
CONFIG_ENDPOINT_URL, STR_ADD_TELEMETRY_INITIALIZER, STR_CLEAR_AUTHENTICATED_USER_CONTEXT, STR_EVT_NAMESPACE, STR_GET_COOKIE_MGR,
STR_GET_PLUGIN, STR_POLL_INTERNAL_LOGS, STR_SET_AUTHENTICATED_USER_CONTEXT, STR_SNIPPET, STR_START_TRACK_EVENT, STR_START_TRACK_PAGE,
STR_STOP_TRACK_EVENT, STR_STOP_TRACK_PAGE, STR_TRACK_DEPENDENCY_DATA, STR_TRACK_EVENT, STR_TRACK_EXCEPTION, STR_TRACK_METRIC,
STR_TRACK_PAGE_VIEW, STR_TRACK_TRACE
} from "./InternalConstants";
Expand Down Expand Up @@ -71,14 +71,14 @@ const default_throttle_config = {

// We need to include all properties that we only reference that we want to be dynamically updatable here
// So they are converted even when not specified in the passed configuration
const defaultConfigValues: IConfigDefaults<IConfiguration|IConfig> = {
const defaultConfigValues: IConfigDefaults<IConfiguration & IConfig> = {
connectionString: UNDEFINED_VALUE,
endpointUrl: UNDEFINED_VALUE,
instrumentationKey: UNDEFINED_VALUE,
userOverrideEndpointUrl: UNDEFINED_VALUE,
diagnosticLogInterval: cfgDfValidate(_chkDiagLevel, 10000),
featureOptIn:{
[IKEY_USAGE]: {mode: FeatureOptInMode.disable},
[IKEY_USAGE]: {mode: FeatureOptInMode.enable}, //for versions after 3.1.2 (>= 3.2.0)
[CDN_USAGE]: {mode: FeatureOptInMode.disable},
[SDK_LOADER_VER]: {mode: FeatureOptInMode.disable}
},
Expand All @@ -89,7 +89,12 @@ const defaultConfigValues: IConfigDefaults<IConfiguration|IConfig> = {
[_eInternalMessageId.SdkLdrUpdate]:cfgDfMerge<IThrottleMgrConfig>(default_throttle_config),
[_eInternalMessageId.CdnDeprecation]:cfgDfMerge<IThrottleMgrConfig>(default_throttle_config)
}
)
),
extensionConfig: cfgDfMerge<{[key: string]: any}>({
["AppInsightsCfgSyncPlugin"]: cfgDfMerge<ICfgSyncConfig>({
cfgUrl: CONFIG_ENDPOINT_URL
})
})
};

function _chkDiagLevel(value: number) {
Expand Down
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -463,6 +463,8 @@ For versions before 3.1.2, these components are disabled by default.
For versions after 3.1.2, these components are **turned on by default**.
See [feature opt-in status](./docs/WebConfig.md) for more details.

For users behind a firewall, see [how to disable fetching from default CfgSync CDN](./docs/WebConfig.md#basic-usage).

## Single Page Applications

By default, this SDK will **not** handle state based route changing that occurs in single page applications. To enable automatic route change tracking for your single page application, you can add `enableAutoRouteTracking: true` to your setup configuration.
Expand Down

0 comments on commit db6f36c

Please sign in to comment.