Skip to content

Commit

Permalink
[gp-code] Report workbench errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mustard-mh committed Aug 29, 2022
1 parent 663d702 commit c983b52
Showing 1 changed file with 72 additions and 7 deletions.
79 changes: 72 additions & 7 deletions src/vs/gitpod/browser/workbench/workbench.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,71 @@
<html>
<head>
<script>
async function gitpodMetricsAddCounter(metricsName, labels, value) {
function getMetricsUrl() {
const baseWorkspaceIDRegex = '(([a-f][0-9a-f]{7}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})|([0-9a-z]{2,16}-[0-9a-z]{2,16}-[0-9a-z]{8,11}))';
// this pattern matches URL prefixes of workspaces
const workspaceUrlPrefixRegex = RegExp(`^([0-9]{4,6}-)?${baseWorkspaceIDRegex}\\.`);
const url = new URL(window.location.href);
url.search = '';
url.hash = '';
url.pathname = '';
if (url.host.match(workspaceUrlPrefixRegex)) {
url.host = url.host.split('.').splice(2).join('.');
} else {
return;
}
const hostSegments = url.host.split('.');
if (hostSegments[0] !== 'ide') {
url.host = 'ide.' + url.host;
}
url.pathname = '/metrics-api';
return url.toString();
}
try {
const metricsUrl = getMetricsUrl();
if (!metricsUrl) {
return false;
}
const url = `${metricsUrl}/metrics/counter/add/${metricsName}`;
const params = { value, labels };
const response = await fetch(url, {
method: 'POST',
body: JSON.stringify(params),
credentials: 'omit',
});
if (!response.ok) {
const data = await response.json(); // { code: number; message: string; }
console.error(`Cannot report metrics with addCounter: ${response.status} ${response.statusText}`, data);
return false;
}
return true;
} catch (err) {
console.error('Cannot report metrics with addCounter, error:', err);
return false;
}
}

const onVsCodeWorkbenchError = (event) => {
if (typeof event?.target?.getAttribute !== 'function') {
gitpodMetricsAddCounter('gitpod_supervisor_frontend_error_total');
return;
}
const labels = {};

// We take a look at what is the resource that was attempted to load;
const resourceSource = event.target.getAttribute('src') || event.target.getAttribute('href');

// If the event has a `target`, it means that it wasn't a script error
if (resourceSource) {
labels['resource'] = 'vscode-web-workbench';
labels['error'] = 'LoadError';
} else {
labels['error'] = 'Unknown';
}
gitpodMetricsAddCounter('gitpod_supervisor_frontend_error_total', labels);
};

performance.mark('code/didStartRenderer');
</script>
<meta charset="utf-8" />
Expand All @@ -28,17 +93,17 @@
<!-- Workbench Icon/Manifest/CSS -->
<link rel="icon" href="/favicon.ico" type="image/x-icon" />
<link rel="manifest" href="/manifest.json" />
<link data-name="vs/workbench/workbench.web.main" rel="stylesheet" href="./static/out/vs/workbench/workbench.web.main.css">
<link data-name="vs/workbench/workbench.web.main" rel="stylesheet" href="./static/out/vs/workbench/workbench.web.main.css" onerror="onVsCodeWorkbenchError(event)">

</head>

<body aria-label="">
</body>

<!-- Startup (do not modify order of script tags!) -->
<script type="text/javascript" src="/_supervisor/frontend/main.js" charset="utf-8"></script>
<script src="./static/out/vs/loader.js"></script>
<script src="./static/out/vs/webPackagePaths.js"></script>
<script type="text/javascript" src="/_supervisor/frontend/main.js" onerror="onVsCodeWorkbenchError(event)" charset="utf-8"></script>
<script src="./static/out/vs/loader.js" onerror="onVsCodeWorkbenchError(event)" ></script>
<script src="./static/out/vs/webPackagePaths.js" onerror="onVsCodeWorkbenchError(event)" ></script>
<script>
Object.keys(self.webPackagePaths).map(function (key, index) {
self.webPackagePaths[key] = `${window.location.origin}/static/node_modules/${key}/${self.webPackagePaths[key]}`;
Expand All @@ -62,7 +127,7 @@
<script>
performance.mark('code/willLoadWorkbenchMain');
</script>
<script src="./static/out/vs/workbench/workbench.web.main.nls.js"></script>
<script src="./static/out/vs/workbench/workbench.web.main.js"></script>
<script src="./static/out/vs/gitpod/browser/workbench/workbench.js"></script>
<script src="./static/out/vs/workbench/workbench.web.main.nls.js" onerror="onVsCodeWorkbenchError(event)"></script>
<script src="./static/out/vs/workbench/workbench.web.main.js" onerror="onVsCodeWorkbenchError(event)"></script>
<script src="./static/out/vs/gitpod/browser/workbench/workbench.js" onerror="onVsCodeWorkbenchError(event)"></script>
</html>

0 comments on commit c983b52

Please sign in to comment.