Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Karlie-777 committed Jun 25, 2024
1 parent 1d84dd0 commit 60c6589
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 99 deletions.
47 changes: 31 additions & 16 deletions AISKU/src/AISku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { AnalyticsPlugin, ApplicationInsights } from "@microsoft/applicationinsi
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,
IExceptionTelemetry, IMetricTelemetry, IPageViewPerformanceTelemetry, IPageViewTelemetry, IRequestHeaders,
AnalyticsPluginIdentifier, ConnectionString, DEFAULT_BREEZE_PATH, IAutoExceptionTelemetry, IConfig, IDependencyTelemetry,
IEventTelemetry, IExceptionTelemetry, IMetricTelemetry, IPageViewPerformanceTelemetry, IPageViewTelemetry, IRequestHeaders,
ITelemetryContext as Common_ITelemetryContext, IThrottleInterval, IThrottleLimit, IThrottleMgrConfig, ITraceTelemetry,
PropertiesPluginIdentifier, ThrottleMgr, parseConnectionString
} from "@microsoft/applicationinsights-common";
Expand Down Expand Up @@ -201,33 +201,48 @@ export class AppInsightsSku implements IApplicationInsights {
// Will get recalled if any referenced values are changed
_addUnloadHook(onConfigChange(cfgHandler, () => {
let configCs = _config.connectionString;
if (isPromiseLike(configCs)) {
let ikeyPromise = createAsyncPromise<string>((resolve, reject) => {

function _parseCs() {
return createAsyncPromise<ConnectionString>((resolve, reject) => {
doAwaitResponse(configCs, (res) => {
let curCs = res.value;
let ikey = _config.instrumentationKey;
let curCs = res && res.value;
let parsedCs = null;
if (!res.rejected && curCs) {
// replace cs with resolved values in case of circular promises
_config.connectionString = curCs;
let resolvedCs = parseConnectionString(curCs);
ikey = resolvedCs.instrumentationkey || ikey;
parsedCs = parseConnectionString(curCs);
}
// if can't resolve cs promise, null will be returned
resolve(parsedCs);
});
});

}

if (isPromiseLike(configCs)) {
let ikeyPromise = createAsyncPromise<string>((resolve, reject) => {
_parseCs().then((cs) => {
let ikey = _config.instrumentationKey;
ikey = cs && cs.instrumentationkey || ikey;
resolve(ikey);
}).catch((e) => {
// parseCs will always resolve(unless timeout)
// return null in case any error happens
resolve(null);
});

});

let urlPromise = createAsyncPromise<string>((resolve, reject) => {
doAwaitResponse(configCs, (res) => {
let curCs = res.value;
_parseCs().then((cs) => {
let url = _config.endpointUrl;
if (!res.rejected && curCs) {
let resolvedCs = parseConnectionString(curCs);
let ingest = resolvedCs.ingestionendpoint;
url = ingest? ingest + DEFAULT_BREEZE_PATH : url;
}
let ingest = cs && cs.ingestionendpoint;
url = ingest? ingest + DEFAULT_BREEZE_PATH : url;
resolve(url);
}).catch((e) => {
// parseCs will always resolve(unless timeout)
// return null in case any error happens
resolve(null);
});

});
Expand Down
1 change: 1 addition & 0 deletions extensions/applicationinsights-dependencies-js/src/ajax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ export class AjaxMonitor extends BaseTelemetryPlugin implements IDependenciesPlu
if (_enableAjaxPerfTracking) {
let iKey = config.instrumentationKey || "unkwn";
// only change the ikey if it is string
// TODO: handle ikey promise
if (isString(iKey)) {
if (iKey.length > 5) {
_markPrefix = AJAX_MONITOR_PREFIX + strSubstring(iKey, iKey.length - 5) + ".";
Expand Down
1 change: 1 addition & 0 deletions shared/AppInsightsCommon/src/applicationinsights-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export {
} from "./Util";
export { ThrottleMgr } from "./ThrottleMgr";
export { parseConnectionString, ConnectionStringParser } from "./ConnectionStringParser";
export { ConnectionString } from "./Interfaces/ConnectionString";
export { FieldType } from "./Enums";
export { IRequestHeaders, RequestHeaders, eRequestHeaders } from "./RequestResponseHeaders";
export { DisabledPropertyName, ProcessLegacy, SampleRate, HttpMethod, DEFAULT_BREEZE_ENDPOINT, DEFAULT_BREEZE_PATH, strNotSpecified } from "./Constants";
Expand Down
164 changes: 83 additions & 81 deletions shared/AppInsightsCore/Tests/Unit/src/ApplicationInsightsCore.Tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1571,92 +1571,94 @@ export class ApplicationInsightsCoreTests extends AITestClass {
}, "Wait for promise response" + new Date().toISOString(), 60, 1000) as any)
});

// this.testCaseAsync({
// name: "ApplicationInsightsCore Init: init with ikey and endpoint timeout promises",
// stepDelay: 100,
// useFakeTimers: true,
// steps: [() => {
// let trackPlugin = new TrackPlugin();
// let channelPlugin = new ChannelPlugin();
// channelPlugin.priority = 1001;
// let core = new AppInsightsCore();
// let channelSpy = this.sandbox.stub(channelPlugin, "processTelemetry");
// this.ctx.core = core;
// this.ctx.channelSpy = channelSpy;

// let ikeyPromise = createTimeoutPromise(60, true,"testIkey");
// let urlPromise = createTimeoutPromise(60, true, "testUrl");

// let config = {
// instrumentationKey: ikeyPromise,
// endpointUrl: urlPromise,
// initTimeOut: 1
// } as IConfiguration;
// core.initialize(
// config,
// [trackPlugin, channelPlugin]);
this.testCaseAsync({
name: "ApplicationInsightsCore Init: init with ikey and endpoint timeout promises",
stepDelay: 100,
useFakeTimers: true,
steps: [() => {
let trackPlugin = new TrackPlugin();
let channelPlugin = new ChannelPlugin();
channelPlugin.priority = 1001;
let core = new AppInsightsCore();
let channelSpy = this.sandbox.stub(channelPlugin, "processTelemetry");
this.ctx.core = core;
this.ctx.channelSpy = channelSpy;

let ikeyPromise = createTimeoutPromise(60, true,"testIkey");
let urlPromise = createTimeoutPromise(60, true, "testUrl");

let config = {
instrumentationKey: ikeyPromise,
endpointUrl: urlPromise,
initTimeOut: 1
} as IConfiguration;
core.initialize(
config,
[trackPlugin, channelPlugin]);


// Assert.ok(!channelSpy.calledOnce, "channel should not be called once");
// Assert.ok(core.eventCnt() == 1, "Event should be queued");
// let activeStatus = core.activeStatus();
// Assert.equal(activeStatus, ActiveStatus.PENDING, "active status should be set to pending");
Assert.ok(!channelSpy.calledOnce, "channel should not be called once");
Assert.ok(core.eventCnt() == 1, "Event should be queued");
let activeStatus = core.activeStatus();
Assert.equal(activeStatus, ActiveStatus.PENDING, "active status should be set to pending");

// }].concat(PollingAssert.createPollingAssert(() => {
// let core = this.ctx.core;
// let activeStatus = core.activeStatus();
// let channelSpy = this.ctx.channelSpy;
}].concat(PollingAssert.createPollingAssert(() => {
let core = this.ctx.core;
let activeStatus = core.activeStatus();
let channelSpy = this.ctx.channelSpy;

// if (activeStatus === ActiveStatus.INACTIVE) {
// Assert.ok(!channelSpy.calledOnce, "channel should not be called once");
// Assert.ok(core.eventCnt() == 0, "Event should be released");
// return true;
// }
// return false;
// }, "Wait for promise response" + new Date().toISOString(), 60, 1000) as any)
// });

// this.testCaseAsync({
// name: "ApplicationInsightsCore Init: init with ikey promises and endpoint timeout promises",
// stepDelay: 100,
// useFakeTimers: true,
// steps: [() => {
// let channelPlugin = new ChannelPlugin();
// channelPlugin.priority = 1001;
// let core = new AppInsightsCore();
// let channelSpy = this.sandbox.stub(channelPlugin, "processTelemetry");

// let ikeyPromise = createAsyncResolvedPromise("testIkey1");
// let urlPromise = createTimeoutPromise(20, false, "testUrl1");

// let config = {
// instrumentationKey: ikeyPromise,
// endpointUrl: urlPromise,
// initTimeOut: 2
// } as IConfiguration;
// core.initialize(
// config,
// [channelPlugin]);
// this.ctx.core = core;
// this.ctx.channelSpy = channelSpy;

// this.clock.tick(1);
// let activeStatus = core.activeStatus();
// Assert.equal(activeStatus, ActiveStatus.PENDING, "active status should be set to pending");

// }].concat(PollingAssert.createPollingAssert(() => {
// let core = this.ctx.core;
// let activeStatus = core.activeStatus();
// let channelSpy = this.ctx.channelSpy
if (activeStatus === ActiveStatus.INACTIVE) {
Assert.ok(!channelSpy.calledOnce, "channel should not be called once");
Assert.ok(core.eventCnt() == 0, "Event should be released");
return true;
}
return false;
}, "Wait for promise response" + new Date().toISOString(), 60, 1000) as any)
});

this.testCaseAsync({
name: "ApplicationInsightsCore Init: init with ikey timeout promises and endpoint promises",
stepDelay: 100,
useFakeTimers: true,
steps: [() => {
let channelPlugin = new ChannelPlugin();
channelPlugin.priority = 1001;
let core = new AppInsightsCore();
let channelSpy = this.sandbox.stub(channelPlugin, "processTelemetry");

let ikeyPromise = createTimeoutPromise(20, true, "testIkey1");
let urlPromise = createTimeoutPromise(1, true, "testUrl1");

let config = {
instrumentationKey: ikeyPromise,
endpointUrl: urlPromise,
initTimeOut: 6
} as IConfiguration;
core.initialize(
config,
[channelPlugin]);
this.ctx.core = core;
this.ctx.channelSpy = channelSpy;

let activeStatus = core.activeStatus();
Assert.equal(activeStatus, ActiveStatus.PENDING, "active status should be set to pending");
Assert.ok(!channelSpy.calledOnce, "channel should not be called");
core.track({name: "testEvent"});


}].concat(PollingAssert.createPollingAssert(() => {
let core = this.ctx.core;
let activeStatus = core.activeStatus();
let channelSpy = this.ctx.channelSpy

// if (activeStatus === ActiveStatus.INACTIVE) {
// //Assert.equal(activeStatus, ActiveStatus.INACTIVE,"should be set to inactive status");
// //Assert.equal(!channelSpy.called, "channel should not be called");
// return true;
// }
// return false;
// }, "Wait for promise response" + new Date().toISOString(), 60, 1000) as any)
// });
if (activeStatus === ActiveStatus.INACTIVE) {
Assert.ok(core.eventCnt() == 0, "Event should be released");
Assert.ok(!channelSpy.called, "channel should not be called");
return true;
}
return false;
}, "Wait for promise response" + new Date().toISOString(), 60, 1000) as any)
});



Expand Down
4 changes: 2 additions & 2 deletions shared/AppInsightsCore/src/JavaScriptSDK/AppInsightsCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import { createCookieMgr } from "./CookieMgr";
import { createUniqueNamespace } from "./DataCacheHelper";
import { getDebugListener } from "./DbgExtensionUtils";
import { DiagnosticLogger, _InternalLogMessage, _throwInternal, _warnToConsole } from "./DiagnosticLogger";
import { getSetValue, proxyFunctionAs, proxyFunctions, toISOString } from "./HelperFuncs";
import { getSetValue, isNotNullOrUndefined, proxyFunctionAs, proxyFunctions, toISOString } from "./HelperFuncs";
import {
STR_CHANNELS, STR_CREATE_PERF_MGR, STR_DISABLED, STR_EMPTY, STR_EXTENSIONS, STR_EXTENSION_CONFIG, UNDEFINED_VALUE
} from "./InternalConstants";
Expand Down Expand Up @@ -400,7 +400,7 @@ export class AppInsightsCore<CfgType extends IConfiguration = IConfiguration> im
// reset to false for new dynamic changes
_isStatusSet = false;
_activeStatus = eActiveStatus.PENDING;
let initTimeout = isNullOrUndefined(rootCfg.initTimeOut)? rootCfg.initTimeOut : maxInitTimeout; // rootCfg.initTimeOut could be 0
let initTimeout = isNotNullOrUndefined(rootCfg.initTimeOut)? rootCfg.initTimeOut : maxInitTimeout; // rootCfg.initTimeOut could be 0
let allPromises = createAllSettledPromise<string>(promises);
_initTimer = scheduleTimeout(() => {
// set _isStatusSet to true
Expand Down

0 comments on commit 60c6589

Please sign in to comment.