Merge "Report plugins and extensions in metrics"

This commit is contained in:
Milutin Kristofic
2019-09-10 08:55:50 +00:00
committed by Gerrit Code Review
2 changed files with 38 additions and 5 deletions

View File

@@ -109,6 +109,9 @@
const pending = [];
const loadedPlugins = [];
const detectedExtensions = [];
const onError = function(oldOnError, msg, url, line, column, error) {
if (oldOnError) {
oldOnError(msg, url, line, column, error);
@@ -190,6 +193,7 @@
reporter(...args) {
const report = (this._isMetricsPluginLoaded() && !pending.length) ?
this.defaultReporter : this.cachingReporter;
args.splice(4, 0, loadedPlugins, detectedExtensions);
report.apply(this, args);
},
@@ -199,16 +203,23 @@
* @param {string} category
* @param {string} eventName
* @param {string|number} eventValue
* @param {Array} plugins
* @param {Array} extensions
* @param {boolean|undefined} opt_noLog If true, the event will not be
* logged to the JS console.
*/
defaultReporter(type, category, eventName, eventValue, opt_noLog) {
defaultReporter(type, category, eventName, eventValue,
loadedPlugins, detectedExtensions, opt_noLog) {
const detail = {
type,
category,
name: eventName,
value: eventValue,
};
if (category === TIMING.CATEGORY_UI_LATENCY) {
detail.loadedPlugins = loadedPlugins;
detail.detectedExtensions = detectedExtensions;
}
document.dispatchEvent(new CustomEvent(type, {detail}));
if (opt_noLog) { return; }
if (type === ERROR.TYPE && category === ERROR.CATEGORY) {
@@ -229,10 +240,13 @@
* @param {string} category
* @param {string} eventName
* @param {string|number} eventValue
* @param {Array} plugins
* @param {Array} extensions
* @param {boolean|undefined} opt_noLog If true, the event will not be
* logged to the JS console.
*/
cachingReporter(type, category, eventName, eventValue, opt_noLog) {
cachingReporter(type, category, eventName, eventValue,
plugins, extensions, opt_noLog) {
if (type === ERROR.TYPE && category === ERROR.CATEGORY) {
console.error(eventValue && eventValue.error || eventName);
}
@@ -242,9 +256,11 @@
this.reporter(...args);
}
}
this.reporter(type, category, eventName, eventValue, opt_noLog);
this.reporter(type, category, eventName, eventValue,
plugins, extensions, opt_noLog);
} else {
pending.push([type, category, eventName, eventValue, opt_noLog]);
pending.push([type, category, eventName, eventValue,
plugins, extensions, opt_noLog]);
}
},
@@ -337,12 +353,16 @@
reportExtension(name) {
this.reporter(EXTENSION.TYPE, EXTENSION.DETECTED, name);
if (!detectedExtensions.includes(name)) {
detectedExtensions.push(name);
}
},
pluginLoaded(name) {
if (name.startsWith('metrics-')) {
this.timeEnd(TIMER.METRICS_PLUGIN_LOADED);
}
loadedPlugins.push(name);
},
pluginsLoaded(pluginsList) {

View File

@@ -265,7 +265,7 @@ limitations under the License.
test('pluginsLoaded reports time', () => {
sandbox.stub(element, 'now').returns(42);
element.pluginsLoaded();
assert.isTrue(element.defaultReporter.calledWithExactly(
assert.isTrue(element.defaultReporter.calledWith(
'timing-report', 'UI Latency', 'PluginsLoaded', 42
));
});
@@ -287,6 +287,19 @@ limitations under the License.
assert.isTrue(element.defaultReporter.called);
});
test('reports plugins in timing events', () => {
element.pluginsLoaded = [];
sandbox.stub(element, 'now').returns(42);
element.pluginLoaded('metrics-xyz1');
// element.pluginLoaded('foo');
element.time('timeAction');
element.timeEnd('timeAction');
assert.isTrue(element.defaultReporter.getCall(1).calledWith(
'timing-report', 'UI Latency', 'timeAction', 0,
['metrics-xyz1']
));
});
test('reports if metrics plugin xyz is loaded', () => {
element.pluginLoaded('metrics-xyz');
assert.isTrue(element.defaultReporter.called);