Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,147 @@ export class AnalyticsPluginTests extends AITestClass {
}
});

this.testCase({
name: "AppInsightsTests: autoExceptionInstrumented can be set correctly without root config",
test: () => {
let appInsights = new AnalyticsPlugin();
let core = new AppInsightsCore();
let channel = new ChannelPlugin();

let config: IConfig & IConfiguration = {
instrumentationKey: "instrumentation_key",
samplingPercentage: 12,
extensionConfig: {}
};

this.onDone(() => {
core.unload(false);
});

// Initialize
core.initialize(config, [appInsights, channel]);

let extConfig = (core.config.extensionConfig || {})[AnalyticsPluginIdentifier] as IConfig;
Assert.equal(extConfig.autoExceptionInstrumented, undefined, "auto exception hook should be undefined for extenstion config");
let autoExceptionHooked = appInsights["_getDbgPlgTargets"]()[1];
Assert.equal(autoExceptionHooked, true, "autoExceptionInstrumented should be set true");
let errorHookCnt = appInsights["_getDbgPlgTargets"]()[0];
Assert.equal(errorHookCnt, 1, "auto exception hook should be instrumented");
Assert.equal(extConfig.autoUnhandledPromiseInstrumented, false, "autoUnhandledPromise should not be Instrumented");
}
});

this.testCase({
name: "AppInsightsTests: autoExceptionInstrumented can be set correctly without root config and with enableUnhandledPromiseRejectionTracking ",
test: () => {
let appInsights = new AnalyticsPlugin();
let core = new AppInsightsCore();
let channel = new ChannelPlugin();

let config: IConfig & IConfiguration = {
instrumentationKey: "instrumentation_key",
samplingPercentage: 12,
extensionConfig: {
[appInsights.identifier]: {
enableUnhandledPromiseRejectionTracking: true
}
}
};

this.onDone(() => {
core.unload(false);
});

// Initialize
core.initialize(config, [appInsights, channel]);

let extConfig = (core.config.extensionConfig || {})[AnalyticsPluginIdentifier] as IConfig;
Assert.equal(extConfig.autoExceptionInstrumented, undefined, "auto exception hook should be undefined for extenstion config");
let autoExceptionHooked = appInsights["_getDbgPlgTargets"]()[1];
Assert.equal(autoExceptionHooked, true, "autoExceptionInstrumented should be set true");
Assert.equal(extConfig.autoUnhandledPromiseInstrumented, true, "autoUnhandledPromiseInstrumented is set to true");
let errorHookCnt = appInsights["_getDbgPlgTargets"]()[0];
Assert.equal(errorHookCnt, 2, "auto exception hook should be instrumented twice");
}
});


this.testCase({
name: "AppInsightsTests: autoExceptionInstrumented can be set correctly with root config",
useFakeTimers: true,
test: () => {
let appInsights = new AnalyticsPlugin();
let core = new AppInsightsCore();
let channel = new ChannelPlugin();

let config: IConfig & IConfiguration = {
instrumentationKey: "instrumentation_key",
samplingPercentage: 12,
autoExceptionInstrumented: true,
extensionConfig: {}
};

this.onDone(() => {
core.unload(false);
});

// Initialize
core.initialize(config, [appInsights, channel]);

let extConfig = (core.config.extensionConfig || {})[AnalyticsPluginIdentifier] as IConfig;
Assert.equal(extConfig.autoExceptionInstrumented, undefined, "auto exception hook should be undefined for extenstion config");
let autoExceptionHooked = appInsights["_getDbgPlgTargets"]()[1];
Assert.equal(autoExceptionHooked, true, "autoExceptionInstrumented should be set true");
let errorHookCnt = appInsights["_getDbgPlgTargets"]()[0];
Assert.equal(errorHookCnt, 0, "auto exception hook should not be instrumented again");
Assert.equal(extConfig.autoUnhandledPromiseInstrumented, false, "autoUnhandledPromise should not be Instrumented");

(core.config as IConfiguration & IConfig).autoExceptionInstrumented = false;
extConfig.autoExceptionInstrumented = false;
this.clock.tick(1);
autoExceptionHooked = appInsights["_getDbgPlgTargets"]()[1];
Assert.equal(autoExceptionHooked, true, "autoExceptionInstrumented should be not be override");



}
});

this.testCase({
name: "AppInsightsTests: autoExceptionInstrumented can be set correctly with root config and enableUnhandledPromiseRejectionTracking",
test: () => {
let appInsights = new AnalyticsPlugin();
let core = new AppInsightsCore();
let channel = new ChannelPlugin();

let config: IConfig & IConfiguration = {
instrumentationKey: "instrumentation_key",
samplingPercentage: 12,
autoExceptionInstrumented: true,
extensionConfig: {
[appInsights.identifier]: {
enableUnhandledPromiseRejectionTracking: true
}
}
};

this.onDone(() => {
core.unload(false);
});

// Initialize
core.initialize(config, [appInsights, channel]);

let extConfig = (core.config.extensionConfig || {})[AnalyticsPluginIdentifier] as IConfig;
Assert.equal(extConfig.autoExceptionInstrumented, undefined, "auto exception hook should be undefined for extenstion config");
let autoExceptionHooked = appInsights["_getDbgPlgTargets"]()[1];
Assert.equal(autoExceptionHooked, true, "autoExceptionInstrumented should be set true");
Assert.equal(extConfig.autoUnhandledPromiseInstrumented, true, "autoUnhandledPromiseInstrumented is set to true");
let errorHookCnt = appInsights["_getDbgPlgTargets"]()[0];
Assert.equal(errorHookCnt, 1, "auto exception hook should be instrumented for autoUnhandledPromiseInstrumented");
}
});

this.testCase({
name: "AppInsightsTests: public members are correct",
test: () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ export class AnalyticsPlugin extends BaseTelemetryPlugin implements IAppInsights
let _prevUri: string; // Assigned in the constructor
let _currUri: string;
let _evtNamespace: string | string[];
// For testing error hooks only
let _errorHookCnt: number;

dynamicProto(AnalyticsPlugin, this, (_self, _base) => {
let _addHook = _base._addHook;
Expand Down Expand Up @@ -558,7 +560,7 @@ export class AnalyticsPlugin extends BaseTelemetryPlugin implements IAppInsights

_preInitTelemetryInitializers = null;
}

_populateDefaults(config);

_pageViewPerformanceManager = new PageViewPerformanceManager(_self.core);
Expand Down Expand Up @@ -619,14 +621,22 @@ export class AnalyticsPlugin extends BaseTelemetryPlugin implements IAppInsights
eventOff(window, null, null, _evtNamespace);
_initDefaults();
};


_self["_getDbgPlgTargets"] = () => {
return [_errorHookCnt, _autoExceptionInstrumented];
};

function _populateDefaults(config: IConfiguration) {
// it is used for 1DS as well, so config type should be IConfiguration only
let identifier = _self.identifier;
let core = _self.core;

_self._addHook(onConfigChange(config, () => {
let ctx = createProcessTelemetryContext(null, config, core);
_extConfig = ctx.getExtCfg(identifier, defaultValues);
// make sure auto exception is instrumented only once and it won't be overriden by the following config changes
_autoExceptionInstrumented = _autoExceptionInstrumented || (config as any).autoExceptionInstrumented || _extConfig.autoExceptionInstrumented;

_expCfg = _extConfig.expCfg;
_autoTrackPageVisitTime = _extConfig.autoTrackPageVisitTime;
Expand Down Expand Up @@ -699,7 +709,6 @@ export class AnalyticsPlugin extends BaseTelemetryPlugin implements IAppInsights

_self._addHook(onConfigChange(_extConfig, () => {
_disableExceptionTracking = _extConfig.disableExceptionTracking;

if (!_disableExceptionTracking && !_autoExceptionInstrumented && !_extConfig.autoExceptionInstrumented) {
// We want to enable exception auto collection and it has not been done so yet
_addHook(InstrumentEvent(_window, "onerror", {
Expand All @@ -717,6 +726,7 @@ export class AnalyticsPlugin extends BaseTelemetryPlugin implements IAppInsights
}
}
}, false));
_errorHookCnt ++;

_autoExceptionInstrumented = true;
}
Expand Down Expand Up @@ -860,7 +870,7 @@ export class AnalyticsPlugin extends BaseTelemetryPlugin implements IAppInsights
}
}
}, false));

_errorHookCnt ++;
_extConfig.autoUnhandledPromiseInstrumented = _autoUnhandledPromiseInstrumented = true;
}
}));
Expand Down Expand Up @@ -904,6 +914,7 @@ export class AnalyticsPlugin extends BaseTelemetryPlugin implements IAppInsights
_currUri = null;
_evtNamespace = null;
_extConfig = null;
_errorHookCnt = 0;

// Define _self.config
objDefine(_self, "config", {
Expand Down
Loading