Bug 1239296 - Add telemetry_handler function to mach context r=gps
authorDan Minor <dminor@mozilla.com>
Fri, 29 Jan 2016 13:06:36 -0500
changeset 283810 585954cce5afff2214a61ecb135774996260fb71
parent 283809 6c2610f9334aaecbc521573e493ed164ebff1a43
child 283811 e4d801abd8dd62ac3ee4426a9e041774722a257f
push id29991
push usercbook@mozilla.com
push dateThu, 11 Feb 2016 10:52:20 +0000
treeherdermozilla-central@d4d72e7b30da [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgps
bugs1239296
milestone47.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
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:
@@ -217,16 +219,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...
@@ -300,16 +327,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'])