Bug 1239296 - Add telemetry_handler function to mach context r?gps draft
authorDan Minor <dminor@mozilla.com>
Fri, 29 Jan 2016 13:06:36 -0500
changeset 329829 3722b49cc42c969636cf5f557445a68dc8b566df
parent 329801 2dfb45d74f42d2a0010696f5fd47c7a7da94cedb
child 329830 19c3ef89f4eb704f6cf8cf0da75bb9c4c3061199
push id10616
push userdminor@mozilla.com
push dateTue, 09 Feb 2016 16:18:53 +0000
reviewersgps
bugs1239296
milestone47.0a1
Bug 1239296 - Add telemetry_handler function to mach context r?gps This adds a telemetry_handler function to the mach context that can be used to store telemetry data in the state directory for later reporting.
build/mach_bootstrap.py
--- a/build/mach_bootstrap.py
+++ b/build/mach_bootstrap.py
@@ -1,19 +1,21 @@
 # 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/.
 
 from __future__ import print_function, unicode_literals
 
 import errno
+import json
 import os
 import platform
 import sys
 import time
+import uuid
 import __builtin__
 
 from types import ModuleType
 
 
 STATE_DIR_FIRST_RUN = '''
 mach and the build system store shared state in a common directory on the
 filesystem. The following directory will be created:
@@ -214,16 +216,41 @@ def bootstrap(topsrcdir, mozilla_dir=Non
     # to react. We always exit after creating the directory because users don't
     # like surprises.
     try:
         import mach.main
     except ImportError:
         sys.path[0:0] = [os.path.join(mozilla_dir, path) for path in SEARCH_PATHS]
         import mach.main
 
+    def telemetry_handler(context, data):
+        # We have not opted-in to telemetry
+        if 'BUILD_SYSTEM_TELEMETRY' not in os.environ:
+            return
+
+        telemetry_dir = os.path.join(get_state_dir()[0], 'telemetry')
+        try:
+            os.mkdir(telemetry_dir)
+        except OSError as e:
+            if e.errno != errno.EEXIST:
+                raise
+        outgoing_dir = os.path.join(telemetry_dir, 'outgoing')
+        try:
+            os.mkdir(outgoing_dir)
+        except OSError as e:
+            if e.errno != errno.EEXIST:
+                raise
+
+        # Add common metadata to help submit sorted data later on.
+        # For now, we'll just record the mach command that was invoked.
+        data['argv'] = sys.argv
+
+        with open(os.path.join(outgoing_dir, str(uuid.uuid4())), 'w') as f:
+            json.dump(data, f, sort_keys=True)
+
     def pre_dispatch_handler(context, handler, args):
         """Perform global checks before command dispatch.
 
         Currently, our goal is to ensure developers periodically run
         `mach mercurial-setup` (when applicable) to ensure their Mercurial
         tools are up to date.
         """
         # Don't do anything when...
@@ -295,16 +322,19 @@ def bootstrap(topsrcdir, mozilla_dir=Non
             return state_dir
 
         if key == 'topdir':
             return topsrcdir
 
         if key == 'pre_dispatch_handler':
             return pre_dispatch_handler
 
+        if key == 'telemetry_handler':
+            return telemetry_handler
+
         raise AttributeError(key)
 
     mach = mach.main.Mach(os.getcwd())
     mach.populate_context_handler = populate_context
 
     for category, meta in CATEGORIES.items():
         mach.define_category(category, meta['short'], meta['long'],
             meta['priority'])