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

Report total download size to newrelic #23877

Merged
merged 2 commits into from
Jul 24, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/src/appMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export default function (app, levels, options) {
next();
}
}
logToCloud.reportPageSize();
Copy link
Contributor

Choose a reason for hiding this comment

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

Out of curiosity, what types of things do we log to New Relic vs. AWS Firehose?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know if we have a general rule. In this case, the rest of the performance data I'm using is already in NewRelic, so I decided to log page sizes there as well.

logToCloud.loadFinished();
}
// exported apps can and need to be setup synchronously
Expand Down
48 changes: 43 additions & 5 deletions apps/src/logToCloud.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var utils = require('./utils');
import { makeEnum } from './utils';
import experiments from './util/experiments';

var PageAction = utils.makeEnum(
const PageAction = makeEnum(
'DropletTransitionError',
'FirebaseRateLimitExceeded',
'SanitizedLevelHtml',
Expand All @@ -12,11 +13,13 @@ var PageAction = utils.makeEnum(
'BrambleError'
);

var MAX_FIELD_LENGTH = 4095;
const MAX_FIELD_LENGTH = 4095;
const REPORT_PAGE_SIZE = experiments.isEnabled('logPageSize') ||
Math.random() < 0.01;

/**
* Shims window.newrelic, which is only included in production. This causes us
* to no-op in other environments.
* Wraps and adds functionality to window.newrelic, which is only included in
* production. This causes us to no-op in other environments.
*/
module.exports = {
PageAction: PageAction,
Expand Down Expand Up @@ -83,4 +86,39 @@ module.exports = {

window.newrelic.finished();
},

logError(e) {
if (!window.newrelic) {
return;
}
window.newrelic.noticeError(e);
},

reportPageSize() {
if (!REPORT_PAGE_SIZE) {
return;
}
try {
const resources = performance && performance.getEntriesByType('resource');
let totalDownloadSize = 0;
let jsDownloadSize = 0;
const jsFileRegex = /\.js$/;
for (const resource of resources) {
if (resource.transferSize === undefined || resource.encodedBodySize === undefined) {
return;
}
totalDownloadSize += resource.transferSize;
if (jsFileRegex.test(resource.name)) {
jsDownloadSize += resource.transferSize;
}
}
if (!window.newrelic) {
return;
}
window.newrelic.setCustomAttribute('totalDownloadSize', totalDownloadSize);
window.newrelic.setCustomAttribute('jsDownloadSize', jsDownloadSize);
} catch (e) {
this.logError(e);
}
},
};