Bug 932092 - Data format for UI telemetry collection. r=mfinkle, a=bajaj.
authorChenxia Liu <liuche@mozilla.com>
Wed, 27 Nov 2013 13:58:55 -0500
changeset 167584 af2d122f17cadea0ee6a917aad6bf5a8cd808ea2
parent 167583 56f4f6a8d4d97ab81465bbb96aaf7b8f81e5e1d9
child 167585 77741eb02905ed06f545c3f55032c45776f1ab0b
push id428
push userbbajaj@mozilla.com
push dateTue, 28 Jan 2014 00:16:25 +0000
treeherdermozilla-release@cd72a7ff3a75 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmfinkle, bajaj
bugs932092
milestone27.0a2
Bug 932092 - Data format for UI telemetry collection. r=mfinkle, a=bajaj.
mobile/android/chrome/content/browser.js
toolkit/components/telemetry/UITelemetry.jsm
toolkit/components/telemetry/moz.build
--- a/mobile/android/chrome/content/browser.js
+++ b/mobile/android/chrome/content/browser.js
@@ -15,16 +15,17 @@ Cu.import("resource://gre/modules/Servic
 Cu.import("resource://gre/modules/AddonManager.jsm");
 Cu.import("resource://gre/modules/FileUtils.jsm");
 Cu.import("resource://gre/modules/JNI.jsm");
 Cu.import('resource://gre/modules/Payment.jsm');
 Cu.import("resource://gre/modules/PermissionPromptHelper.jsm");
 Cu.import("resource://gre/modules/ContactService.jsm");
 Cu.import("resource://gre/modules/NotificationDB.jsm");
 Cu.import("resource://gre/modules/SpatialNavigation.jsm");
+Cu.import("resource://gre/modules/UITelemetry.jsm");
 
 #ifdef ACCESSIBILITY
 Cu.import("resource://gre/modules/accessibility/AccessFu.jsm");
 #endif
 
 XPCOMUtils.defineLazyModuleGetter(this, "PluralForm",
                                   "resource://gre/modules/PluralForm.jsm");
 
@@ -332,16 +333,17 @@ var BrowserApp = {
     ActivityObserver.init();
     WebappsUI.init();
     RemoteDebugger.init();
     Reader.init();
     UserAgentOverrides.init();
     DesktopUserAgent.init();
     Distribution.init();
     Tabs.init();
+    UITelemetry.init();
 #ifdef ACCESSIBILITY
     AccessFu.attach(window);
 #endif
 
     // Init LoginManager
     Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager);
 
     let url = null;
new file mode 100644
--- /dev/null
+++ b/toolkit/components/telemetry/UITelemetry.jsm
@@ -0,0 +1,103 @@
+/* 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/. */
+
+"use strict";
+
+const Cu = Components.utils;
+
+this.EXPORTED_SYMBOLS = [
+  "UITelemetry"
+];
+
+Cu.import("resource://gre/modules/Services.jsm");
+
+/**
+ * UITelemetry is a helper JSM used to record UI specific telemetry events.
+ */
+this.UITelemetry =  {
+
+  measurements: [],
+
+  init: function init() {
+    Services.obs.addObserver(this, "UITelemetry:Event", false);
+    Services.obs.addObserver(this, "UITelemetry:Session", false);
+  },
+
+  observe: function observe(aMessage, aTopic, aData) {
+    switch(aTopic) {
+      case "UITelemetry:Event":
+        let args = JSON.parse(aData);
+        this.addEvent(args.action, args.method, args.extras, args.timestamp);
+        break;
+      case "UITelemetry:Session":
+        args = JSON.parse(aData);
+        let sessionName = args.name;
+        let timestamp = args.timestamp;
+        if (args.state == "start") {
+          this.startSession(sessionName, timestamp);
+        } else if (args.state == "stop") {
+          this.stopSession(sessionName, timestamp);
+        }
+        break;
+    }
+  },
+
+  /**
+   * Adds a single event described by an action, and the calling method. Optional
+   * paramaters are extras and timestamp. The timestamp will be set here if it is
+   * not passed in by the caller.
+   */
+  addEvent: function addEvent(aAction, aMethod, aExtras, aTimestamp) {
+    let timestamp = aTimestamp || Date.now();
+    let aEvent = {
+      type: "event",
+      action: aAction,
+      method: aMethod,
+      timestamp: timestamp
+    };
+
+    if (aExtras) aEvent.extras = aExtras;
+    this._logEvent(aEvent);
+  },
+
+  activeSessions: {},
+
+  /**
+   * Begins tracking a session by storing a timestamp for session start.
+   */
+  startSession: function startSession(aName, aTimestamp) {
+   let timestamp = aTimestamp || Date.now();
+   if (this.activeSessions[aName]) {
+    // Do not overwrite a previous event start if it already exsts.
+    return;
+   }
+   this.activeSessions[aName] = timestamp;
+  },
+
+  /**
+   * Tracks the end of a session with a timestamp.
+   */
+  stopSession: function stopSession(aName, aTimestamp) {
+    let timestamp = aTimestamp || Date.now();
+    let sessionStart = this.activeSessions[aName];
+
+    if (!sessionStart) {
+      Services.console.logStringMessage("UITelemetry error: no session [" + aName + "] to stop!");
+      return;
+    }
+
+    let aEvent = {
+      type: "session",
+      name: aName,
+      start: sessionStart,
+      end: timestamp
+    };
+
+    this._logEvent(aEvent);
+  },
+
+  _logEvent: function sendEvent(aEvent) {
+    this.measurements.push(aEvent);
+  }
+};
--- a/toolkit/components/telemetry/moz.build
+++ b/toolkit/components/telemetry/moz.build
@@ -29,16 +29,17 @@ EXTRA_COMPONENTS += [
 EXTRA_PP_COMPONENTS += [
     'TelemetryPing.js',
 ]
 
 EXTRA_JS_MODULES += [
     'TelemetryFile.jsm',
     'TelemetryStopwatch.jsm',
     'ThirdPartyCookieProbe.jsm',
+    'UITelemetry.jsm',
 ]
 
 FAIL_ON_WARNINGS = True
 
 LIBXUL_LIBRARY = True
 
 MSVC_ENABLE_PGO = True