Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[main] Adding round robin retry for snippet script src loading #2131

Merged
merged 20 commits into from
Aug 29, 2023
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
68 changes: 51 additions & 17 deletions tools/applicationinsights-web-snippet/src/snippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,19 @@ declare var cfg:ISnippetConfig;

return envelope;
}

let domainRetryIndex = -1;
let domainRetryCount = 0;
let domains = [
"js.monitor.azure.com",
siyuniu-ms marked this conversation as resolved.
Show resolved Hide resolved
"js.cdn.applicationinsights.io",
"js.cdn.monitor.azure.com",
"js0.cdn.applicationinsights.io",
"js0.cdn.monitor.azure.com",
"js2.cdn.applicationinsights.io",
"js2.cdn.monitor.azure.com",
"az416426.vo.msecnd.net" // this domain is supported but not recommended
]

// Assigning these to local variables allows them to be minified to save space:
let targetSrc : string = (aiConfig as any)["url"] || cfg.src
Expand All @@ -194,12 +207,31 @@ declare var cfg:ISnippetConfig;
});
// let message = "Load Version 2 SDK instead to support IE"; // where to report this error?
}

if (cfg.cr !== false){
for (var i = 0; i < domains.length; i++){
if (targetSrc.indexOf(domains[i]) > 0){
domainRetryIndex = i;
break;
}
}
}

const _handleError = (evt?: any) => {
loadFailed = true;
appInsights.queue = []; // Clear the queue
if (!handled) {
handled = true;
_reportFailure(targetSrc);
// start retry
if (domainRetryIndex >= 0 && domainRetryCount + 1 < domains.length){ // domainRetryIndex will be negative when client using own domain (the supported domain list is defined above)
let nextIdx = (domainRetryIndex + domainRetryCount + 1) % domains.length;
_createScript(targetSrc.replace(/^(.*\/\/)([\w\.]*)(\/.*)$/, function (_all, http, domain, qs) {
return http + domains[nextIdx] + qs;
}));
domainRetryCount += 1;
} else {
handled = true;
loadFailed = true;
_reportFailure(targetSrc);
}
}
}

Expand All @@ -213,11 +245,12 @@ declare var cfg:ISnippetConfig;
}
}, 500);
}
loadFailed = false;
}

const _createScript = () => {
const _createScript = (src: string) => {
let scriptElement : HTMLElement = doc.createElement(scriptText);
(scriptElement as any)["src"] = targetSrc;
(scriptElement as any)["src"] = src;
// Allocate Cross origin only if defined and available
let crossOrigin = cfg[strCrossOrigin];
if ((crossOrigin || crossOrigin === "") && scriptElement[strCrossOrigin] != strUndefined) {
Expand All @@ -232,20 +265,21 @@ declare var cfg:ISnippetConfig;
}
};

return scriptElement;
}
if (cfg.ld && cfg.ld < 0) {
// if user wants to append tag to document head, blocking page load
let headNode = doc.getElementsByTagName("head")[0];
headNode.appendChild(scriptElement);
} else {
setTimeout(function () {
// Attempts to place the script tag in the same location as the first script on the page
doc.getElementsByTagName(scriptText)[0].parentNode.appendChild(scriptElement);
}, cfg.ld || 0);
}


let theScript = _createScript();
if (cfg.ld && cfg.ld < 0) {
// if user wants to append tag to document head, blocking page load
let headNode = doc.getElementsByTagName("head")[0];
headNode.appendChild(theScript);
} else {
setTimeout(function () {
// Attempts to place the script tag in the same location as the first script on the page
doc.getElementsByTagName(scriptText)[0].parentNode.appendChild(theScript);
}, cfg.ld || 0);
return scriptElement;
}
_createScript(targetSrc);
}

// capture initial cookie
Expand Down
1 change: 1 addition & 0 deletions tools/applicationinsights-web-snippet/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface ISnippetConfig {
crossOrigin?: string;
onInit?: any;
cfg: IConfiguration;
cr?: boolean; // cdn retry would be proceed if ture
}

export interface Fields {
Expand Down
Loading