This commit is contained in:
Родитель
fd15c290e8
Коммит
a02674d639
|
@ -2624,6 +2624,8 @@ export class AjaxTests extends AITestClass {
|
|||
this.testCase({
|
||||
name: "Ajax: should create and pass a single traceparent header if w3c is enabled with multiple instances",
|
||||
test: () => {
|
||||
// Switch back to the "real" XMLHttpRequest so that the instrumentation hooks work
|
||||
this.useFakeServer = false;
|
||||
let _ajax2 = new AjaxMonitor();
|
||||
let appInsightsFirst = new AppInsightsCore();
|
||||
let coreConfig2 = {
|
||||
|
@ -2680,21 +2682,21 @@ export class AjaxTests extends AITestClass {
|
|||
|
||||
// Act
|
||||
var xhr = new XMLHttpRequest();
|
||||
var stub = this.sandbox.stub(xhr, "setRequestHeader");
|
||||
var spy = this.sandbox.spy(xhr, "setRequestHeader");
|
||||
xhr.open("GET", "http://www.example.com");
|
||||
xhr.send();
|
||||
|
||||
// Assert that both headers are sent and that it was called by both instances (3 each)
|
||||
Assert.equal(6, stub.callCount, "setRequestHeader called multiple times");
|
||||
Assert.equal(true, stub.calledWith(RequestHeaders.requestIdHeader)); // AI
|
||||
Assert.equal(true, stub.calledWith(RequestHeaders.traceParentHeader)); // W3C
|
||||
|
||||
let request = this._getXhrRequests();
|
||||
Assert.equal(1, request.length);
|
||||
// Assert that both headers are sent and that it was only called by the first instances (3 headers one only)
|
||||
Assert.equal(3, spy.callCount, "setRequestHeader called multiple times");
|
||||
Assert.equal(true, spy.calledWith(RequestHeaders.requestIdHeader)); // AI
|
||||
Assert.equal(true, spy.calledWith(RequestHeaders.traceParentHeader)); // W3C
|
||||
|
||||
// Assert that the W3C header is included
|
||||
Assert.equal(RequestHeaders.traceParentHeader, stub.args[2][0], "Validate the actual header sent");
|
||||
Assert.ok(stub.args[2][1].indexOf("00-" + firstTraceId) != -1, "Validate the actual header sent - actual: [" + stub.args[2][1] + "], expected parent [" + firstExpectedTraceParent + "] - alt: " + coreExpectedTraceParent);
|
||||
Assert.equal(RequestHeaders.traceParentHeader, spy.args[2][0], "Validate the actual header sent");
|
||||
Assert.ok(spy.args[2][1].indexOf("00-" + firstTraceId) != -1, "Validate the actual header sent - actual: [" + spy.args[2][1] + "], expected parent [" + firstExpectedTraceParent + "] - alt: " + coreExpectedTraceParent);
|
||||
|
||||
Assert.equal(RequestHeaders.requestIdHeader, spy.args[0][0], "Validate the actual header sent");
|
||||
Assert.ok(spy.args[0][1].indexOf("|" + firstTraceId + ".") != -1, "Validate the actual header sent - actual: [" + spy.args[0][1] + "], expected parent [" + firstExpectedTraceParent + "] - alt: " + coreExpectedTraceParent);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -454,17 +454,27 @@ export class AjaxMonitor extends BaseTelemetryPlugin implements IDependenciesPlu
|
|||
} else if (xhr) { // XHR
|
||||
if (correlationIdCanIncludeCorrelationHeader(_extensionConfig, ajaxData.getAbsoluteUrl(), currentWindowHost)) {
|
||||
if (_isUsingAIHeaders) {
|
||||
const id = "|" + ajaxData.traceID + "." + ajaxData.spanID;
|
||||
xhr.setRequestHeader(RequestHeaders[eRequestHeaders.requestIdHeader], id);
|
||||
if (_enableRequestHeaderTracking) {
|
||||
ajaxData.requestHeaders[RequestHeaders[eRequestHeaders.requestIdHeader]] = id;
|
||||
if (!_isHeaderSet(xhr, RequestHeaders[eRequestHeaders.requestIdHeader])) {
|
||||
const id = "|" + ajaxData.traceID + "." + ajaxData.spanID;
|
||||
xhr.setRequestHeader(RequestHeaders[eRequestHeaders.requestIdHeader], id);
|
||||
if (_enableRequestHeaderTracking) {
|
||||
ajaxData.requestHeaders[RequestHeaders[eRequestHeaders.requestIdHeader]] = id;
|
||||
}
|
||||
} else {
|
||||
_throwInternalWarning(_self, _eInternalMessageId.FailedMonitorAjaxSetRequestHeader,
|
||||
"Unable to set [" + RequestHeaders[eRequestHeaders.requestIdHeader] + "] as it has already been set by another instance");
|
||||
}
|
||||
}
|
||||
const appId = _appId || (_context && _context.appId());
|
||||
if (appId) {
|
||||
xhr.setRequestHeader(RequestHeaders[eRequestHeaders.requestContextHeader], RequestHeaders[eRequestHeaders.requestContextAppIdFormat] + appId);
|
||||
if (_enableRequestHeaderTracking) {
|
||||
ajaxData.requestHeaders[RequestHeaders[eRequestHeaders.requestContextHeader]] = RequestHeaders[eRequestHeaders.requestContextAppIdFormat] + appId;
|
||||
if (!_isHeaderSet(xhr, RequestHeaders[eRequestHeaders.requestContextHeader])) {
|
||||
xhr.setRequestHeader(RequestHeaders[eRequestHeaders.requestContextHeader], RequestHeaders[eRequestHeaders.requestContextAppIdFormat] + appId);
|
||||
if (_enableRequestHeaderTracking) {
|
||||
ajaxData.requestHeaders[RequestHeaders[eRequestHeaders.requestContextHeader]] = RequestHeaders[eRequestHeaders.requestContextAppIdFormat] + appId;
|
||||
}
|
||||
} else {
|
||||
_throwInternalWarning(_self, _eInternalMessageId.FailedMonitorAjaxSetRequestHeader,
|
||||
"Unable to set [" + RequestHeaders[eRequestHeaders.requestContextHeader] + "] as it has already been set by another instance");
|
||||
}
|
||||
}
|
||||
if (_isUsingW3CHeaders) {
|
||||
|
@ -798,12 +808,12 @@ export class AjaxMonitor extends BaseTelemetryPlugin implements IDependenciesPlu
|
|||
_hookProto(XMLHttpRequest, "setRequestHeader", {
|
||||
ns: _evtNamespace,
|
||||
req: (callDetails: IInstrumentCallDetails, header: string, value: string) => {
|
||||
if (!_disableAjaxTracking && _enableRequestHeaderTracking) {
|
||||
if (!_disableAjaxTracking) {
|
||||
let xhr = callDetails.inst as XMLHttpRequestInstrumented;
|
||||
let ajaxData = _getAjaxData(xhr, _ajaxDataId);
|
||||
if (_isMonitoredXhrInstance(xhr, ajaxData)) {
|
||||
if (ajaxData && _isMonitoredXhrInstance(xhr, ajaxData)) {
|
||||
_addSharedXhrHeaders(xhr, header, value);
|
||||
if (_canIncludeHeaders(header)) {
|
||||
if (_enableRequestHeaderTracking && _canIncludeHeaders(header)) {
|
||||
if (ajaxData) {
|
||||
ajaxData.requestHeaders[header] = value;
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче