Bug 1059186 - Client needs to report number of generated URLs via Telemetry. r=MattN

This commit is contained in:
Paolo Amadini 2014-09-18 13:05:39 +01:00
Родитель 22f9b706c4
Коммит a24d21503f
6 изменённых файлов: 124 добавлений и 3 удалений

Просмотреть файл

@ -474,7 +474,21 @@ function injectLoopAPI(targetWindow) {
"body=" + encodeURIComponent(body);
extProtocolSvc.loadURI(CommonUtils.makeURI(mailtoURL));
}
}
},
/**
* Adds a value to a telemetry histogram.
*
* @param {string} histogramId Name of the telemetry histogram to update.
* @param {integer} value Value to add to the histogram.
*/
telemetryAdd: {
enumerable: true,
writable: true,
value: function(histogramId, value) {
Services.telemetry.getHistogramById(histogramId).add(value);
}
},
};
function onStatusChanged(aSubject, aTopic, aData) {

Просмотреть файл

@ -111,6 +111,7 @@ loop.Client = (function($) {
this.mozLoop.hawkRequest("/call-url/", "POST", {callerId: nickname},
function (error, responseText) {
if (error) {
this._telemetryAdd("LOOP_CLIENT_CALL_URL_REQUESTS_SUCCESS", false);
this._failureHandler(cb, error);
return;
}
@ -118,8 +119,14 @@ loop.Client = (function($) {
try {
var urlData = JSON.parse(responseText);
cb(null, this._validate(urlData, expectedCallUrlProperties));
// This throws if the data is invalid, in which case only the failure
// telementry will be recorded.
var returnData = this._validate(urlData, expectedCallUrlProperties);
this._telemetryAdd("LOOP_CLIENT_CALL_URL_REQUESTS_SUCCESS", true);
cb(null, returnData);
} catch (err) {
this._telemetryAdd("LOOP_CLIENT_CALL_URL_REQUESTS_SUCCESS", false);
console.log("Error requesting call info", err);
cb(err);
}
@ -187,6 +194,20 @@ loop.Client = (function($) {
this._requestCallUrlInternal(nickname, cb);
}.bind(this));
},
/**
* Adds a value to a telemetry histogram, ignoring errors.
*
* @param {string} histogramId Name of the telemetry histogram to update.
* @param {integer} value Value to add to the histogram.
*/
_telemetryAdd: function(histogramId, value) {
try {
this.mozLoop.telemetryAdd(histogramId, value);
} catch (err) {
console.error("Error recording telemetry", err);
}
},
};
return Client;

Просмотреть файл

@ -34,7 +34,8 @@ describe("loop.Client", function() {
.returns(fakeToken),
ensureRegistered: sinon.stub().callsArgWith(0, null),
noteCallUrlExpiry: sinon.spy(),
hawkRequest: sinon.stub()
hawkRequest: sinon.stub(),
telemetryAdd: sinon.spy(),
};
// Alias for clearer tests.
hawkRequestStub = mozLoop.hawkRequest;
@ -156,6 +157,30 @@ describe("loop.Client", function() {
sinon.assert.notCalled(mozLoop.noteCallUrlExpiry);
});
it("should call mozLoop.telemetryAdd when the request succeeds",
function(done) {
var callUrlData = {
"callUrl": "fakeCallUrl",
"expiresAt": 60
};
// Sets up the hawkRequest stub to trigger the callback with no error
// and the url.
hawkRequestStub.callsArgWith(3, null,
JSON.stringify(callUrlData));
client.requestCallUrl("foo", function(err) {
expect(err).to.be.null;
sinon.assert.calledOnce(mozLoop.telemetryAdd);
sinon.assert.calledWith(mozLoop.telemetryAdd,
"LOOP_CLIENT_CALL_URL_REQUESTS_SUCCESS",
true);
done();
});
});
it("should send an error when the request fails", function() {
// Sets up the hawkRequest stub to trigger the callback with
// an error
@ -181,6 +206,24 @@ describe("loop.Client", function() {
return /Invalid data received/.test(err.message);
}));
});
it("should call mozLoop.telemetryAdd when the request fails",
function(done) {
// Sets up the hawkRequest stub to trigger the callback with
// an error
hawkRequestStub.callsArgWith(3, fakeErrorRes);
client.requestCallUrl("foo", function(err) {
expect(err).not.to.be.null;
sinon.assert.calledOnce(mozLoop.telemetryAdd);
sinon.assert.calledWith(mozLoop.telemetryAdd,
"LOOP_CLIENT_CALL_URL_REQUESTS_SUCCESS",
false);
done();
});
});
});
});
});

Просмотреть файл

@ -16,3 +16,5 @@ skip-if = e10s
skip-if = buildapp == 'mulet'
[browser_toolbarbutton.js]
[browser_mozLoop_pluralStrings.js]
[browser_mozLoop_telemetry.js]
skip-if = e10s

Просмотреть файл

@ -0,0 +1,36 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
/*
* This file contains tests for the mozLoop telemetry API.
*/
add_task(loadLoopPanel);
/**
* Tests that boolean histograms exist and can be updated.
*/
add_task(function* test_mozLoop_telemetryAdd_boolean() {
for (let histogramId of [
"LOOP_CLIENT_CALL_URL_REQUESTS_SUCCESS",
]) {
let snapshot = Services.telemetry.getHistogramById(histogramId).snapshot();
let initialFalseCount = snapshot.counts[0];
let initialTrueCount = snapshot.counts[1];
for (let value of [false, false, true]) {
gMozLoopAPI.telemetryAdd(histogramId, value);
}
// The telemetry service updates histograms asynchronously, so we need to
// poll for the final values and time out otherwise.
info("Waiting for update of " + histogramId);
do {
yield new Promise(resolve => setTimeout(resolve, 50));
snapshot = Services.telemetry.getHistogramById(histogramId).snapshot();
} while (snapshot.counts[0] == initialFalseCount + 2 &&
snapshot.counts[1] == initialTrueCount + 1);
ok(true, "Correctly updated " + histogramId);
}
});

Просмотреть файл

@ -6589,6 +6589,11 @@
"n_values": 3,
"description": "Doorhanger shown = 0, Disable = 1, Enable = 2"
},
"LOOP_CLIENT_CALL_URL_REQUESTS_SUCCESS": {
"expires_in_version": "never",
"kind": "boolean",
"description": "Stores 1 if generating a call URL succeeded, and 0 if it failed."
},
"E10S_AUTOSTART": {
"expires_in_version": "never",
"kind": "boolean",