toolkit/modules/TelemetryTimestamps.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/modules/TelemetryTimestamps.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,54 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +this.EXPORTED_SYMBOLS = ["TelemetryTimestamps"];
     1.9 +
    1.10 +const Cu = Components.utils;
    1.11 +
    1.12 +/**
    1.13 + * This module's purpose is to collect timestamps for important
    1.14 + * application-specific events.
    1.15 + *
    1.16 + * The TelemetryPing component attaches the timestamps stored by this module to
    1.17 + * the telemetry submission, substracting the process lifetime so that the times
    1.18 + * are relative to process startup. The overall goal is to produce a basic
    1.19 + * timeline of the startup process.
    1.20 + */
    1.21 +let timeStamps = {};
    1.22 +
    1.23 +this.TelemetryTimestamps = {
    1.24 +  /**
    1.25 +   * Adds a timestamp to the list. The addition of TimeStamps that already have
    1.26 +   * a value stored is ignored.
    1.27 +   *
    1.28 +   * @param name must be a unique, generally "camelCase" descriptor of what the
    1.29 +   *             timestamp represents. e.g.: "delayedStartupStarted"
    1.30 +   * @param value is a timeStamp in milliseconds since the epoch. If omitted,
    1.31 +   *              defaults to Date.now().
    1.32 +   */
    1.33 +  add: function TT_add(name, value) {
    1.34 +    // Default to "now" if not specified
    1.35 +    if (value == null)
    1.36 +      value = Date.now();
    1.37 +
    1.38 +    if (isNaN(value))
    1.39 +      throw new Error("Value must be a timestamp");
    1.40 +
    1.41 +    // If there's an existing value, just ignore the new value.
    1.42 +    if (timeStamps.hasOwnProperty(name))
    1.43 +      return;
    1.44 +
    1.45 +    timeStamps[name] = value;
    1.46 +  },
    1.47 +
    1.48 +  /**
    1.49 +   * Returns a JS object containing all of the timeStamps as properties (can be
    1.50 +   * easily serialized to JSON). Used by TelemetryPing to retrieve the data
    1.51 +   * to attach to the telemetry submission.
    1.52 +   */
    1.53 +  get: function TT_get() {
    1.54 +    // Return a copy of the object.
    1.55 +    return Cu.cloneInto(timeStamps, {});
    1.56 +  }
    1.57 +};

mercurial