Skip to content

Commit

Permalink
[BUG] enableAjaxErrorStatusText: false (which is the default setting)… (
Browse files Browse the repository at this point in the history
#1644)

* [BUG] enableAjaxErrorStatusText: false (which is the default setting) does not turn off logging error response body #1640

* Update test descriptions
  • Loading branch information
MSNev authored Aug 30, 2021
1 parent 6596e4e commit 9234ce9
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,90 @@ export class AjaxTests extends AITestClass {
}
});

this.testCase({
name: "Ajax: xhr respond error data is not tracked as part C data when enableAjaxErrorStatusText flag is false",
test: () => {
this._ajax = new AjaxMonitor();
let appInsightsCore = new AppInsightsCore();
let coreConfig: IConfiguration & IConfig = { instrumentationKey: "abc", disableAjaxTracking: false, enableAjaxErrorStatusText: false };
appInsightsCore.initialize(coreConfig, [this._ajax, new TestChannelPlugin()]);

var trackStub = this.sandbox.stub(appInsightsCore, "track");

// act
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://microsoft.com");
xhr.send();

// Emulate response
(<any>xhr).respond(403, {}, "error data with status code 403");

// assert
Assert.ok(trackStub.calledOnce, "track is called");
let data = trackStub.args[0][0].baseData;
Assert.equal("Ajax", data.type, "request is Ajax type");
Assert.equal(undefined, data.properties.responseText, "xhr request's reponse error is not stored in part C");

// act
var xhr2 = new XMLHttpRequest();
xhr2.open("GET", "http://microsoft.com");
xhr2.responseType = "json";
xhr2.send();

// Emulate response
let responseJSON = '{ "error":"error data with status code 403" }';
(<any>xhr2).respond(403, {}, responseJSON);

// assert
Assert.ok(trackStub.calledTwice, "track is called");
data = trackStub.args[1][0].baseData;
Assert.equal("Ajax", data.type, "request is Ajax type");
Assert.equal(undefined, data.properties.responseText, "xhr request's reponse error is not stored in part C");
}
});

this.testCase({
name: "Ajax: xhr respond error data is not tracked as part C data when enableAjaxErrorStatusText flag is default",
test: () => {
this._ajax = new AjaxMonitor();
let appInsightsCore = new AppInsightsCore();
let coreConfig: IConfiguration & IConfig = { instrumentationKey: "abc", disableAjaxTracking: false };
appInsightsCore.initialize(coreConfig, [this._ajax, new TestChannelPlugin()]);

var trackStub = this.sandbox.stub(appInsightsCore, "track");

// act
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://microsoft.com");
xhr.send();

// Emulate response
(<any>xhr).respond(403, {}, "error data with status code 403");

// assert
Assert.ok(trackStub.calledOnce, "track is called");
let data = trackStub.args[0][0].baseData;
Assert.equal("Ajax", data.type, "request is Ajax type");
Assert.equal(undefined, data.properties.responseText, "xhr request's reponse error is not stored in part C");

// act
var xhr2 = new XMLHttpRequest();
xhr2.open("GET", "http://microsoft.com");
xhr2.responseType = "json";
xhr2.send();

// Emulate response
let responseJSON = '{ "error":"error data with status code 403" }';
(<any>xhr2).respond(403, {}, responseJSON);

// assert
Assert.ok(trackStub.calledTwice, "track is called");
data = trackStub.args[1][0].baseData;
Assert.equal("Ajax", data.type, "request is Ajax type");
Assert.equal(undefined, data.properties.responseText, "xhr request's reponse error is not stored in part C");
}
});

this.testCaseAsync({
name: "Fetch: fetch with disabled flag isn't tracked",
stepDelay: 10,
Expand Down
8 changes: 6 additions & 2 deletions extensions/applicationinsights-dependencies-js/src/ajax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ export class AjaxMonitor extends BaseTelemetryPlugin implements IDependenciesPlu
let _currentWindowHost:string = location && location.host && location.host.toLowerCase();
let _config: ICorrelationConfig = AjaxMonitor.getEmptyConfig();
let _enableRequestHeaderTracking = false;
let _enableAjaxErrorStatusText = false;
let _trackAjaxAttempts: number = 0;
let _context: ITelemetryContext;
let _isUsingW3CHeaders: boolean;
Expand All @@ -231,6 +232,7 @@ export class AjaxMonitor extends BaseTelemetryPlugin implements IDependenciesPlu

let distributedTracingMode = _config.distributedTracingMode;
_enableRequestHeaderTracking = _config.enableRequestHeaderTracking;
_enableAjaxErrorStatusText = _config.enableAjaxErrorStatusText;
_enableAjaxPerfTracking = _config.enableAjaxPerfTracking;
_maxAjaxCallsPerView = _config.maxAjaxCallsPerView;
_enableResponseHeaderTracking = _config.enableResponseHeaderTracking;
Expand Down Expand Up @@ -635,6 +637,7 @@ export class AjaxMonitor extends BaseTelemetryPlugin implements IDependenciesPlu
ajaxData.xhrMonitoringState.openDone = true;
ajaxData.requestHeaders = {};
ajaxData.async = async;
ajaxData.errorStatusText = _enableAjaxErrorStatusText;
xhr[strAjaxData] = ajaxData;

_attachToOnReadyStateChange(xhr);
Expand Down Expand Up @@ -671,7 +674,7 @@ export class AjaxMonitor extends BaseTelemetryPlugin implements IDependenciesPlu
return xhr.responseText;
}
} catch (e) {
// This shouldn't happend because of the above check -- but just in case, so just ignore
// This shouldn't happen because of the above check -- but just in case, so just ignore
}

return null;
Expand Down Expand Up @@ -794,7 +797,7 @@ export class AjaxMonitor extends BaseTelemetryPlugin implements IDependenciesPlu
performance.mark(markId);
let entries = performance.getEntriesByName(markId);
if (entries && entries.length === 1) {
ajaxData.perfMark = entries[0];
ajaxData.perfMark = entries[0] as any;
}
}
}
Expand Down Expand Up @@ -869,6 +872,7 @@ export class AjaxMonitor extends BaseTelemetryPlugin implements IDependenciesPlu

const ajaxData = new ajaxRecord(traceID, spanID, _self[strDiagLog]());
ajaxData.requestSentTime = dateTimeUtilsNow();
ajaxData.errorStatusText = _enableAjaxErrorStatusText;

if (input instanceof Request) {
ajaxData.requestUrl = input ? input.url : "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ export class ajaxRecord {
public perfAttempts?:number;
public async?:boolean;

/// <summary>Should the Error Status text be included in the response</summary>
public errorStatusText?:boolean;

/// <summary>Returns the HTTP status code.</summary>
public status:string|number;

Expand Down Expand Up @@ -328,7 +331,7 @@ export class ajaxRecord {
}
}

if (self.status >= 400) {
if (self.errorStatusText && self.status >= 400) {
const responseType = response.type;
dependency[strProperties] = dependency[strProperties] || {};
if (responseType === "" || responseType === "text") {
Expand Down

0 comments on commit 9234ce9

Please sign in to comment.