summaryrefslogtreecommitdiff
path: root/toolkit/components/telemetry/TelemetryTimestamps.jsm
blob: e49d7453c0ae416bea0b9225cfd19c6b93488878 (plain)
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
/* 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.EXPORTED_SYMBOLS = ["TelemetryTimestamps"];

const Cu = Components.utils;

/**
 * This module's purpose is to collect timestamps for important
 * application-specific events.
 *
 * The TelemetryController component attaches the timestamps stored by this module to
 * the telemetry submission, substracting the process lifetime so that the times
 * are relative to process startup. The overall goal is to produce a basic
 * timeline of the startup process.
 */
var timeStamps = {};

this.TelemetryTimestamps = {
  /**
   * Adds a timestamp to the list. The addition of TimeStamps that already have
   * a value stored is ignored.
   *
   * @param name must be a unique, generally "camelCase" descriptor of what the
   *             timestamp represents. e.g.: "delayedStartupStarted"
   * @param value is a timeStamp in milliseconds since the epoch. If omitted,
   *              defaults to Date.now().
   */
  add: function TT_add(name, value) {
    // Default to "now" if not specified
    if (value == null)
      value = Date.now();

    if (isNaN(value))
      throw new Error("Value must be a timestamp");

    // If there's an existing value, just ignore the new value.
    if (timeStamps.hasOwnProperty(name))
      return;

    timeStamps[name] = value;
  },

  /**
   * Returns a JS object containing all of the timeStamps as properties (can be
   * easily serialized to JSON). Used by TelemetryController to retrieve the data
   * to attach to the telemetry submission.
   */
  get: function TT_get() {
    // Return a copy of the object.
    return Cu.cloneInto(timeStamps, {});
  }
};