Bug 1462725 - Add TelemetryEvents class to Savant JSM; r=rhelmer, a=RyanVM
authorBianca Danforth <bdanforth@mozilla.com>
Mon, 04 Jun 2018 16:19:09 -0700
changeset 473701 055f4112239953fd379d2a73ec1af7ae81abd652
parent 473700 6d9e80629c215964674d5d7ee39ccf531334fdb5
child 473702 8b46c0eeed0efb79837acfbaed9df3ceaf82686e
push id1728
push userjlund@mozilla.com
push dateMon, 18 Jun 2018 21:12:27 +0000
treeherdermozilla-release@c296fde26f5f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrhelmer, RyanVM
bugs1462725
milestone61.0
Bug 1462725 - Add TelemetryEvents class to Savant JSM; r=rhelmer, a=RyanVM This class will handle enabling/disabling the "savant" category of event telemetry. MozReview-Commit-ID: ClnkSnzL2vd
browser/modules/ShieldStudySavant.jsm
--- a/browser/modules/ShieldStudySavant.jsm
+++ b/browser/modules/ShieldStudySavant.jsm
@@ -20,56 +20,87 @@ XPCOMUtils.defineLazyGetter(this, "log",
     prefix: "ShieldStudySavant",
   };
   return new ConsoleAPI(consoleOptions);
 });
 
 class ShieldStudySavantClass {
   constructor() {
     this.SHIELD_STUDY_SAVANT_PREF = "shield.savant.enabled";
+    this.STUDY_TELEMETRY_CATEGORY = "savant";
   }
 
   init() {
+    this.TelemetryEvents = new TelemetryEvents(this.STUDY_TELEMETRY_CATEGORY);
+
+    // TODO: implement eligibility (#13)
+    const isEligible = true;
+    if (!isEligible) {
+      this.endStudy("ineligible");
+      return;
+    }
     // check the pref in case Normandy flipped it on before we could add the pref listener
     this.shouldCollect = Services.prefs.getBoolPref(this.SHIELD_STUDY_SAVANT_PREF);
     if (this.shouldCollect) {
-      this.enableCollection();
+      this.TelemetryEvents.enableCollection();
     }
     Services.prefs.addObserver(this.SHIELD_STUDY_SAVANT_PREF, this);
   }
 
   observe(subject, topic, data) {
     if (topic === "nsPref:changed" && data === this.SHIELD_STUDY_SAVANT_PREF) {
       // toggle state of the pref
       this.shouldCollect = !this.shouldCollect;
       if (this.shouldCollect) {
-        this.enableCollection();
+        this.TelemetryEvents.enableCollection();
       } else {
-        // Normandy has flipped off the pref
+        // The pref has been turned off
         this.endStudy("expired");
       }
     }
   }
 
-  enableCollection() {
-    log.debug("Study has been enabled; turning on data collection.");
-    // TODO: enable data collection
+  sendEvent(method, object, value, extra) {
+    this.TelemetryEvents.sendEvent(method, object, value, extra);
   }
 
   endStudy(reason) {
-    this.disableCollection();
+    this.TelemetryEvents.disableCollection();
     // TODO: send endStudy ping with reason code
     this.uninit();
   }
 
-  disableCollection() {
-    log.debug("Study has been disabled; turning off data collection.");
-    // TODO: disable data collection
-  }
-
+  // Called on every Firefox shutdown and endStudy
   uninit() {
+    // TODO: Make sure uninit() is called on every Firefox shutdown (look inside
+    // nsBrowserGlue.js to see where Normandy uninits)
+    // TODO: See what happens during Normandy's uninit method to ensure nothing
+    // is forgotten.
     Services.prefs.removeObserver(this.SHIELD_STUDY_SAVANT_PREF, this);
     Services.prefs.clearUserPref(this.SHIELD_STUDY_SAVANT_PREF);
     Services.prefs.clearUserPref(PREF_LOG_LEVEL);
   }
-};
+}
 
 const ShieldStudySavant = new ShieldStudySavantClass();
+
+// references:
+// - https://hg.mozilla.org/mozilla-central/file/tip/toolkit/components/normandy/lib/TelemetryEvents.jsm
+// - https://hg.mozilla.org/mozilla-central/file/tip/toolkit/components/normandy/lib/PreferenceExperiments.jsm#l357
+class TelemetryEvents {
+  constructor(studyCategory) {
+    this.STUDY_TELEMETRY_CATEGORY = studyCategory;
+  }
+
+  sendEvent(method, object, value, extra) {
+    Services.telemetry.recordEvent(this.STUDY_TELEMETRY_CATEGORY, method, object, value, extra);
+  }
+
+  enableCollection() {
+    log.debug("Study has been enabled; turning ON data collection.");
+    // Services.telemetry.setEventRecordingEnabled(this.STUDY_TELEMETRY_CATEGORY, true);
+  }
+
+  disableCollection() {
+    log.debug("Study has been disabled; turning OFF data collection.");
+    // Services.telemetry.setEventRecordingEnabled(this.STUDY_TELEMETRY_CATEGORY, false);
+  }
+}