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 8 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
1 change: 1 addition & 0 deletions AISKU/src/Snippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export interface Snippet {
queue?: Array<() => void>;
sv?: string;
version?: number;
cr?: boolean; // cdn retry would be proceed if ture
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once we have tested this, we will (I think) want this to be the default, so then if someone doesn't want to retry they would be required to set this to false and then we just change if (appInsights.cr){ to if (appInsights.cr !== false){ so that anything other then false would cause the retry to occur.

}
41 changes: 34 additions & 7 deletions tools/applicationinsights-web-snippet/src/snippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ declare var cfg:ISnippetConfig;
let ingest = conString[strIngestionendpoint];
let endpointUrl = ingest ? ingest + "/v2/track" : aiConfig.endpointUrl; // only add /v2/track when from connectionstring

let message = "SDK LOAD Failure: Failed to load Application Insights SDK script (See stack for details)";
let message = "SDK LOAD Failure: Failed to load Application Insights SDK script via " + targetSrc + " (See stack for details)";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to add the targetSrc here as it gets included in the message details via the _createException and _createInternal, so this becomes a duplication

let evts:IEnvelope[] = [];
evts.push(_createException(iKey, message, targetSrc, endpointUrl));
evts.push(_createInternal(iKey, message, targetSrc, endpointUrl));
Expand Down Expand Up @@ -183,6 +183,19 @@ declare var cfg:ISnippetConfig;

return envelope;
}

let domainRetryIndex = -1;
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",
// "js1.cdn.monitor.azure.com",
"js2.cdn.applicationinsights.io",
"js2.cdn.monitor.azure.com",
"az416426.vo.msecnd.net"
]

// 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,24 @@ declare var cfg:ISnippetConfig;
});
// let message = "Load Version 2 SDK instead to support IE"; // where to report this error?
}
if (appInsights.cr){
for (var i = 0; i < domains.length; i++){
if (targetSrc.indexOf(domains[i])){
siyuniu-ms marked this conversation as resolved.
Show resolved Hide resolved
domainRetryIndex = 0;
break;
}
}
const _handleError = (evt?: any) => {
loadFailed = true;
appInsights.queue = []; // Clear the queue
if (!handled) {
handled = true;
_reportFailure(targetSrc);
// start retry
if (domainRetryIndex > 0 && domainRetryIndex < domains.length){
siyuniu-ms marked this conversation as resolved.
Show resolved Hide resolved
theScript = _createScript("https://" + domains[domainRetryIndex] + "/scripts/b/ai.2.min.js");
} else {
handled = true;
loadFailed = true;
_reportFailure(targetSrc);
}
}
}

Expand All @@ -213,11 +238,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 @@ -235,7 +261,8 @@ declare var cfg:ISnippetConfig;
return scriptElement;
}

let theScript = _createScript();
let theScript = _createScript(targetSrc);

if (cfg.ld && cfg.ld < 0) {
// if user wants to append tag to document head, blocking page load
let headNode = doc.getElementsByTagName("head")[0];
Expand Down
Loading