Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Commit

Permalink
Fix #5293, stop sending metrics if server isn't available
Browse files Browse the repository at this point in the history
Also adds logic to not send timing information if server is unavailable
  • Loading branch information
ianb committed Jan 15, 2019
1 parent d0e2700 commit 9bc130d
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions webextension/background/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ this.analytics = (function() {

let telemetryPrefKnown = false;
let telemetryEnabled;
// If we ever get a 410 Gone response from the server, we'll stop trying to send events for the rest
// If we ever get a 410 Gone response (or 404) from the server, we'll stop trying to send events for the rest
// of the session
let hasReturnedGone = false;
// If there's this many entirely failed responses (e.g., server can't be contacted), then stop sending events
// for the rest of the session:
let serverFailedResponses = 3;

const EVENT_BATCH_DURATION = 1000; // ms for setTimeout
let pendingEvents = [];
Expand Down Expand Up @@ -61,6 +64,9 @@ this.analytics = (function() {
function sendTiming(timingLabel, timingVar, timingValue) {
// sendTiming is only called in response to sendEvent, so no need to check
// the telemetry pref again here.
if (hasReturnedGone || serverFailedResponses <= 0) {
return;
}
const timingCategory = "addon";
pendingTimings.push({
timingCategory,
Expand Down Expand Up @@ -113,7 +119,7 @@ this.analytics = (function() {
for (const [gaField, value] of Object.entries(abTests)) {
options[gaField] = value;
}
if (hasReturnedGone) {
if (hasReturnedGone || serverFailedResponses <= 0) {
// We don't want to save or send the events anymore
return Promise.resolve();
}
Expand Down Expand Up @@ -315,14 +321,21 @@ this.analytics = (function() {

function fetchWatcher(request) {
request.then(response => {
if (response.status === 410) { // Gone
if (response.status === 410 || response.status === 404) { // Gone
hasReturnedGone = true;
pendingEvents = [];
pendingTimings = [];
}
if (!response.ok) {
log.debug(`Error code in event response: ${response.status} ${response.statusText}`);
}
}).catch(error => {
serverFailedResponses--;
if (serverFailedResponses <= 0) {
log.info(`Server is not responding, no more events will be sent`);
pendingEvents = [];
pendingTimings = [];
}
log.debug(`Error event in response: ${error}`);
});
}
Expand Down

0 comments on commit 9bc130d

Please sign in to comment.