-
Notifications
You must be signed in to change notification settings - Fork 54
/
TelemetrySend.jsm
1363 lines (1168 loc) · 46.4 KB
/
TelemetrySend.jsm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* This module is responsible for uploading pings to the server and persisting
* pings that can't be send now.
* Those pending pings are persisted on disk and sent at the next opportunity,
* newest first.
*/
"use strict";
var EXPORTED_SYMBOLS = [
"TelemetrySend",
];
ChromeUtils.import("resource://gre/modules/AppConstants.jsm", this);
ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm", this);
const {ClientID} = ChromeUtils.import("resource://gre/modules/ClientID.jsm");
ChromeUtils.import("resource://gre/modules/Log.jsm", this);
const {PromiseUtils} = ChromeUtils.import("resource://gre/modules/PromiseUtils.jsm");
ChromeUtils.import("resource://gre/modules/ServiceRequest.jsm", this);
ChromeUtils.import("resource://gre/modules/Services.jsm", this);
ChromeUtils.import("resource://gre/modules/TelemetryUtils.jsm", this);
ChromeUtils.import("resource://gre/modules/Timer.jsm", this);
ChromeUtils.defineModuleGetter(this, "TelemetryStorage",
"resource://gre/modules/TelemetryStorage.jsm");
ChromeUtils.defineModuleGetter(this, "TelemetryReportingPolicy",
"resource://gre/modules/TelemetryReportingPolicy.jsm");
ChromeUtils.defineModuleGetter(this, "OS",
"resource://gre/modules/osfile.jsm");
XPCOMUtils.defineLazyServiceGetter(this, "Telemetry",
"@mozilla.org/base/telemetry;1",
"nsITelemetry");
ChromeUtils.defineModuleGetter(this, "TelemetryHealthPing",
"resource://gre/modules/HealthPing.jsm");
const Utils = TelemetryUtils;
const LOGGER_NAME = "Toolkit.Telemetry";
const LOGGER_PREFIX = "TelemetrySend::";
const TOPIC_IDLE_DAILY = "idle-daily";
// The following topics are notified when Firefox is closing
// because the OS is shutting down.
const TOPIC_QUIT_APPLICATION_GRANTED = "quit-application-granted";
const TOPIC_QUIT_APPLICATION_FORCED = "quit-application-forced";
const PREF_CHANGED_TOPIC = "nsPref:changed";
const TOPIC_PROFILE_CHANGE_NET_TEARDOWN = "profile-change-net-teardown";
// Whether the FHR/Telemetry unification features are enabled.
// Changing this pref requires a restart.
const IS_UNIFIED_TELEMETRY = Services.prefs.getBoolPref(TelemetryUtils.Preferences.Unified, false);
const MS_IN_A_MINUTE = 60 * 1000;
const PING_TYPE_OPTOUT = "optout";
// We try to spread "midnight" pings out over this interval.
const MIDNIGHT_FUZZING_INTERVAL_MS = 60 * MS_IN_A_MINUTE;
// We delay sending "midnight" pings on this client by this interval.
const MIDNIGHT_FUZZING_DELAY_MS = Math.random() * MIDNIGHT_FUZZING_INTERVAL_MS;
// Timeout after which we consider a ping submission failed.
const PING_SUBMIT_TIMEOUT_MS = 1.5 * MS_IN_A_MINUTE;
// To keep resource usage in check, we limit ping sending to a maximum number
// of pings per minute.
const MAX_PING_SENDS_PER_MINUTE = 10;
// If we have more pending pings then we can send right now, we schedule the next
// send for after SEND_TICK_DELAY.
const SEND_TICK_DELAY = 1 * MS_IN_A_MINUTE;
// If we had any ping send failures since the last ping, we use a backoff timeout
// for the next ping sends. We increase the delay exponentially up to a limit of
// SEND_MAXIMUM_BACKOFF_DELAY_MS.
// This exponential backoff will be reset by external ping submissions & idle-daily.
const SEND_MAXIMUM_BACKOFF_DELAY_MS = 120 * MS_IN_A_MINUTE;
// Strings to map from XHR.errorCode to TELEMETRY_SEND_FAILURE_TYPE.
// Echoes XMLHttpRequestMainThread's ErrorType enum.
// Make sure that any additions done to XHR_ERROR_TYPE enum are also mirrored in
// TELEMETRY_SEND_FAILURE_TYPE's labels.
const XHR_ERROR_TYPE = [
"eOK",
"eRequest",
"eUnreachable",
"eChannelOpen",
"eRedirect",
"eTerminated",
];
/**
* This is a policy object used to override behavior within this module.
* Tests override properties on this object to allow for control of behavior
* that would otherwise be very hard to cover.
*/
var Policy = {
now: () => new Date(),
midnightPingFuzzingDelay: () => MIDNIGHT_FUZZING_DELAY_MS,
pingSubmissionTimeout: () => PING_SUBMIT_TIMEOUT_MS,
setSchedulerTickTimeout: (callback, delayMs) => setTimeout(callback, delayMs),
clearSchedulerTickTimeout: (id) => clearTimeout(id),
gzipCompressString: (data) => gzipCompressString(data),
};
/**
* Determine if the ping has the new v4 ping format or the legacy v2 one or earlier.
*/
function isV4PingFormat(aPing) {
return ("id" in aPing) && ("application" in aPing) &&
("version" in aPing) && (aPing.version >= 2);
}
/**
* Check if the provided ping is an optout ping.
* @param {Object} aPing The ping to check.
* @return {Boolean} True if the ping is an optout ping, false otherwise.
*/
function isOptoutPing(aPing) {
return isV4PingFormat(aPing) && (aPing.type == PING_TYPE_OPTOUT);
}
/**
* Save the provided ping as a pending ping.
* @param {Object} aPing The ping to save.
* @return {Promise} A promise resolved when the ping is saved.
*/
function savePing(aPing) {
return TelemetryStorage.savePendingPing(aPing);
}
/**
* @return {String} This returns a string with the gzip compressed data.
*/
function gzipCompressString(string) {
let observer = {
buffer: "",
onStreamComplete(loader, context, status, length, result) {
// String.fromCharCode can only deal with 500,000 characters at
// a time, so chunk the result into parts of that size.
const chunkSize = 500000;
for (let offset = 0; offset < result.length; offset += chunkSize) {
this.buffer += String.fromCharCode.apply(String, result.slice(offset, offset + chunkSize));
}
},
};
let scs = Cc["@mozilla.org/streamConverters;1"]
.getService(Ci.nsIStreamConverterService);
let listener = Cc["@mozilla.org/network/stream-loader;1"]
.createInstance(Ci.nsIStreamLoader);
listener.init(observer);
let converter = scs.asyncConvertData("uncompressed", "gzip",
listener, null);
let stringStream = Cc["@mozilla.org/io/string-input-stream;1"]
.createInstance(Ci.nsIStringInputStream);
stringStream.data = string;
converter.onStartRequest(null, null);
converter.onDataAvailable(null, stringStream, 0, string.length);
converter.onStopRequest(null, null, null);
return observer.buffer;
}
var TelemetrySend = {
get pendingPingCount() {
return TelemetrySendImpl.pendingPingCount;
},
/**
* Partial setup that runs immediately at startup. This currently triggers
* the crash report annotations.
*/
earlyInit() {
TelemetrySendImpl.earlyInit();
},
/**
* Initializes this module.
*
* @param {Boolean} testing Whether this is run in a test. This changes some behavior
* to enable proper testing.
* @return {Promise} Resolved when setup is finished.
*/
setup(testing = false) {
return TelemetrySendImpl.setup(testing);
},
/**
* Shutdown this module - this will cancel any pending ping tasks and wait for
* outstanding async activity like network and disk I/O.
*
* @return {Promise} Promise that is resolved when shutdown is finished.
*/
shutdown() {
return TelemetrySendImpl.shutdown();
},
/**
* Submit a ping for sending. This will:
* - send the ping right away if possible or
* - save the ping to disk and send it at the next opportunity
*
* @param {Object} ping The ping data to send, must be serializable to JSON.
* @param {Object} [aOptions] Options object.
* @param {Boolean} [options.usePingSender=false] if true, send the ping using the PingSender.
* @return {Promise} Test-only - a promise that is resolved when the ping is sent or saved.
*/
submitPing(ping, options = {}) {
options.usePingSender = options.usePingSender || false;
return TelemetrySendImpl.submitPing(ping, options);
},
/**
* Check if sending is disabled. If Telemetry is not allowed to upload,
* pings are not sent to the server.
* If trying to send an optout ping, don't block it.
*
* @param {Object} [ping=null] A ping to be checked.
* @return {Boolean} True if pings can be send to the servers, false otherwise.
*/
sendingEnabled(ping = null) {
return TelemetrySendImpl.sendingEnabled(ping);
},
/**
* Notify that we can start submitting data to the servers.
*/
notifyCanUpload() {
return TelemetrySendImpl.notifyCanUpload();
},
/**
* Only used in tests. Used to reset the module data to emulate a restart.
*/
reset() {
return TelemetrySendImpl.reset();
},
/**
* Only used in tests.
*/
setServer(server) {
return TelemetrySendImpl.setServer(server);
},
/**
* Clear out unpersisted, yet to be sent, pings and cancel outgoing ping requests.
*/
clearCurrentPings() {
return TelemetrySendImpl.clearCurrentPings();
},
/**
* Only used in tests to wait on outgoing pending pings.
*/
testWaitOnOutgoingPings() {
return TelemetrySendImpl.promisePendingPingActivity();
},
/**
* Only used in tests to set whether it is too late in shutdown to send pings.
*/
testTooLateToSend(tooLate) {
TelemetrySendImpl._tooLateToSend = tooLate;
},
/**
* Test-only - this allows overriding behavior to enable ping sending in debug builds.
*/
setTestModeEnabled(testing) {
TelemetrySendImpl.setTestModeEnabled(testing);
},
/**
* This returns state info for this module for AsyncShutdown timeout diagnostics.
*/
getShutdownState() {
return TelemetrySendImpl.getShutdownState();
},
/**
* Send a ping using the ping sender.
* This method will not wait for the ping to be sent, instead it will return
* as soon as the pingsender program has been launched.
*
* This method is currently exposed here only for testing purposes as it's
* only used internally.
*
* @param {String} aUrl The telemetry server URL
* @param {String} aPingFilePath The path to the file holding the ping
* contents, if if sent successfully the pingsender will delete it.
*
* @throws NS_ERROR_FAILURE if we couldn't find or run the pingsender
* executable.
* @throws NS_ERROR_NOT_IMPLEMENTED on Android as the pingsender is not
* available.
*/
testRunPingSender(url, pingPath) {
TelemetrySendImpl.runPingSender(url, pingPath);
},
};
var CancellableTimeout = {
_deferred: null,
_timer: null,
/**
* This waits until either the given timeout passed or the timeout was cancelled.
*
* @param {Number} timeoutMs The timeout in ms.
* @return {Promise<bool>} Promise that is resolved with false if the timeout was cancelled,
* false otherwise.
*/
promiseWaitOnTimeout(timeoutMs) {
if (!this._deferred) {
this._deferred = PromiseUtils.defer();
this._timer = Policy.setSchedulerTickTimeout(() => this._onTimeout(), timeoutMs);
}
return this._deferred.promise;
},
_onTimeout() {
if (this._deferred) {
this._deferred.resolve(false);
this._timer = null;
this._deferred = null;
}
},
cancelTimeout() {
if (this._deferred) {
Policy.clearSchedulerTickTimeout(this._timer);
this._deferred.resolve(true);
this._timer = null;
this._deferred = null;
}
},
};
/**
* SendScheduler implements the timer & scheduling behavior for ping sends.
*/
var SendScheduler = {
// Whether any ping sends failed since the last tick. If yes, we start with our exponential
// backoff timeout.
_sendsFailed: false,
// The current retry delay after ping send failures. We use this for the exponential backoff,
// increasing this value everytime we had send failures since the last tick.
_backoffDelay: SEND_TICK_DELAY,
_shutdown: false,
_sendTask: null,
// A string that tracks the last seen send task state, null if it never ran.
_sendTaskState: null,
_logger: null,
get _log() {
if (!this._logger) {
this._logger = Log.repository.getLoggerWithMessagePrefix(LOGGER_NAME, LOGGER_PREFIX + "Scheduler::");
}
return this._logger;
},
shutdown() {
this._log.trace("shutdown");
this._shutdown = true;
CancellableTimeout.cancelTimeout();
return Promise.resolve(this._sendTask);
},
start() {
this._log.trace("start");
this._sendsFailed = false;
this._backoffDelay = SEND_TICK_DELAY;
this._shutdown = false;
},
/**
* Only used for testing, resets the state to emulate a restart.
*/
reset() {
this._log.trace("reset");
return this.shutdown().then(() => this.start());
},
/**
* Notify the scheduler of a failure in sending out pings that warrants retrying.
* This will trigger the exponential backoff timer behavior on the next tick.
*/
notifySendsFailed() {
this._log.trace("notifySendsFailed");
if (this._sendsFailed) {
return;
}
this._sendsFailed = true;
this._log.trace("notifySendsFailed - had send failures");
},
/**
* Returns whether ping submissions are currently throttled.
*/
isThrottled() {
const now = Policy.now();
const nextPingSendTime = this._getNextPingSendTime(now);
return (nextPingSendTime > now.getTime());
},
waitOnSendTask() {
return Promise.resolve(this._sendTask);
},
triggerSendingPings(immediately) {
this._log.trace("triggerSendingPings - active send task: " + !!this._sendTask + ", immediately: " + immediately);
if (!this._sendTask) {
this._sendTask = this._doSendTask();
let clear = () => this._sendTask = null;
this._sendTask.then(clear, clear);
} else if (immediately) {
CancellableTimeout.cancelTimeout();
}
return this._sendTask;
},
async _doSendTask() {
this._sendTaskState = "send task started";
this._backoffDelay = SEND_TICK_DELAY;
this._sendsFailed = false;
const resetBackoffTimer = () => {
this._backoffDelay = SEND_TICK_DELAY;
};
for (;;) {
this._log.trace("_doSendTask iteration");
this._sendTaskState = "start iteration";
if (this._shutdown) {
this._log.trace("_doSendTask - shutting down, bailing out");
this._sendTaskState = "bail out - shutdown check";
return;
}
// Get a list of pending pings, sorted by last modified, descending.
// Filter out all the pings we can't send now. This addresses scenarios like "optout" pings
// which can be sent even when upload is disabled.
let pending = TelemetryStorage.getPendingPingList();
let current = TelemetrySendImpl.getUnpersistedPings();
this._log.trace("_doSendTask - pending: " + pending.length + ", current: " + current.length);
// Note that the two lists contain different kind of data. |pending| only holds ping
// info, while |current| holds actual ping data.
if (!TelemetrySendImpl.sendingEnabled()) {
// If sending is disabled, only handle an unpersisted optout ping
pending = [];
current = current.filter(p => isOptoutPing(p));
}
this._log.trace("_doSendTask - can send - pending: " + pending.length + ", current: " + current.length);
// Bail out if there is nothing to send.
if ((pending.length == 0) && (current.length == 0)) {
this._log.trace("_doSendTask - no pending pings, bailing out");
this._sendTaskState = "bail out - no pings to send";
return;
}
// If we are currently throttled (e.g. fuzzing to avoid midnight spikes), wait for the next send window.
const now = Policy.now();
if (this.isThrottled()) {
const nextPingSendTime = this._getNextPingSendTime(now);
this._log.trace("_doSendTask - throttled, delaying ping send to " + new Date(nextPingSendTime));
this._sendTaskState = "wait for throttling to pass";
const delay = nextPingSendTime - now.getTime();
const cancelled = await CancellableTimeout.promiseWaitOnTimeout(delay);
if (cancelled) {
this._log.trace("_doSendTask - throttling wait was cancelled, resetting backoff timer");
resetBackoffTimer();
}
continue;
}
let sending = pending.slice(0, MAX_PING_SENDS_PER_MINUTE);
pending = pending.slice(MAX_PING_SENDS_PER_MINUTE);
this._log.trace("_doSendTask - triggering sending of " + sending.length + " pings now" +
", " + pending.length + " pings waiting");
this._sendsFailed = false;
const sendStartTime = Policy.now();
this._sendTaskState = "wait on ping sends";
await TelemetrySendImpl.sendPings(current, sending.map(p => p.id));
if (this._shutdown || (TelemetrySend.pendingPingCount == 0)) {
this._log.trace("_doSendTask - bailing out after sending, shutdown: " + this._shutdown +
", pendingPingCount: " + TelemetrySend.pendingPingCount);
this._sendTaskState = "bail out - shutdown & pending check after send";
return;
}
// Calculate the delay before sending the next batch of pings.
// We start with a delay that makes us send max. 1 batch per minute.
// If we had send failures in the last batch, we will override this with
// a backoff delay.
const timeSinceLastSend = Policy.now() - sendStartTime;
let nextSendDelay = Math.max(0, SEND_TICK_DELAY - timeSinceLastSend);
if (!this._sendsFailed) {
this._log.trace("_doSendTask - had no send failures, resetting backoff timer");
resetBackoffTimer();
} else {
const newDelay = Math.min(SEND_MAXIMUM_BACKOFF_DELAY_MS,
this._backoffDelay * 2);
this._log.trace("_doSendTask - had send failures, backing off -" +
" old timeout: " + this._backoffDelay +
", new timeout: " + newDelay);
this._backoffDelay = newDelay;
nextSendDelay = this._backoffDelay;
}
this._log.trace("_doSendTask - waiting for next send opportunity, timeout is " + nextSendDelay);
this._sendTaskState = "wait on next send opportunity";
const cancelled = await CancellableTimeout.promiseWaitOnTimeout(nextSendDelay);
if (cancelled) {
this._log.trace("_doSendTask - batch send wait was cancelled, resetting backoff timer");
resetBackoffTimer();
}
}
},
/**
* This helper calculates the next time that we can send pings at.
* Currently this mostly redistributes ping sends from midnight until one hour after
* to avoid submission spikes around local midnight for daily pings.
*
* @param now Date The current time.
* @return Number The next time (ms from UNIX epoch) when we can send pings.
*/
_getNextPingSendTime(now) {
// 1. First we check if the pref is set to skip any delay and send immediately.
// 2. Next we check if the time is between 0am and 1am. If it's not, we send
// immediately.
// 3. If we confirmed the time is indeed between 0am and 1am in step 1, we disallow
// sending before (midnight + fuzzing delay), which is a random time between 0am-1am
// (decided at startup).
let disableFuzzingDelay = Services.prefs.getBoolPref(TelemetryUtils.Preferences.DisableFuzzingDelay, false);
if (disableFuzzingDelay) {
return now.getTime();
}
const midnight = Utils.truncateToDays(now);
// Don't delay pings if we are not within the fuzzing interval.
if ((now.getTime() - midnight.getTime()) > MIDNIGHT_FUZZING_INTERVAL_MS) {
return now.getTime();
}
// Delay ping send if we are within the midnight fuzzing range.
// We spread those ping sends out between |midnight| and |midnight + midnightPingFuzzingDelay|.
return midnight.getTime() + Policy.midnightPingFuzzingDelay();
},
getShutdownState() {
return {
shutdown: this._shutdown,
hasSendTask: !!this._sendTask,
sendsFailed: this._sendsFailed,
sendTaskState: this._sendTaskState,
backoffDelay: this._backoffDelay,
};
},
};
var TelemetrySendImpl = {
_sendingEnabled: false,
// Tracks the shutdown state.
_shutdown: false,
_logger: null,
// This tracks all pending ping requests to the server.
_pendingPingRequests: new Map(),
// This tracks all the pending async ping activity.
_pendingPingActivity: new Set(),
// This is true when running in the test infrastructure.
_testMode: false,
// This holds pings that we currently try and haven't persisted yet.
_currentPings: new Map(),
// Used to skip spawning the pingsender if OS is shutting down.
_isOSShutdown: false,
// Has the network shut down, making it too late to send pings?
_tooLateToSend: false,
OBSERVER_TOPICS: [
TOPIC_IDLE_DAILY,
TOPIC_QUIT_APPLICATION_GRANTED,
TOPIC_QUIT_APPLICATION_FORCED,
TOPIC_PROFILE_CHANGE_NET_TEARDOWN,
],
OBSERVED_PREFERENCES: [
TelemetryUtils.Preferences.TelemetryEnabled,
TelemetryUtils.Preferences.FhrUploadEnabled,
],
// Whether sending pings has been overridden.
get _overrideOfficialCheck() {
return Services.prefs.getBoolPref(TelemetryUtils.Preferences.OverrideOfficialCheck, false);
},
get _log() {
if (!this._logger) {
this._logger = Log.repository.getLoggerWithMessagePrefix(LOGGER_NAME, LOGGER_PREFIX);
}
return this._logger;
},
get pendingPingRequests() {
return this._pendingPingRequests;
},
get pendingPingCount() {
return TelemetryStorage.getPendingPingList().length + this._currentPings.size;
},
setTestModeEnabled(testing) {
this._testMode = testing;
},
earlyInit() {
this._annotateCrashReport();
// Install the observer to detect OS shutdown early enough, so
// that we catch this before the delayed setup happens.
Services.obs.addObserver(this, TOPIC_QUIT_APPLICATION_FORCED);
Services.obs.addObserver(this, TOPIC_QUIT_APPLICATION_GRANTED);
},
QueryInterface: ChromeUtils.generateQI([Ci.nsISupportsWeakReference]),
async setup(testing) {
this._log.trace("setup");
this._testMode = testing;
Services.obs.addObserver(this, TOPIC_IDLE_DAILY);
Services.obs.addObserver(this, TOPIC_PROFILE_CHANGE_NET_TEARDOWN);
this._server = Services.prefs.getStringPref(TelemetryUtils.Preferences.Server, undefined);
this._sendingEnabled = true;
// Annotate crash reports so that crash pings are sent correctly and listen
// to pref changes to adjust the annotations accordingly.
for (let pref of this.OBSERVED_PREFERENCES) {
Services.prefs.addObserver(pref, this, true);
}
this._annotateCrashReport();
// Check the pending pings on disk now.
try {
await this._checkPendingPings();
} catch (ex) {
this._log.error("setup - _checkPendingPings rejected", ex);
}
// Enforce the pending pings storage quota. It could take a while so don't
// block on it.
TelemetryStorage.runEnforcePendingPingsQuotaTask();
// Start sending pings, but don't block on this.
SendScheduler.triggerSendingPings(true);
},
/**
* Triggers the crash report annotations depending on the current
* configuration. This communicates to the crash reporter if it can send a
* crash ping or not. This method can be called safely before setup() has
* been called.
*/
_annotateCrashReport() {
try {
const cr = Cc["@mozilla.org/toolkit/crash-reporter;1"];
if (cr) {
const crs = cr.getService(Ci.nsICrashReporter);
let clientId = ClientID.getCachedClientID();
let server = this._server || Services.prefs.getStringPref(TelemetryUtils.Preferences.Server, undefined);
if (!this.sendingEnabled() || !TelemetryReportingPolicy.canUpload()) {
// If we cannot send pings then clear the crash annotations
crs.removeCrashReportAnnotation("TelemetryClientId");
crs.removeCrashReportAnnotation("TelemetryServerURL");
} else {
crs.annotateCrashReport("TelemetryClientId", clientId);
crs.annotateCrashReport("TelemetryServerURL", server);
}
}
} catch (e) {
// Ignore errors when crash reporting is disabled
}
},
/**
* Discard old pings from the pending pings and detect overdue ones.
* @return {Boolean} True if we have overdue pings, false otherwise.
*/
async _checkPendingPings() {
// Scan the pending pings - that gives us a list sorted by last modified, descending.
let infos = await TelemetryStorage.loadPendingPingList();
this._log.info("_checkPendingPings - pending ping count: " + infos.length);
if (infos.length == 0) {
this._log.trace("_checkPendingPings - no pending pings");
return;
}
const now = Policy.now();
// Submit the age of the pending pings.
for (let pingInfo of infos) {
const ageInDays =
Utils.millisecondsToDays(Math.abs(now.getTime() - pingInfo.lastModificationDate));
Telemetry.getHistogramById("TELEMETRY_PENDING_PINGS_AGE").add(ageInDays);
}
},
async shutdown() {
this._shutdown = true;
for (let pref of this.OBSERVED_PREFERENCES) {
// FIXME: When running tests this causes errors to be printed out if
// TelemetrySend.shutdown() is called twice in a row without calling
// TelemetrySend.setup() in-between.
Services.prefs.removeObserver(pref, this);
}
for (let topic of this.OBSERVER_TOPICS) {
try {
Services.obs.removeObserver(this, topic);
} catch (ex) {
this._log.error("shutdown - failed to remove observer for " + topic, ex);
}
}
// We can't send anymore now.
this._sendingEnabled = false;
// Cancel any outgoing requests.
await this._cancelOutgoingRequests();
// Stop any active send tasks.
await SendScheduler.shutdown();
// Wait for any outstanding async ping activity.
await this.promisePendingPingActivity();
// Save any outstanding pending pings to disk.
await this._persistCurrentPings();
},
reset() {
this._log.trace("reset");
this._shutdown = false;
this._currentPings = new Map();
this._tooLateToSend = false;
this._isOSShutdown = false;
this._sendingEnabled = true;
const histograms = [
"TELEMETRY_SUCCESS",
"TELEMETRY_SEND_SUCCESS",
"TELEMETRY_SEND_FAILURE",
"TELEMETRY_SEND_FAILURE_TYPE",
];
histograms.forEach(h => Telemetry.getHistogramById(h).clear());
return SendScheduler.reset();
},
/**
* Notify that we can start submitting data to the servers.
*/
notifyCanUpload() {
if (!this._sendingEnabled) {
this._log.trace("notifyCanUpload - notifying before sending is enabled. Ignoring.");
return Promise.resolve();
}
// Let the scheduler trigger sending pings if possible, also inform the
// crash reporter that it can send crash pings if appropriate.
SendScheduler.triggerSendingPings(true);
this._annotateCrashReport();
return this.promisePendingPingActivity();
},
observe(subject, topic, data) {
let setOSShutdown = () => {
this._log.trace("setOSShutdown - in OS shutdown");
this._isOSShutdown = true;
};
switch (topic) {
case TOPIC_IDLE_DAILY:
SendScheduler.triggerSendingPings(true);
break;
case TOPIC_QUIT_APPLICATION_FORCED:
setOSShutdown();
break;
case TOPIC_QUIT_APPLICATION_GRANTED:
if (data == "syncShutdown") {
setOSShutdown();
}
break;
case PREF_CHANGED_TOPIC:
if (this.OBSERVED_PREFERENCES.includes(data)) {
this._annotateCrashReport();
}
break;
case TOPIC_PROFILE_CHANGE_NET_TEARDOWN:
this._tooLateToSend = true;
break;
}
},
/**
* Spawn the PingSender process that sends a ping. This function does
* not return an error or throw, it only logs an error.
*
* Even if the function doesn't fail, it doesn't mean that the ping was
* successfully sent, as we have no control over the spawned process. If it,
* succeeds, the ping is eventually removed from the disk to prevent duplicated
* submissions.
*
* @param {String} pingId The id of the ping to send.
* @param {String} submissionURL The complete Telemetry-compliant URL for the ping.
*/
_sendWithPingSender(pingId, submissionURL) {
this._log.trace("_sendWithPingSender - sending " + pingId + " to " + submissionURL);
try {
const pingPath = OS.Path.join(TelemetryStorage.pingDirectoryPath, pingId);
this.runPingSender(submissionURL, pingPath);
} catch (e) {
this._log.error("_sendWithPingSender - failed to submit ping", e);
}
},
submitPing(ping, options) {
this._log.trace("submitPing - ping id: " + ping.id + ", options: " + JSON.stringify(options));
if (!this.sendingEnabled(ping)) {
this._log.trace("submitPing - Telemetry is not allowed to send pings.");
return Promise.resolve();
}
// Send the ping using the PingSender, if requested and the user was
// notified of our policy. We don't support the pingsender on Android,
// so ignore this option on that platform (see bug 1335917).
// Moreover, if the OS is shutting down, we don't want to spawn the
// pingsender as it could unnecessarily slow down OS shutdown.
// Additionally, it could be be killed before it can complete its tasks,
// for example after successfully sending the ping but before removing
// the copy from the disk, resulting in receiving duplicate pings when
// Firefox restarts.
if (options.usePingSender &&
!this._isOSShutdown &&
TelemetryReportingPolicy.canUpload() &&
AppConstants.platform != "android") {
const url = this._buildSubmissionURL(ping);
// Serialize the ping to the disk and then spawn the PingSender.
return savePing(ping).then(() => this._sendWithPingSender(ping.id, url));
}
if (!this.canSendNow) {
// Sending is disabled or throttled, add this to the persisted pending pings.
this._log.trace("submitPing - can't send ping now, persisting to disk - " +
"canSendNow: " + this.canSendNow);
return savePing(ping);
}
// Let the scheduler trigger sending pings if possible.
// As a safety mechanism, this resets any currently active throttling.
this._log.trace("submitPing - can send pings, trying to send now");
this._currentPings.set(ping.id, ping);
SendScheduler.triggerSendingPings(true);
return Promise.resolve();
},
/**
* Only used in tests.
*/
setServer(server) {
this._log.trace("setServer", server);
this._server = server;
},
/**
* Clear out unpersisted, yet to be sent, pings and cancel outgoing ping requests.
*/
async clearCurrentPings() {
if (this._shutdown) {
this._log.trace("clearCurrentPings - in shutdown, bailing out");
return;
}
// Temporarily disable the scheduler. It must not try to reschedule ping sending
// while we're deleting them.
await SendScheduler.shutdown();
// Now that the ping activity has settled, abort outstanding ping requests.
this._cancelOutgoingRequests();
// Also, purge current pings.
this._currentPings.clear();
// We might have been interrupted and shutdown could have been started.
// We need to bail out in that case to avoid triggering send activity etc.
// at unexpected times.
if (this._shutdown) {
this._log.trace("clearCurrentPings - in shutdown, not spinning SendScheduler up again");
return;
}
// Enable the scheduler again and spin the send task.
SendScheduler.start();
SendScheduler.triggerSendingPings(true);
},
_cancelOutgoingRequests() {
// Abort any pending ping XHRs.
for (let [id, request] of this._pendingPingRequests) {
this._log.trace("_cancelOutgoingRequests - aborting ping request for id " + id);
try {
request.abort();
} catch (e) {
this._log.error("_cancelOutgoingRequests - failed to abort request for id " + id, e);
}
}
this._pendingPingRequests.clear();
},
sendPings(currentPings, persistedPingIds) {
let pingSends = [];
// Prioritize health pings to enable low-latency monitoring.
currentPings = [
...currentPings.filter(ping => ping.type === "health"),
...currentPings.filter(ping => ping.type !== "health"),
];
for (let current of currentPings) {
let ping = current;
let p = (async () => {
try {
await this._doPing(ping, ping.id, false);
} catch (ex) {
if (isOptoutPing(ping)) {
// Optout pings should only be tried once and then discarded.
this._log.info("sendPings - optout ping " + ping.id + " not sent, discarding", ex);
} else {
this._log.info("sendPings - ping " + ping.id + " not sent, saving to disk", ex);
await savePing(ping);
}
} finally {
this._currentPings.delete(ping.id);
}
})();
this._trackPendingPingTask(p);
pingSends.push(p);
}
if (persistedPingIds.length > 0) {
pingSends.push(this._sendPersistedPings(persistedPingIds).catch((ex) => {
this._log.info("sendPings - persisted pings not sent", ex);
}));
}
return Promise.all(pingSends);
},
/**
* Send the persisted pings to the server.
*
* @param {Array<string>} List of ping ids that should be sent.
*
* @return Promise A promise that is resolved when all pings finished sending or failed.
*/
async _sendPersistedPings(pingIds) {
this._log.trace("sendPersistedPings");
if (this.pendingPingCount < 1) {
this._log.trace("_sendPersistedPings - no pings to send");
return;